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

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