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.

lefactory.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #-*- coding: utf-8 -*-
  2. import functools
  3. from lodel.editorial_model.components import *
  4. from lodel.leapi.leobject import LeObject
  5. from lodel.leapi.datahandlers.base_classes import DataHandler
  6. ## @brief Generate python module code from a given model
  7. # @param model lodel.editorial_model.model.EditorialModel
  8. def dyncode_from_em(model):
  9. cls_code, modules, bootstrap_instr = generate_classes(model)
  10. imports = """from lodel.leapi.leobject import LeObject
  11. from lodel.leapi.datahandlers.base_classes import DataField
  12. """
  13. for module in modules:
  14. imports += "import %s\n" % module
  15. res_code = """#-*- coding: utf-8 -*-
  16. {imports}
  17. {classes}
  18. {bootstrap_instr}
  19. del(LeObject._set__fields)
  20. """.format(
  21. imports = imports,
  22. classes = cls_code,
  23. bootstrap_instr = bootstrap_instr,
  24. )
  25. return res_code
  26. ## @brief return A list of EmClass sorted by dependencies
  27. #
  28. # The first elts in the list depends on nothing, etc.
  29. # @return a list of EmClass instances
  30. def emclass_sorted_by_deps(emclass_list):
  31. def emclass_deps_cmp(cls_a, cls_b):
  32. if len(cls_a.parents) + len(cls_b.parents) == 0:
  33. return 0
  34. elif len(cls_a.parents) == 0:
  35. return -1
  36. elif len(cls_b.parents) == 0:
  37. return 1
  38. if cls_a in cls_b.parents_recc:
  39. return -1
  40. elif cls_b in cls_a.parents_recc:
  41. return 1
  42. else:
  43. return 0
  44. return sorted(emclass_list, key = functools.cmp_to_key(emclass_deps_cmp))
  45. ## @brief Given an EmField returns the data_handler constructor suitable for dynamic code
  46. def data_handler_constructor(emfield):
  47. #dh_module_name = DataHandler.module_name(emfield.data_handler_name)+'.DataHandler'
  48. get_handler_class_instr = 'DataField.from_name(%s)' % repr(emfield.data_handler_name)
  49. options = []
  50. return ('%s(**{' % get_handler_class_instr)+(', '.join([repr(name)+': '+forge_optval(val) for name, val in emfield.data_handler_options.items()])) + '})'
  51. def forge_optval(optval):
  52. if isinstance(optval, dict):
  53. return '{' + (', '.join( [ '%s: %s' % (repr(name), forge_optval(val)) for name, val in optval.items()])) + '}'
  54. if isinstance(optval, (set, list, tuple)):
  55. return '[' + (', '.join([forge_optval(val) for val in optval])) + ']'
  56. if isinstance(optval, EmField):
  57. return "{leobject}.data_handler({fieldname})".format(
  58. leobject = LeObject.name2objname(optval._emclass.uid),
  59. fieldname = repr(optval.uid)
  60. )
  61. elif isinstance(optval, EmClass):
  62. return LeObject.name2objname(optval.uid)
  63. else:
  64. return repr(optval)
  65. ## @brief Generate dyncode from an EmClass
  66. # @param model EditorialModel :
  67. # @param emclass EmClass : EmClass instance
  68. # @return a tuple with emclass python code, a set containing modules name to import, and a list of python instruction to bootstrap dynamic code, in this order
  69. def generate_classes(model):
  70. res = ""
  71. imports = list()
  72. bootstrap = ""
  73. # Generating field list for LeObjects generated from EmClass
  74. for em_class in [ cls for cls in emclass_sorted_by_deps(model.classes()) if not cls.pure_abstract ]:
  75. uid = list() # List of fieldnames that are part of the EmClass primary key
  76. parents = list() # List of parents EmClass
  77. # Determine pk
  78. for field in em_class.fields():
  79. if field.data_handler_instance.is_primary_key():
  80. uid.append(field.uid)
  81. # Determine parent for inheritance
  82. if len(em_class.parents) > 0:
  83. for parent in em_class.parents:
  84. parents.append(LeObject.name2objname(parent.uid))
  85. else:
  86. parents.append('LeObject')
  87. # Dynamic code generation for LeObject childs classes
  88. em_cls_code = """
  89. class {clsname}({parents}):
  90. __abstract = {abstract}
  91. __fields = None
  92. __uid = {uid_list}
  93. """.format(
  94. clsname = LeObject.name2objname(em_class.uid),
  95. parents = ', '.join(parents),
  96. abstract = 'True' if em_class.abstract else 'False',
  97. uid_list = repr(uid),
  98. )
  99. res += em_cls_code
  100. # Dyncode bootstrap instructions
  101. bootstrap += """{classname}._set__fields({fields})
  102. #del({classname}._set__fields)
  103. """.format(
  104. classname = LeObject.name2objname(em_class.uid),
  105. fields = '{' + (', '.join(['\n\t%s: %s' % (repr(emfield.uid),data_handler_constructor(emfield)) for emfield in em_class.fields()])) + '}',
  106. )
  107. return res, set(imports), bootstrap