Selaa lähdekoodia

dyncode and init_datasources in Commands

prieto 7 vuotta sitten
vanhempi
commit
c12f2e0b0b

+ 9
- 1
management/commands/generate_dyncode.py Näytä tiedosto

@@ -7,13 +7,21 @@ class GenerateDyncode(Command):
7 7
     option_list = (
8 8
         Option('--model', '-m', dest='model_file'),
9 9
         Option('--translator', '-t', dest='translator'),
10
+        Option('--filename', '-f', dest='filename')
10 11
     )
11 12
     
12 13
     ## @brief Generates the dynamic code corresponding to an editorial model
13 14
     # @param model_file str : file path corresponding to a given editorial model
14 15
     # @param translator
15 16
     # @return module
16
-    def run(self, model_file, translator):
17
+    def run(self, model_file, translator, filename):
17 18
         dyncode = generate_dyncode(model_file, translator)        
18 19
         print("Dyncode successfully generated for the editorial model from the file %s" % model_file)
20
+        
21
+        if filename is not None:
22
+            with open(filename, 'w+') as out_fd:
23
+                out_fd.write(dyncode)
24
+                out_fd.close()
25
+                print("Dynamic leapi code written in %s", filename)
26
+                
19 27
         return dyncode

+ 17
- 0
management/commands/init_datasources.py Näytä tiedosto

@@ -0,0 +1,17 @@
1
+from flask_script import Command, Option
2
+
3
+from management.utils import init_all_datasources
4
+
5
+class InitDataSources(Command):
6
+
7
+    option_list = (
8
+        Option('--dyncode', '-m', dest='dyncode'),
9
+    )
10
+    
11
+    ## @brief Generates the dynamic code corresponding to an editorial model
12
+    # @param model_file str : file path corresponding to a given editorial model
13
+    # @param translator
14
+    # @return module
15
+    def run(self, dyncode):
16
+        init_all_datasources(dyncode)       
17
+        print("Datasources successfully started")

+ 55
- 0
management/utils.py Näytä tiedosto

@@ -16,3 +16,58 @@ def generate_dyncode(model_file, translator):
16 16
     model = EditorialModel.load(translator, filename=model_file)
17 17
     dyncode = lefactory.dyncode_from_em(model)
18 18
     return dyncode
19
+
20
+def init_all_datasources(dyncode):
21
+    from lodel.plugin.datasource_plugin import DatasourcePlugin
22
+    from lodel import logger
23
+
24
+    ds_cls = dict() # EmClass indexed by rw_datasource
25
+    for cls in dyncode.dynclasses:
26
+        ds = cls._datasource_name
27
+        if ds not in ds_cls:
28
+            ds_cls[ds] = [cls]
29
+        else:
30
+            ds_cls[ds].append(cls)
31
+    
32
+    for ds_name in ds_cls:
33
+        mh = DatasourcePlugin.init_migration_handler(ds_name)
34
+        #Retrieve plugin_name & ds_identifier for logging purpose
35
+        plugin_name, ds_identifier = DatasourcePlugin.plugin_name(
36
+            ds_name, False)
37
+        try:
38
+            mh.init_db(ds_cls[ds_name])
39
+        except Exception as e:
40
+            msg = "Migration failed for datasource %s(%s.%s) when running \
41
+init_db method: %s"
42
+            msg %= (ds_name, plugin_name, ds_identifier, e)
43
+        logger.info("Database initialisation done for %s(%s.%s)" % (
44
+            ds_name, plugin_name, ds_identifier))
45
+
46
+def list_registered_hooks():
47
+    from lodel.plugin.hooks import LodelHook
48
+    from lodel.plugin.plugins import Plugin
49
+    
50
+    Plugin.load_all()
51
+    hlist = LodelHook.hook_list()
52
+    print("Registered hooks are : ")
53
+    for name in sorted(hlist.keys()):
54
+        print("\t- %s is registered by : " % name)
55
+        for reg_hook in hlist[name]:
56
+            hook, priority = reg_hook
57
+            msg = "\t\t- {modname}.{funname} with priority : {priority}"
58
+            msg = msg.format(
59
+                modname = hook.__module__,
60
+                funname = hook.__name__,
61
+                priority = priority)
62
+            print(msg)
63
+        print("\n")
64
+
65
+##@brief update plugin's discover cache
66
+def update_plugin_discover_cache(path_list = None):
67
+    os.environ['LODEL2_NO_SETTINGS_LOAD'] = 'True'
68
+    from lodel.plugin.plugins import Plugin
69
+    res = Plugin.discover(path_list)
70
+    print("Plugin discover result in %s :\n" % res['path_list'])
71
+    for pname, pinfos in res['plugins'].items():
72
+        print("\t- %s %s -> %s" % (
73
+            pname, pinfos['version'], pinfos['path']))

Loading…
Peruuta
Tallenna