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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #-*- coding: utf-8 -*-
  2. import EditorialModel
  3. from EditorialModel.model import Model
  4. from EditorialModel.fieldtypes.generic import GenericFieldType
  5. ## @brief The factory that will return LeObject childs instances
  6. #
  7. # The name is not good but i've no other ideas for the moment
  8. class LeFactory(object):
  9. def __init__(self):raise NotImplementedError("Not designed (yet?) to be implemented")
  10. ## @brief Return a call to a FieldType constructor given an EmField
  11. # @param emfield EmField : An EmField
  12. # @return a string representing the python code to instanciate a EmFieldType
  13. @staticmethod
  14. def fieldtype_construct_from_field(emfield):
  15. return '%s.EmFieldType(**%s)'%(
  16. GenericFieldType.module_name(emfield.fieldtype),
  17. emfield._fieldtype_args.__repr__(),
  18. )
  19. ## @brief Generate python code containing the LeObject API
  20. # @param model_args dict : Dict of Model __init__ method arguments
  21. # @param datasource_args dict : Dict of datasource __init__ method arguments
  22. # @return A string representing python code
  23. @staticmethod
  24. def generate_python(backend_cls, backend_args, datasource_cls, datasource_args):
  25. result = ""
  26. result += "#-*- coding: utf-8 -*-\n"
  27. #Putting import directives in result
  28. result += "\n\n\
  29. from EditorialModel.model import Model\n\
  30. from leobject.leobject import _LeObject\n\
  31. from leobject.leclass import LeClass\n\
  32. from leobject.letype import LeType\n\
  33. import EditorialModel.fieldtypes\n\
  34. "
  35. result += "\n\
  36. import %s\n\
  37. import %s\n\
  38. "%(backend_cls.__module__, datasource_cls.__module__)
  39. #Generating the code for LeObject class
  40. backend_constructor = '%s.%s(**%s)'%(backend_cls.__module__, backend_cls.__name__, backend_args.__repr__())
  41. result += "\n\
  42. class LeObject(_LeObject):\n\
  43. _model = Model(backend=%s)\n\
  44. _datasource = %s(**%s)\n\
  45. \n\
  46. "%(backend_constructor, datasource_cls.__name__, datasource_args.__repr__())
  47. model = Model(backend=backend_cls(**backend_args))
  48. for emclass in model.components(EditorialModel.classes.EmClass):
  49. cls_fields = dict()
  50. for field in emclass.fields():
  51. cls_fields[field.name] = LeFactory.fieldtype_construct_from_field(field)
  52. cls_fieldgroup = dict()
  53. for fieldgroup in emclass.fieldgroups():
  54. cls_fieldgroup[fieldgroup.name] = list()
  55. for field in fieldgroup.fields():
  56. cls_fieldgroup[fieldgroup.name].append(field.name)
  57. result += "\n\
  58. class %s(LeObject, LeClass):\n\
  59. _fieldtypes = %s\n\
  60. _fieldgroups = %s\n\
  61. \n\
  62. "%(emclass.name.title(), cls_fields.__repr__(), cls_fieldgroup.__repr__())
  63. for emtype in model.components(EditorialModel.types.EmType):
  64. type_fields = list()
  65. for field in emtype.fields():
  66. type_fields.append(field.name)
  67. result += "\n\
  68. class %s(%s,LeType):\n\
  69. _fields = %s\n\
  70. _leClass = %s\n\
  71. \n\
  72. "%(emtype.name.title(), emtype.em_class.name.title(), type_fields.__repr__(), emtype.em_class.name.title())
  73. return result