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 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef, SingleRef, FieldValidationError
  3. class Link(SingleRef):
  4. pass
  5. ##@brief Child class of MultipleRef where references are represented in the form of a python list
  6. class List(MultipleRef):
  7. ##@brief instanciates a list reference
  8. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  9. # @param internal bool
  10. # @param kwargs
  11. def __init__(self, max_length = None, **kwargs):
  12. super().__init__(**kwargs)
  13. ##@brief Check value
  14. # @param value *
  15. # @return tuple(value, exception)
  16. def _check_data_value(self, value):
  17. val, expt = super()._check_data_value()
  18. if not isinstance(expt, Exception):
  19. val = list(val)
  20. val, expt = super()._check_data_value(value.values())
  21. return val, expt
  22. ##@brief Child class of MultipleRef where references are represented in the form of a python set
  23. class Set(MultipleRef):
  24. ##@brief instanciates a set reference
  25. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  26. # @param internal bool : if False, the field is not internal
  27. # @param kwargs : Other named arguments
  28. def __init__(self, **kwargs):
  29. super().__init__(**kwargs)
  30. ##@brief Check value
  31. # @param value *
  32. # @return tuple(value, exception)
  33. def _check_data_value(self, value):
  34. val, expt = super()._check_data_value()
  35. if not isinstance(expt, Exception):
  36. val = set(val)
  37. val, expt = super()._check_data_value(value.values())
  38. return val, expt
  39. ##@brief Child class of MultipleRef where references are represented in the form of a python dict
  40. class Map(MultipleRef):
  41. ##@brief instanciates a dict reference
  42. # @param allowed_classes list | None : list of allowed em classes if None no restriction
  43. # @param internal bool : if False, the field is not internal
  44. # @param kwargs : Other named arguments
  45. def __init__(self, **kwargs):
  46. super().__init__(**kwargs)
  47. ##@brief Check value
  48. # @param value *
  49. # @return tuple(value, exception)
  50. def _check_data_value(self, value):
  51. if not isinstance(value, dict):
  52. return None, FieldValidationError("Values for dict fields should be dict")
  53. val, expt = super()._check_data_value(value.values())
  54. return (
  55. None if isinstance(expt, Exception) else value,
  56. expt)
  57. ##@brief This Reference class is designed to handler hierarchy with some constraint
  58. class Hierarch(MultipleRef):
  59. ##@brief Instanciate a data handler handling hierarchical relation with constraints
  60. # @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
  61. # @param max_depth int | None : limit of depth
  62. # @param max_childs int | Nine : maximum number of childs by nodes
  63. def __init__(self, back_reference, max_depth = None, max_childs = None, **kwargs):
  64. super().__init__(back_reference = back_reference, max_depth = max_depth, )
  65. def _check_data_value(self, value):
  66. value, expt = super()._check_data_value(value)
  67. if isinstance(expt, Exception):
  68. return None, expt
  69. # determine depth with datasource