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.

references.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. from lodel.context import LodelContext
  3. LodelContext.expose_modules(globals(), {
  4. 'lodel.leapi.datahandlers.base_classes': ['Reference', 'MultipleRef',
  5. 'SingleRef'],
  6. 'lodel.logger': 'logger',
  7. 'lodel.exceptions': ['LodelException', 'LodelExceptions',
  8. 'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
  9. class Link(SingleRef):
  10. pass
  11. ##@brief Child class of MultipleRef where references are represented in the form of a python list
  12. class List(MultipleRef):
  13. ##@brief instanciates a list reference
  14. # @param max_length int
  15. # @param kwargs
  16. # - allowed_classes list | None : list of allowed em classes if None no restriction
  17. # - internal bool
  18. def __init__(self, max_length = None, **kwargs):
  19. super().__init__(**kwargs)
  20. @classmethod
  21. def empty(cls):
  22. return list()
  23. ##@brief Check and cast value in appropriate type
  24. #@param value *
  25. #@throw FieldValidationError if value is unappropriate or can not be cast
  26. #@return value
  27. def _check_data_value(self, value):
  28. value = super()._check_data_value(value)
  29. try:
  30. return list(value)
  31. except Exception as e:
  32. raise FieldValidationError("Given iterable is not castable in \
  33. a list : %s" % e)
  34. return value
  35. ##@brief Child class of MultipleRef where references are represented in the form of a python set
  36. class Set(MultipleRef):
  37. ##@brief instanciates a set reference
  38. # @param kwargs : named arguments
  39. # - allowed_classes list | None : list of allowed em classes if None no restriction
  40. # - internal bool : if False, the field is not internal
  41. def __init__(self, **kwargs):
  42. super().__init__(**kwargs)
  43. @classmethod
  44. def empty(cls):
  45. return set()
  46. ##@brief Check and cast value in appropriate type
  47. #@param value *
  48. #@throw FieldValidationError if value is unappropriate or can not be cast
  49. #@return value
  50. def _check_data_value(self, value):
  51. value = super()._check_data_value(value)
  52. try:
  53. return set(value)
  54. except Exception as e:
  55. raise FieldValidationError("Given iterable is not castable in \
  56. a set : %s" % e)
  57. ##@brief Child class of MultipleRef where references are represented in the form of a python dict
  58. class Map(MultipleRef):
  59. ##@brief instanciates a dict reference
  60. # @param kwargs : named arguments
  61. # - allowed_classes list | None : list of allowed em classes if None no restriction
  62. # - internal bool : if False, the field is not internal
  63. def __init__(self, **kwargs):
  64. super().__init__(**kwargs)
  65. @classmethod
  66. def empty(cls):
  67. return dict()
  68. ##@brief Check and cast value in appropriate type
  69. #@param value *
  70. #@throw FieldValidationError if value is unappropriate or can not be cast
  71. #@return value
  72. def _check_data_value(self, value):
  73. value = super()._check_data_value(value)
  74. if not isinstance(value, dict):
  75. raise FieldValidationError("Values for dict fields should be dict")
  76. return value
  77. ##@brief This Reference class is designed to handler hierarchy with some constraint
  78. class Hierarch(MultipleRef):
  79. directly_editable = False
  80. ##@brief Instanciate a data handler handling hierarchical relation with constraints
  81. # @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
  82. # @param max_depth int | None : limit of depth
  83. # @param max_childs int | Nine : maximum number of childs by nodes
  84. def __init__(self, back_reference, max_depth = None, max_childs = None, **kwargs):
  85. super().__init__( back_reference = back_reference,
  86. max_depth = max_depth,
  87. max_childs = max_childs,
  88. **kwargs)
  89. @classmethod
  90. def empty(cls):
  91. return tuple()
  92. ##@brief Check and cast value in appropriate type
  93. #@param value *
  94. #@throw FieldValidationError if value is unappropriate or can not be cast
  95. #@return value
  96. def _check_data_value(self, value):
  97. value = super()._check_data_value(value)
  98. if not (isinstance(value, list) or isinstance(value, str)):
  99. raise FieldValidationError(
  100. "List or string expected for a set field")
  101. return value