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.

component.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #-*- coding: utf-8 -*-
  2. import itertools
  3. from lodel.utils.mlstring import MlString
  4. from lodel.editorial_model.exceptions import *
  5. ## @brief Abstract class to represent editorial model components
  6. # @see EmClass EmField
  7. class EmComponent(object):
  8. ## @brief Instanciate an EmComponent
  9. # @param uid str : uniq identifier
  10. # @param display_name MlString|str|dict : component display_name
  11. # @param help_text MlString|str|dict : help_text
  12. def __init__(self, uid, display_name = None, help_text = None, group = None):
  13. if self.__class__ == EmComponent:
  14. raise NotImplementedError('EmComponent is an abstract class')
  15. self.uid = uid
  16. self.display_name = None if display_name is None else MlString(display_name)
  17. self.help_text = None if help_text is None else MlString(help_text)
  18. self.group = group
  19. def __str__(self):
  20. if self.display_name is None:
  21. return str(self.uid)
  22. return str(self.display_name)
  23. ## @brief Handles editorial model objects classes
  24. #
  25. # @note The inheritance system allow child classes to overwrite parents EmField. But it's maybe not a good idea
  26. class EmClass(EmComponent):
  27. ## @brief Instanciate a new EmClass
  28. # @param uid str : uniq identifier
  29. # @param display_name MlString|str|dict : component display_name
  30. # @param abstract bool : set the class as asbtract if True
  31. # @param parents list: parent EmClass list or uid list
  32. # @param help_text MlString|str|dict : help_text
  33. def __init__(self, uid, display_name = None, help_text = None, abstract = False, parents = None, group = None):
  34. super().__init__(uid, display_name, help_text, group)
  35. self.abstract = bool(abstract)
  36. if parents is not None:
  37. if not isinstance(parents, list):
  38. parents = [parents]
  39. for parent in parents:
  40. if not isinstance(parent, EmClass):
  41. raise ValueError("<class EmClass> expected in parents list, but %s found" % type(parent))
  42. self.parents = parents
  43. ## @brief Stores EmFields instances indexed by field uid
  44. self.__fields = dict()
  45. ## @brief Property that represent a dict of all fields (the EmField defined in this class and all its parents)
  46. @property
  47. def __all_fields(self):
  48. res = dict()
  49. for pfields in [ p.__all_fields for p in self.parents]:
  50. res.update(pfields)
  51. res.update(self.__fields)
  52. return res
  53. ## @brief EmField getter
  54. # @param uid None | str : If None returns an iterator on EmField instances else return an EmField instance
  55. # @param no_parents bool : If True returns only fields defined is this class and not the one defined in parents classes
  56. # @return An iterator on EmFields instances (if uid is None) else return an EmField instance
  57. def fields(self, uid = None, no_parents = False):
  58. fields = self.__fields if no_parents else self.__all_fields
  59. try:
  60. return iter(fields.values()) if uid is None else fields[uid]
  61. except KeyError:
  62. raise EditorialModelError("No such EmField '%s'" % uid)
  63. ## @brief Add a field to the EmClass
  64. # @param emfield EmField : an EmField instance
  65. # @warning do not add an EmField allready in another class !
  66. # @throw EditorialModelException if an EmField with same uid allready in this EmClass (overwritting allowed from parents)
  67. def add_field(self, emfield):
  68. if emfield.uid in self.__fields:
  69. raise EditorialModelException("Duplicated uid '%s' for EmField in this class ( %s )" % (emfield.uid, self))
  70. self.__fields[emfield.uid] = emfield
  71. ## @brief Create a new EmField and add it to the EmClass
  72. # @param uid str : the EmField uniq id
  73. # @param **field_kwargs : EmField constructor parameters ( see @ref EmField.__init__() )
  74. def new_field(self, uid, **field_kwargs):
  75. return self.add_field(EmField(uid, **kwargs))
  76. ## @brief Handles editorial model classes fields
  77. class EmField(EmComponent):
  78. ## @brief Instanciate a new EmField
  79. # @param uid str : uniq identifier
  80. # @param display_name MlString|str|dict : field display_name
  81. # @param data_handler class|str : A DataHandler class or display_name
  82. # @param help_text MlString|str|dict : help text
  83. # @param group EmGroup :
  84. # @param **handler_kwargs : data handler arguments
  85. def __init__(self, uid, data_handler, display_name = None, help_text = None, group = None, **handler_kwargs):
  86. super().__init__(uid, display_name, help_text, group)
  87. self.data_handler = data_handler
  88. self.data_handler_options = data_handler_options