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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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
  10. # designed globally and immutable
  11. # @param **args
  12. # @throw NotImplementedError if it is instanciated directly
  13. def __init__(self, internal=False, immutable=False, **args):
  14. if self.__class__ == FieldDataHandler:
  15. raise NotImplementedError("Abstract class")
  16. self.internal = internal # Check this value ?
  17. self.immutable = bool(immutable)
  18. for argname, argval in args.items():
  19. setattr(self, argname, argval)
  20. ## Fieldtype name
  21. @staticmethod
  22. def name(cls):
  23. return cls.__module__.split('.')[-1]
  24. ## @brief checks if a fieldtype is internal
  25. # @return bool
  26. def is_internal(self):
  27. return self.internal is not False
  28. ## @brief calls the data_field defined _check_data_value() method
  29. # @return tuple (value, error|None)
  30. def check_data_value(self, value):
  31. return self._check_data_value(value)
  32. def _check_data_value(self, value):
  33. return value, None
  34. ## @brief checks if this class can override the given data handler
  35. # @param data_handler DataHandler
  36. # @return bool
  37. def can_override(self, data_handler):
  38. if data_handler.__class__.base_type != self.__class__.base_type:
  39. return False
  40. return True
  41. ## @brief Build field value
  42. # @param emcomponent EmComponent : An EmComponent child class instance
  43. # @param fname str : The field name
  44. # @param datas dict : dict storing fields values (from the component)
  45. # @param cur_value : the value from the current field (identified by fieldname)
  46. def construct_data(self, emcomponent, fname, datas, cur_value):
  47. emcomponent_fields = emcomponent.fields()
  48. fname_data_handler = None
  49. if fname in emcomponent_fields:
  50. fname_data_handler = FieldDataHandler.from_name(emcomponent_fields[fname])
  51. if fname in datas.keys():
  52. return cur_value
  53. elif fname_data_handler is not None and hasattr(fname_data_handler, 'default'):
  54. return fname_data_handler.default
  55. elif fname_data_handler is not None and fname_data_handler.nullable:
  56. return None
  57. raise RuntimeError("Unable to construct data for field %s", fname)
  58. ## @brief Check datas consistency
  59. # @param emcomponent EmComponent : An EmComponent child class instance
  60. # @param fname : the field name
  61. # @param datas dict : dict storing fields values
  62. # @return an Exception instance if fails else True
  63. # @todo A implémenter
  64. def check_data_consistency(self, emcomponent, fname, datas):
  65. return True
  66. ## @brief given a field type name, returns the associated python class
  67. # @param fieldtype_name str : A field type name
  68. # @return DataField child class
  69. @staticmethod
  70. def from_name(data_handler_name):
  71. data_handler_name = data_handler_name.lower()
  72. mod = None
  73. for mname in FieldDataHandler.modules_name(data_handler_name):
  74. try:
  75. mod = importlib.import_module(mname)
  76. except ImportError:
  77. pass
  78. if mod is None:
  79. raise NameError("Unknown data_handler name : '%s'" % data_handler_name)
  80. return mod.DataHandler
  81. ## @brief get a module name given a fieldtype name
  82. # @param fieldtype_name str : a field type name
  83. # @return a string representing a python module name
  84. @staticmethod
  85. def modules_name(fieldtype_name):
  86. return (
  87. 'lodel.leapi.datahandlers.data_fields.%s' % fieldtype_name,
  88. 'lodel.leapi.datahandlers.references.%s' % fieldtype_name
  89. )
  90. ## @brief __hash__ implementation for fieldtypes
  91. def __hash__(self):
  92. hash_dats = [self.__class__.__module__]
  93. for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
  94. hash_dats.append((kdic, getattr(self, kdic)))
  95. return hash(tuple(hash_dats))