Browse Source

Add a new Makefile rule for instance emgraph that will generate an image representing the instance editorial model

Yann Weber 9 years ago
parent
commit
1aa64505eb
3 changed files with 45 additions and 1 deletions
  1. 4
    1
      Lodel/settings_format.py
  2. 38
    0
      install/utils.py
  3. 3
    0
      settings.py

+ 4
- 1
Lodel/settings_format.py View File

@@ -12,4 +12,7 @@ MANDATORY = [
12 12
     'migration_options',
13 13
 ]
14 14
 
15
-ALLOWED = []
15
+ALLOWED = [
16
+    'em_graph_output',
17
+    'em_graph_format',
18
+]

+ 38
- 0
install/utils.py View File

@@ -1,6 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 
3 3
 from loader import *
4
+import warnings
4 5
 
5 6
 def refreshdyn():
6 7
     import sys
@@ -24,4 +25,41 @@ def db_init():
24 25
     em = Model(EmBackendJson(Settings.em_file))
25 26
     em.migrate_handler(mh)
26 27
 
28
+def em_graph(output_file = None, image_format = None):
29
+    from EditorialModel.model import Model
30
+    from EditorialModel.backend.json_backend import EmBackendJson
31
+    from EditorialModel.backend.graphviz import EmBackendGraphviz
32
+    import subprocess
33
+    
34
+    if image_format is None:
35
+        if hasattr(Settings, 'em_graph_format'):
36
+            image_format = Settings.em_graph_format
37
+        else:
38
+            image_format = 'png'
39
+    if output_file is None:
40
+        if hasattr(Settings, 'em_graph_output'):
41
+            output_file = Settings.em_graph_output
42
+        else:
43
+            output_file = '/tmp/em_%s_graph.dot'
44
+    image_format = image_format.lower()
45
+    try:
46
+        output_file = output_file%Settings.sitename
47
+    except TypeError:
48
+        warnings.warn("Bad filename for em_graph output. The filename should be in the form '/foo/bar/file_%s_name.png")
49
+        pass
50
+
51
+    dot_file = output_file+".dot"
52
+    
53
+    graphviz_bckend = EmBackendGraphviz(dot_file)
54
+    edmod = Model(EmBackendJson(Settings.em_file))
55
+    graphviz_bckend.save(edmod)
56
+    dot_cmd = [
57
+        "dot",
58
+        "-T%s"%image_format,
59
+        dot_file
60
+    ]
61
+    with open(output_file, "w+") as outfp:
62
+        subprocess.check_call(dot_cmd, stdout=outfp)
63
+    os.unlink(dot_file)
64
+    print("Output image written in file : '%s'" % output_file)
27 65
 

+ 3
- 0
settings.py View File

@@ -19,3 +19,6 @@ migration_options = {
19 19
     'foreign_keys': True,
20 20
     'drop_if_exists': False,
21 21
 }
22
+
23
+em_graph_format = 'png'
24
+em_graph_output = '/tmp/em_%s_graph.png'

Loading…
Cancel
Save