No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

field_data_handler.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. import importlib
  3. class FieldDataHandler(object):
  4. help_text = 'Generic Field Data Handler'
  5. ## @brief List fields that will be exposed to the construct_data_method
  6. _construct_datas_deps = []
  7. ## @brief constructor
  8. # @param internal False | str : define whether or not a field is internal
  9. # @param immutable bool : indicates if the fieldtype has to be defined in child classes of LeObject or if it is designed globally and immutable
  10. # @param **args
  11. def __init__(self, internal=False, immutable=False, **args):
  12. self.internal = internal # Check this value ?
  13. self.immutable = bool(immutable)
  14. for argname, argval in args.items():
  15. setattr(self, argname, argval)
  16. ## Fieldtype name
  17. @staticmethod
  18. def name(cls):
  19. return cls.__module__.split('.')[-1]
  20. ## @brief checks if a fieldtype is internal
  21. # @return bool
  22. def is_internal(self):
  23. return self.internal != False
  24. ## @brief calls the data_field defined _check_data_value() method
  25. # @return tuple (value, error|None)
  26. def check_data_value(self, value):
  27. return self._check_data_value(value)
  28. def _check_data_value(self, value):
  29. return (value, None)
  30. ## @brief given a field type name, returns the associated python class
  31. # @param fieldtype_name str : A field type name
  32. # @return DataField child class
  33. @staticmethod
  34. def from_name(fieldtype_name):
  35. mod = importlib.import_module(FieldDataHandler.module_name(fieldtype_name))
  36. return mod.EmDataField
  37. ## @brief get a module name given a fieldtype name
  38. # @param fieldtype_name str : a field type name
  39. # @return a string representing a python module name
  40. @staticmethod
  41. def module_name(self, fieldtype_name):
  42. return 'leapi.datahandlers.data_fields.%s' % fieldtype_name
  43. ## @brief __hash__ implementation for fieldtypes
  44. def __hash__(self):
  45. hash_dats = [self.__class__.__module__]
  46. for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
  47. hash_dats.append((kdic, getattr(self, kdic)))
  48. return hash(tuple(hash_dats))