1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2025-11-02 04:20:55 +01:00

Code cleaning

This commit is contained in:
Roland Haroutiounian 2017-02-10 09:48:18 +01:00 committed by prieto
commit 822ad1bea2

View file

@ -50,11 +50,12 @@ class DataHandler(MlNamedObject):
_construct_datas_deps = []
directly_editable = True
## @brief constructor
#
# @param internal False | str : define whether or not a field is internal
# @param immutable bool : indicates if the fieldtype has to be defined in child classes of LeObject or if it is
# designed globally and immutable
# @param **args
# @param immutable bool : indicates if the fieldtype has to be defined in child classes of
# LeObject or if it is designed globally and immutable
# @throw NotImplementedError if it is instanciated directly
def __init__(self, **kwargs):
if self.__class__ == DataHandler:
@ -69,22 +70,24 @@ class DataHandler(MlNamedObject):
self.default, error = self.check_data_value(kwargs['default'])
if error:
raise error
del(kwargs['default'])
del kwargs['default']
for argname, argval in kwargs.items():
setattr(self, argname, argval)
self.check_options()
super().__init__(self.display_name, self.help_text)
## @brief Sets properly casted and checked options for the datahandler
# @raises LodelDataHandlerNotAllowedOptionException when a passed option is not in the option specifications of the datahandler
# @raises LodelDataHandlerNotAllowedOptionException when a passed option is not in the option
# specifications of the datahandler
def check_options(self):
for option_name, option_datas in self.options_spec.items():
if option_name in self.options_values:
# There is a configured option, we check its value
try:
self.options_values[option_name] = option_datas[1].check_value(self.options_values[option_name])
self.options_values[option_name] = option_datas[1].check_value(
self.options_values[option_name])
except ValueError:
pass # TODO Deal with the case where the value used for an option has not been validated
pass # TODO Deal with the case where the value used for an option is invalid
else:
# This option was not configured, we get the default value from the specs
self.options_values[option_name] = option_datas[0]
@ -110,7 +113,7 @@ class DataHandler(MlNamedObject):
def is_internal(self):
return self.internal is not False
##brief check if a value can be nullable
## @brief check if a value can be nullable
# @param value *
# @throw DataNoneValid if value is None and nullable. LodelExceptions if not nullable
# @return value (if not None)
@ -235,7 +238,8 @@ class DataHandler(MlNamedObject):
## @brief given a field type name, returns the associated python class
# @param fieldtype_name str : A field type name (not case sensitive)
# @return DataField child class
# @note To access custom data handlers it can be cool to prefix the handler name by plugin name for example ? (to ensure name unicity)
# @note To access custom data handlers it can be cool to prefix the handler name by plugin
# name for example ? (to ensure name unicity)
@classmethod
def from_name(cls, name):
cls.load_base_handlers()
@ -269,14 +273,13 @@ class DataHandler(MlNamedObject):
class DataField(DataHandler):
pass
## @brief Abstract class for all references
# @ingroup lodel2_datahandlers
#
# References are fields that stores a reference to another
# editorial object
#@todo Construct data implementation : transform the data into a LeObject
#instance
# @todo Construct data implementation : transform the data into a LeObject instance
class Reference(DataHandler):
base_type = "ref"
@ -287,12 +290,15 @@ class Reference(DataHandler):
# @param **kwargs : other arguments
def __init__(self, allowed_classes=None, back_reference=None, internal=False, **kwargs):
self.__allowed_classes = set() if allowed_classes is None else set(allowed_classes)
self.allowed_classes = list() if allowed_classes is None else allowed_classes # For now usefull to jinja 2
# For now usefull to jinja 2
self.allowed_classes = list() if allowed_classes is None else allowed_classes
if back_reference is not None:
if len(back_reference) != 2:
raise ValueError("A tuple (classname, fieldname) expected but got '%s'" % back_reference)
#if not issubclass(lodel.leapi.leobject.LeObject, back_reference[0]) or not isinstance(back_reference[1], str):
# raise TypeError("Back reference was expected to be a tuple(<class LeObject>, str) but got : (%s, %s)" % (back_reference[0], back_reference[1]))
# if not issubclass(lodel.leapi.leobject.LeObject, back_reference[0])
# or not isinstance(back_reference[1], str):
# raise TypeError("Back reference was expected to be a tuple(<class LeObject>, str)
# but got : (%s, %s)" % (back_reference[0], back_reference[1]))
self.__back_reference = back_reference
super().__init__(internal=internal, **kwargs)
@ -415,7 +421,7 @@ referenced object with uid %s" % value)
# @note for the moment split on ',' chars
class MultipleRef(Reference):
##
## @brief Constructor
# @param max_item int | None : indicate the maximum number of item referenced by this field, None mean no limit
def __init__(self, max_item=None, **kwargs):
self.max_item = max_item
@ -473,6 +479,7 @@ class MultipleRef(Reference):
raise LodelDataHandlerConsistencyException("Unable to find \
some referenced objects. Following uids were not found : %s" % ','.join(left))
## @brief Class designed to handle datas access will fieldtypes are constructing datas
# @ingroup lodel2_datahandlers
#
@ -488,15 +495,15 @@ class DatasConstructor(object):
# @param datas dict : dict with field name as key and field values as value
# @param fields_handler dict : dict with field name as key and data handler instance as value
def __init__(self, leobject, datas, fields_handler):
## Stores concerned class
# Stores concerned class
self._leobject = leobject
## Stores datas and constructed datas
# Stores datas and constructed datas
self._datas = copy.copy(datas)
## Stores fieldtypes
# Stores fieldtypes
self._fields_handler = fields_handler
## Stores list of fieldname for constructed datas
# Stores list of fieldname for constructed datas
self._constructed = []
## Stores construct calls list
# Stores construct calls list
self._construct_calls = []
## @brief Implements the dict.keys() method on instance