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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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',
  9. 'FieldValidationError']})
  10. class Link(SingleRef):
  11. pass
  12. ## @brief Child class of MultipleRef where references are represented in the form of a python list
  13. class List(MultipleRef):
  14. ## @brief instanciates a list reference
  15. # @param max_length int
  16. # @param kwargs
  17. # - allowed_classes list | None : list of allowed em classes if None no restriction
  18. # - internal bool
  19. def __init__(self, max_length=None, **kwargs):
  20. super().__init__(**kwargs)
  21. @classmethod
  22. def empty(cls):
  23. return list()
  24. ## @brief Check and cast value in appropriate type
  25. # @param value *
  26. # @throw FieldValidationError if value is unappropriate or can not be cast
  27. # @return value
  28. def _check_data_value(self, value):
  29. value = super()._check_data_value(value)
  30. try:
  31. return list(value)
  32. except Exception as e:
  33. raise FieldValidationError("Given iterable is not castable in \
  34. a list : %s" % e)
  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