暫無描述
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. fname_data_handler = None
  82. if fname in emcomponent_fields:
  83. fname_data_handler = DataHandler.from_name(emcomponent_fields[fname])
  84. if fname in datas.keys():
  85. return cur_value
  86. elif fname_data_handler is not None and hasattr(fname_data_handler, 'default'):
  87. return fname_data_handler.default
  88. elif fname_data_handler is not None and fname_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. name = name.lower()
  126. if name not in cls.__base_handlers:
  127. raise NameError("No data handlers named '%s'" % (name,))
  128. return cls.__base_handlers[name]
  129. ##@brief Return the module name to import in order to use the datahandler
  130. # @param data_handler_name str : Data handler name
  131. # @return a str
  132. @classmethod
  133. def module_name(cls, name):
  134. name = name.lower()
  135. handler_class = cls.from_name(name)
  136. return '{module_name}.{class_name}'.format(
  137. module_name = handler_class.__module__,
  138. class_name = handler_class.__name__
  139. )
  140. ##@brief __hash__ implementation for fieldtypes
  141. def __hash__(self):
  142. hash_dats = [self.__class__.__module__]
  143. for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
  144. hash_dats.append((kdic, getattr(self, kdic)))
  145. return hash(tuple(hash_dats))
  146. ##@brief Base class for datas data handler (by opposition with references)
  147. class DataField(DataHandler):
  148. pass
  149. ##@brief Abstract class for all references
  150. #
  151. # References are fields that stores a reference to another
  152. # editorial object
  153. class Reference(DataHandler):
  154. ##@brief Instanciation
  155. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  156. # @param back_reference tuple | None : tuple containing (LeObject child class, fieldname)
  157. # @param internal bool : if False, the field is not internal
  158. # @param **kwargs : other arguments
  159. def __init__(self, allowed_classes = None, back_reference = None, internal=False, **kwargs):
  160. self.__allowed_classes = None if allowed_classes is None else set(allowed_classes)
  161. if back_reference is not None:
  162. if len(back_reference) != 2:
  163. raise ValueError("A tuple (classname, fieldname) expected but got '%s'" % back_reference)
  164. #if not issubclass(back_reference[0], LeObject) or not isinstance(back_reference[1], str):
  165. # raise TypeError("Back reference was expected to be a tuple(<class LeObject>, str) but got : (%s, %s)" % (back_reference[0], back_reference[1]))
  166. self.__back_reference = back_reference
  167. super().__init__(internal=internal, **kwargs)
  168. @property
  169. def back_reference(self):
  170. return copy.copy(self.__back_reference)
  171. ##@brief Set the back reference for this field.
  172. def _set_back_reference(self, back_reference):
  173. self.__back_reference = back_reference
  174. ##@brief Check value
  175. # @param value *
  176. # @return tuple(value, exception)
  177. # @todo implement the check when we have LeObject to check value
  178. def _check_data_value(self, value):
  179. return value, None
  180. if isinstance(value, lodel.editorial_model.components.EmClass):
  181. value = [value]
  182. for elt in value:
  183. if not issubclass(elt.__class__, EmClass):
  184. return None, FieldValidationError("Some elements of this references are not EmClass instances")
  185. if self.__allowed_classes is not None:
  186. if not isinstance(elt, self.__allowed_classes):
  187. return None, FieldValidationError("Some element of this references are not valids (don't fit with allowed_classes")
  188. return value
  189. ##@brief This class represent a data_handler for single reference to another object
  190. #
  191. # The fields using this data handlers are like "foreign key" on another object
  192. class SingleRef(Reference):
  193. def __init__(self, allowed_classes = None, **kwargs):
  194. super().__init__(allowed_classes = allowed_classes)
  195. def _check_data_value(self, value):
  196. val, expt = super()._check_data_value(value)
  197. if not isinstance(expt, Exception):
  198. if len(val) > 1:
  199. return None, FieldValidationError("Only single values are allowed for SingleRef fields")
  200. return val, expt
  201. ##@brief This class represent a data_handler for multiple references to another object
  202. #
  203. # The fields using this data handlers are like SingleRef but can store multiple references in one field
  204. # @note SQL implementation could be tricky
  205. class MultipleRef(Reference):
  206. ##
  207. # @param max_item int | None : indicate the maximum number of item referenced by this field, None mean no limit
  208. def __init__(self, max_item = None, **kwargs):
  209. super().__init__(**kwargs)
  210. def _check_data_value(self, value):
  211. if self.max_item is not None:
  212. if self.max_item < len(value):
  213. return None, FieldValidationError("To many items")
  214. ## @brief Class designed to handle datas access will fieldtypes are constructing datas
  215. #
  216. # This class is designed to allow automatic scheduling of construct_data calls.
  217. #
  218. # In theory it's able to detect circular dependencies
  219. # @todo test circular deps detection
  220. # @todo test circulat deps false positiv
  221. class DatasConstructor(object):
  222. ## @brief Init a DatasConstructor
  223. # @param lec LeCrud : @ref LeObject child class
  224. # @param datas dict : dict with field name as key and field values as value
  225. # @param fields_handler dict : dict with field name as key and data handler instance as value
  226. def __init__(self, leobject, datas, fields_handler):
  227. ## Stores concerned class
  228. self._leobject = leobject
  229. ## Stores datas and constructed datas
  230. self._datas = copy.copy(datas)
  231. ## Stores fieldtypes
  232. self._fields_handler = fields_handler
  233. ## Stores list of fieldname for constructed datas
  234. self._constructed = []
  235. ## Stores construct calls list
  236. self._construct_calls = []
  237. ## @brief Implements the dict.keys() method on instance
  238. def keys(self):
  239. return self._datas.keys()
  240. ## @brief Allows to access the instance like a dict
  241. def __getitem__(self, fname):
  242. if fname not in self._constructed:
  243. if fname in self._construct_calls:
  244. raise RuntimeError('Probably circular dependencies in fieldtypes')
  245. cur_value = self._datas[fname] if fname in self._datas else None
  246. self._datas[fname] = self._fields_handler[fname].construct_data(self._leobject, fname, self, cur_value)
  247. self._constructed.append(fname)
  248. return self._datas[fname]
  249. ## @brief Allows to set instance values like a dict
  250. # @warning Should not append in theory
  251. def __setitem__(self, fname, value):
  252. self._datas[fname] = value
  253. warnings.warn("Setting value of an DatasConstructor instance")