Browse Source

Introduction of LeFactory and its generatePython method

LeFactory.generatePython() is a method that generate LeObject code and python classes for each EmClass and EmType in an EditorialModel
Yann Weber 9 years ago
parent
commit
f27bb26f9a
3 changed files with 102 additions and 4 deletions
  1. 6
    1
      EditorialModel/fields.py
  2. 11
    3
      EditorialModel/fieldtypes/generic.py
  3. 85
    0
      leobject/lefactory.py

+ 6
- 1
EditorialModel/fields.py View File

@@ -47,7 +47,6 @@ class EmField(EmComponent):
47 47
         if not fieldtype_instance.check(default):
48 48
             raise TypeError("Default value ('%s') is not valid given the fieldtype '%s'"%(default, fieldtype))
49 49
 
50
-
51 50
         self.nullable = nullable
52 51
         self.default = default
53 52
         self.uniq = uniq
@@ -62,6 +61,12 @@ class EmField(EmComponent):
62 61
     def fieldtypes_list():
63 62
         return [f for f in EditorialModel.fieldtypes.__all__ if f != '__init__']
64 63
 
64
+    ## @brief Get the fieldtype instance
65
+    # @return a fieldtype instance
66
+    def fieldtype_instance(self):
67
+        return self._fieldtype_cls(self._fieldtype_args)
68
+        
69
+
65 70
     ## @brief Return the list of relation fields for a rel_to_type
66 71
     # @return None if the field is not a rel_to_type else return a list of EmField
67 72
     def rel_to_type_fields(self):

+ 11
- 3
EditorialModel/fieldtypes/generic.py View File

@@ -51,13 +51,21 @@ class GenericFieldType(object):
51 51
         pass
52 52
 
53 53
     ## @brief Given a fieldtype name return the associated python class
54
+    # @param fieldtype_name str : A fieldtype name
54 55
     # @return An GenericFieldType derivated class
55 56
     @staticmethod
56
-    def from_name(name):
57
-        module_name = 'EditorialModel.fieldtypes.%s'%(name)
58
-        mod = importlib.import_module(module_name)
57
+    def from_name(fieldtype_name):
58
+        mod = importlib.import_module(GenericFieldType.module_name(fieldtype_name))
59 59
         return mod.EmFieldType
60 60
 
61
+    ## @brief Get a module name given a fieldtype name
62
+    # @param fieldtype_name str : A fieldtype name
63
+    # @return a string representing a python module name
64
+    @staticmethod
65
+    def module_name(fieldtype_name):
66
+        return 'EditorialModel.fieldtypes.%s'%(fieldtype_name)
67
+        
68
+
61 69
     
62 70
     ## @brief Transform a value into a valid python representation according to the fieldtype
63 71
     # @param value ? : The value to cast

+ 85
- 0
leobject/lefactory.py View File

@@ -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
+

Loading…
Cancel
Save