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.

base_classes.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # -*- coding: utf-8 -*-
  2. ## @package lodel.leapi.datahandlers.base_classes Define all base/abstract class for data handlers
  3. #
  4. # Contains custom exceptions too
  5. import copy
  6. import importlib
  7. import inspect
  8. from lodel import logger
  9. class FieldValidationError(Exception):
  10. pass
  11. ##@brief Base class for all data handlers
  12. class DataHandler(object):
  13. _HANDLERS_MODULES = ('datas_base', 'datas', 'references')
  14. ##@brief Stores the DataHandler childs classes indexed by name
  15. _base_handlers = None
  16. ##@brief Stores custom datahandlers classes indexed by name
  17. # @todo do it ! (like plugins, register handlers... blablabla)
  18. __custom_handlers = dict()
  19. help_text = 'Generic Field Data Handler'
  20. ##@brief List fields that will be exposed to the construct_data_method
  21. _construct_datas_deps = []
  22. ##@brief constructor
  23. # @param internal False | str : define whether or not a field is internal
  24. # @param immutable bool : indicates if the fieldtype has to be defined in child classes of LeObject or if it is
  25. # designed globally and immutable
  26. # @param **args
  27. # @throw NotImplementedError if it is instanciated directly
  28. def __init__(self, **kwargs):
  29. if self.__class__ == DataHandler:
  30. raise NotImplementedError("Abstract class")
  31. self.__arguments = kwargs
  32. self.nullable = True
  33. self.uniq = False
  34. self.immutable = False
  35. self.primary_key = False
  36. self.internal = False
  37. if 'defaults' in kwargs:
  38. self.default, error = self.check_data_value(kwargs['default'])
  39. if error:
  40. raise error
  41. del(args['default'])
  42. for argname, argval in kwargs.items():
  43. setattr(self, argname, argval)
  44. ## Fieldtype name
  45. @staticmethod
  46. def name(cls):
  47. return cls.__module__.split('.')[-1]
  48. @classmethod
  49. def is_reference(cls):
  50. return issubclass(cls, Reference)
  51. def is_primary_key(self):
  52. return self.primary_key
  53. ##@brief checks if a fieldtype is internal
  54. # @return bool
  55. def is_internal(self):
  56. return self.internal is not False
  57. ##@brief calls the data_field defined _check_data_value() method
  58. # @return tuple (value, error|None)
  59. def check_data_value(self, value):
  60. if value is None:
  61. if not self.nullable:
  62. return None, TypeError("'None' value but field is not nullable")
  63. return None, None
  64. return self._check_data_value(value)
  65. ##@brief checks if this class can override the given data handler
  66. # @param data_handler DataHandler
  67. # @return bool
  68. def can_override(self, data_handler):
  69. if data_handler.__class__.base_type != self.__class__.base_type:
  70. return False
  71. return True
  72. ##@brief Build field value
  73. # @param emcomponent EmComponent : An EmComponent child class instance
  74. # @param fname str : The field name
  75. # @param datas dict : dict storing fields values (from the component)
  76. # @param cur_value : the value from the current field (identified by fieldname)
  77. # @return the value
  78. # @throw RunTimeError if data construction fails
  79. def construct_data(self, emcomponent, fname, datas, cur_value):
  80. emcomponent_fields = emcomponent.fields()
  81. data_handler = None
  82. if fname in emcomponent_fields:
  83. data_handler = emcomponent_fields[fname]
  84. if fname in datas.keys():
  85. return cur_value
  86. elif data_handler is not None and hasattr(data_handler, 'default'):
  87. return data_handler.default
  88. elif data_handler is not None and data_handler.nullable:
  89. return None
  90. return RuntimeError("Unable to construct data for field %s", fname)
  91. ##@brief Check datas consistency
  92. # @param emcomponent EmComponent : An EmComponent child class instance
  93. # @param fname : the field name
  94. # @param datas dict : dict storing fields values
  95. # @return an Exception instance if fails else True
  96. # @todo A implémenter
  97. def check_data_consistency(self, emcomponent, fname, datas):
  98. return True
  99. ##@brief This method is use by plugins to register new data handlers
  100. @classmethod
  101. def register_new_handler(cls, name, data_handler):
  102. if not inspect.isclass(data_handler):
  103. raise ValueError("A class was expected but %s given" % type(data_handler))
  104. if not issubclass(data_handler, DataHandler):
  105. raise ValueError("A data handler HAS TO be a child class of DataHandler")
  106. cls.__custom_handlers[name] = data_handler
  107. @classmethod
  108. def load_base_handlers(cls):
  109. if cls._base_handlers is None:
  110. cls._base_handlers = dict()
  111. for module_name in cls._HANDLERS_MODULES:
  112. module = importlib.import_module('lodel.leapi.datahandlers.%s' % module_name)
  113. for name, obj in inspect.getmembers(module):
  114. if inspect.isclass(obj):
  115. logger.debug("Load data handler %s.%s" % (obj.__module__, obj.__name__))
  116. cls._base_handlers[name.lower()] = obj
  117. return copy.copy(cls._base_handlers)
  118. ##@brief given a field type name, returns the associated python class
  119. # @param fieldtype_name str : A field type name (not case sensitive)
  120. # @return DataField child class
  121. # @todo implements custom handlers fetch
  122. # @note To access custom data handlers it can be cool to preffix the handler name by plugin name for example ? (to ensure name unicity)
  123. @classmethod
  124. def from_name(cls, name):
  125. cls.load_base_handlers()
  126. name = name.lower()
  127. if name not in cls._base_handlers:
  128. raise NameError("No data handlers named '%s'" % (name,))
  129. return cls._base_handlers[name]
  130. ##@brief Return the module name to import in order to use the datahandler
  131. # @param data_handler_name str : Data handler name
  132. # @return a str
  133. @classmethod
  134. def module_name(cls, name):
  135. name = name.lower()
  136. handler_class = cls.from_name(name)
  137. return '{module_name}.{class_name}'.format(
  138. module_name = handler_class.__module__,
  139. class_name = handler_class.__name__
  140. )
  141. ##@brief __hash__ implementation for fieldtypes
  142. def __hash__(self):
  143. hash_dats = [self.__class__.__module__]
  144. for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
  145. hash_dats.append((kdic, getattr(self, kdic)))
  146. return hash(tuple(hash_dats))
  147. ##@brief Base class for datas data handler (by opposition with references)
  148. class DataField(DataHandler):
  149. pass
  150. ##@brief Abstract class for all references
  151. #
  152. # References are fields that stores a reference to another
  153. # editorial object
  154. class Reference(DataHandler):
  155. ##@brief Instanciation
  156. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  157. # @param back_reference tuple | None : tuple containing (LeObject child class, fieldname)
  158. # @param internal bool : if False, the field is not internal
  159. # @param **kwargs : other arguments
  160. def __init__(self, allowed_classes = None, back_reference = None, internal=False, **kwargs):
  161. self.__allowed_classes = [] if allowed_classes is None else set(allowed_classes)
  162. if back_reference is not None:
  163. if len(back_reference) != 2:
  164. raise ValueError("A tuple (classname, fieldname) expected but got '%s'" % back_reference)
  165. #if not issubclass(back_reference[0], LeObject) or not isinstance(back_reference[1], str):
  166. # raise TypeError("Back reference was expected to be a tuple(<class LeObject>, str) but got : (%s, %s)" % (back_reference[0], back_reference[1]))
  167. self.__back_reference = back_reference
  168. super().__init__(internal=internal, **kwargs)
  169. @property
  170. def back_reference(self):
  171. return copy.copy(self.__back_reference)
  172. @property
  173. def linked_classes(self):
  174. return copy.copy(self.__allowed_classes)
  175. ##@brief Set the back reference for this field.
  176. def _set_back_reference(self, back_reference):
  177. self.__back_reference = back_reference
  178. ##@brief Check value
  179. # @param value *
  180. # @return tuple(value, exception)
  181. # @todo implement the check when we have LeObject to check value
  182. def _check_data_value(self, value):
  183. return value, None
  184. if isinstance(value, lodel.editorial_model.components.EmClass):
  185. value = [value]
  186. for elt in value:
  187. if not issubclass(elt.__class__, EmClass):
  188. return None, FieldValidationError("Some elements of this references are not EmClass instances")
  189. if self.__allowed_classes is not None:
  190. if not isinstance(elt, self.__allowed_classes):
  191. return None, FieldValidationError("Some element of this references are not valids (don't fit with allowed_classes")
  192. return value
  193. ##@brief This class represent a data_handler for single reference to another object
  194. #
  195. # The fields using this data handlers are like "foreign key" on another object
  196. class SingleRef(Reference):
  197. def __init__(self, allowed_classes = None, **kwargs):
  198. super().__init__(allowed_classes = allowed_classes)
  199. def _check_data_value(self, value):
  200. val, expt = super()._check_data_value(value)
  201. if not isinstance(expt, Exception):
  202. if len(val) > 1:
  203. return None, FieldValidationError("Only single values are allowed for SingleRef fields")
  204. return val, expt
  205. ##@brief This class represent a data_handler for multiple references to another object
  206. #
  207. # The fields using this data handlers are like SingleRef but can store multiple references in one field
  208. # @note SQL implementation could be tricky
  209. class MultipleRef(Reference):
  210. ##
  211. # @param max_item int | None : indicate the maximum number of item referenced by this field, None mean no limit
  212. def __init__(self, max_item = None, **kwargs):
  213. super().__init__(**kwargs)
  214. def _check_data_value(self, value):
  215. if self.max_item is not None:
  216. if self.max_item < len(value):
  217. return None, FieldValidationError("To many items")
  218. ## @brief Class designed to handle datas access will fieldtypes are constructing datas
  219. #
  220. # This class is designed to allow automatic scheduling of construct_data calls.
  221. #
  222. # In theory it's able to detect circular dependencies
  223. # @todo test circular deps detection
  224. # @todo test circulat deps false positiv
  225. class DatasConstructor(object):
  226. ## @brief Init a DatasConstructor
  227. # @param lec LeCrud : @ref LeObject child class
  228. # @param datas dict : dict with field name as key and field values as value
  229. # @param fields_handler dict : dict with field name as key and data handler instance as value
  230. def __init__(self, leobject, datas, fields_handler):
  231. ## Stores concerned class
  232. self._leobject = leobject
  233. ## Stores datas and constructed datas
  234. self._datas = copy.copy(datas)
  235. ## Stores fieldtypes
  236. self._fields_handler = fields_handler
  237. ## Stores list of fieldname for constructed datas
  238. self._constructed = []
  239. ## Stores construct calls list
  240. self._construct_calls = []
  241. ## @brief Implements the dict.keys() method on instance
  242. def keys(self):
  243. return self._datas.keys()
  244. ## @brief Allows to access the instance like a dict
  245. def __getitem__(self, fname):
  246. if fname not in self._constructed:
  247. if fname in self._construct_calls:
  248. raise RuntimeError('Probably circular dependencies in fieldtypes')
  249. cur_value = self._datas[fname] if fname in self._datas else None
  250. self._datas[fname] = self._fields_handler[fname].construct_data(self._leobject, fname, self, cur_value)
  251. self._constructed.append(fname)
  252. return self._datas[fname]
  253. ## @brief Allows to set instance values like a dict
  254. # @warning Should not append in theory
  255. def __setitem__(self, fname, value):
  256. self._datas[fname] = value
  257. warnings.warn("Setting value of an DatasConstructor instance")