Browse Source

Adding a new feature to LeFactory

Now lefactory inject python code from lodel/leapi/lefactory_common.py in generated code
Yann Weber 8 years ago
parent
commit
693bfd7a45
2 changed files with 56 additions and 14 deletions
  1. 19
    14
      lodel/leapi/lefactory.py
  2. 37
    0
      lodel/leapi/lefactory_common.py

+ 19
- 14
lodel/leapi/lefactory.py View File

@@ -1,5 +1,6 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
+import os, os.path
3 4
 import functools
4 5
 from lodel.editorial_model.components import *
5 6
 from lodel.leapi.leobject import LeObject
@@ -28,29 +29,33 @@ from lodel.plugin.hooks import LodelHook
28 29
 {imports}
29 30
 {classes}
30 31
 {bootstrap_instr}
32
+#List of dynamically generated classes
31 33
 dynclasses = {class_list}
32
-{init_hook}
34
+#Dict of dynamically generated classes indexed by name
35
+dynclasses_dict = {class_dict}
36
+{common_code}
33 37
 """.format(
34 38
             imports = imports,
35 39
             classes = cls_code,
36 40
             bootstrap_instr = bootstrap_instr,
37 41
             class_list = '[' + (', '.join([cls for cls in class_list]))+']',
38
-            init_hook = datasource_init_hook(),
42
+            class_dict = '{' + (', '.join([ "'%s': %s" % (cls, cls)
43
+                for cls in class_list]))+'}',
44
+            common_code = common_code(),
39 45
     )
40 46
     return res_code
41 47
 
42
-##@brief Produce the source code of the LodelHook that initialize datasources
43
-#in dyncode
44
-#@return str
45
-def datasource_init_hook():
46
-    return """
47
-@LodelHook("lodel2_plugins_loaded")
48
-def lodel2_dyncode_datasources_init(self, caller, payload):
49
-    for cls in dynclasses:
50
-        cls._init_datasources()
51
-    from lodel.plugin.hooks import LodelHook
52
-    LodelHook.call_hook("lodel2_dyncode_loaded", __name__, dynclasses)
53
-"""
48
+##@brief Return the content of lodel.leapi.lefactory_common
49
+def common_code():
50
+    res = ""
51
+    fname = os.path.dirname(__file__)
52
+    fname = os.path.join(fname, 'lefactory_common.py')
53
+    with open(fname, 'r') as cfp:
54
+        for line in cfp:
55
+            if not line.startswith('#-'):
56
+                res += line
57
+    return res
58
+    
54 59
 
55 60
 ##@brief return A list of EmClass sorted by dependencies
56 61
 #

+ 37
- 0
lodel/leapi/lefactory_common.py View File

@@ -0,0 +1,37 @@
1
+#-
2
+#-  THE CONTENT OF THIS FILE IS DESIGNED TO BE INCLUDED IN DYNAMICALLY 
3
+#-  GENERATED CODE
4
+#-
5
+#- All lines that begins with #- will be deleted from dynamically generated
6
+#- code...
7
+
8
+
9
+##@brief Return a dynamically generated class given it's name
10
+#@param name str : The dynamic class name
11
+#@return False or a child class of LeObject
12
+def name2class(name):
13
+    if name not in dynclasses_dict:
14
+        return False
15
+    return dynclasses_dict[name]
16
+
17
+
18
+##@brief Return a dynamically generated class given it's name
19
+#@note Case insensitive version of name2class
20
+#@param name str
21
+#@retrun False or a child class of LeObject
22
+def lowername2class(name):
23
+    name = name.lower()
24
+    new_dict = {k.lower():v for k,v in dynclasses_dict.items()}
25
+    if name not in new_dict:
26
+        return False
27
+    return new_dict[name]
28
+
29
+
30
+##@brief Trigger dynclasses datasources initialisation
31
+@LodelHook("lodel2_plugins_loaded")
32
+def lodel2_dyncode_datasources_init(self, caller, payload):
33
+    for cls in dynclasses:
34
+        cls._init_datasources()
35
+    from lodel.plugin.hooks import LodelHook
36
+    LodelHook.call_hook("lodel2_dyncode_loaded", __name__, dynclasses)
37
+

Loading…
Cancel
Save