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