Explorar el Código

New graphviz backend + some methods added to some components

Yann Weber hace 8 años
padre
commit
4c0b8d3279

+ 126
- 0
EditorialModel/backend/graphviz.py Ver fichero

@@ -0,0 +1,126 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import datetime
4
+from EditorialModel.classtypes import EmClassType
5
+from EditorialModel.fieldgroups import EmFieldGroup
6
+from EditorialModel.types import EmType
7
+from Lodel.utils.mlstring import MlString
8
+
9
+class EmBackendGraphviz(object):
10
+
11
+    ## @brief Constructor
12
+    # @param dot_fname str : The filename where we want to save the dot repr of the EM
13
+    def __init__(self, dot_fname):
14
+        self.edges = ""
15
+        self.dot_fname = dot_fname
16
+        #with open(dot_file, 'w+') as dot_fp:
17
+    
18
+    ## @brief Not implementend
19
+    # @warning Not implemented
20
+    def load(self):
21
+        raise NotImplementedError(self.__class__.__name__+' cannot load an EM')
22
+
23
+    ## @brief Save an EM in a dot file
24
+    # @param em model : The EM to save
25
+    # @warning hardcoded classtype
26
+    def save(self, em):
27
+        self.edges = ""
28
+        with open(self.dot_fname, 'w') as dotfp:
29
+            dotfp.write("digraph G {\n\trankdir = BT\n")
30
+            
31
+            dotfp.write('subgraph cluster_classtype {\nstyle="invis"\n')
32
+            for ct in [ 'entity', 'entry', 'person' ]:
33
+                dotfp.write('\n\nct%s [ label="classtype %s" shape="tripleoctagon" ]\n'%(ct, ct))
34
+            dotfp.write("}\n")
35
+
36
+            
37
+            dotfp.write('subgraph cluster_class {\nstyle="invis"\n')
38
+            for c in em.classes():
39
+
40
+                dotfp.write(self._component_node(c, em))
41
+                cn = c.__class__.__name__
42
+                cid = self._component_id(c)
43
+                self.edges += cid+' -> ct%s [ style="dashed" ]\n'%c.classtype
44
+            dotfp.write("}\n")
45
+
46
+            #dotfp.write('subgraph cluster_fieldgroup {\nstyle="invis"\n')
47
+            for c in em.components(EmFieldGroup):
48
+                dotfp.write(self._component_node(c, em))
49
+                cn = c.__class__.__name__
50
+                cid = self._component_id(c)
51
+                self.edges += cid+' -> '+self._component_id(c.em_class)+' [ style="dashed" ]\n'
52
+            #dotfp.write("}\n")
53
+
54
+
55
+            #dotfp.write('subgraph cluster_type {\nstyle="invis"\n')
56
+            for c in em.components(EmType):
57
+                dotfp.write(self._component_node(c, em))
58
+                cn = c.__class__.__name__
59
+                cid = self._component_id(c)
60
+                self.edges += cid+' -> '+self._component_id(c.em_class)+' [ style="dotted" ]\n'
61
+                for fg in c.fieldgroups():
62
+                    self.edges += cid+' -> '+self._component_id(fg)+' [ style="dashed" ]\n'
63
+                for nat in c.superiors():
64
+                    self.edges += cid+' -> '+self._component_id(c.superiors()[nat])+' [ label="%s" color="green" ]'%nat
65
+            #dotfp.write("}\n")
66
+
67
+            dotfp.write(self.edges)
68
+
69
+            dotfp.write("\n}")
70
+        pass
71
+
72
+    @staticmethod
73
+    def _component_id(c):
74
+        return 'emcomp%d'%c.uid
75
+
76
+    def _component_node(self, c, em):
77
+        #ret = 'emcomp%d '%c.uid
78
+        ret = "\t"+EmBackendGraphviz._component_id(c)
79
+        cn = c.__class__.__name__
80
+        rel_field = ""
81
+        if cn == 'EmClass':
82
+            ret += '[ label="%s", shape="%s" ]'%(c.name, 'doubleoctagon')
83
+        elif cn == 'EmType' or cn == 'EmFieldGroup':
84
+            ret += '[ label="%s %s '%(cn, c.name)
85
+
86
+            cntref = 0
87
+            first = True
88
+            for f in c.fields():
89
+                if ((cn == 'EmType' and f.optional) or (cn == 'EmFieldGroup' and not f.optional)) and f.rel_field_id is None:
90
+                    
91
+                    if not (f.rel_to_type_id is None):
92
+                        rel_node_id = '%s%s'%(EmBackendGraphviz._component_id(c), EmBackendGraphviz._component_id(em.component(f.rel_to_type_id)))
93
+
94
+                        rel_node = '\t%s [ label="rel_to_type'%rel_node_id
95
+
96
+                        if len(f.rel_to_type_fields()) > 0:
97
+                            #rel_node += '| {'
98
+                            first = True
99
+                            for rf in f.rel_to_type_fields():
100
+                                rel_node += ' | '
101
+                                if first:
102
+                                    rel_node += '{ '
103
+                                    first = False
104
+                                rel_node += rf.name
105
+                        rel_node += '}" shape="record" style="dashed"]\n'
106
+
107
+                        rel_field += rel_node
108
+
109
+                        ref_node = EmBackendGraphviz._component_id(em.component(f.rel_to_type_id))
110
+                        self.edges += '%s:f%d -> %s [ color="purple" ]\n'%(EmBackendGraphviz._component_id(c), cntref, rel_node_id)
111
+                        self.edges += '%s -> %s [color="purple"]\n'%(rel_node_id, ref_node)
112
+
113
+                    ret += '|'
114
+                    if first:
115
+                        ret += ' { '
116
+                        first = False
117
+                    if not (f.rel_to_type_id is None):
118
+                        ret += '<f%d> '%cntref
119
+                        cntref += 1
120
+                    ret += f.name
121
+            ret += '}" shape="record" color="%s" ]'%('blue' if cn == 'EmType' else 'red')
122
+        else:
123
+            return ""
124
+        ret +="\n"+rel_field
125
+        return ret
126
+            

+ 1
- 1
EditorialModel/backend/json_backend.py Ver fichero

@@ -69,5 +69,5 @@ class EmBackendJson(object):
69 69
     #
70 70
     # @return bool
71 71
     # @todo à implémenter
72
-    def save(self):
72
+    def save(self, em):
73 73
         return True

+ 4
- 0
EditorialModel/fieldgroups.py Ver fichero

@@ -20,6 +20,10 @@ class EmFieldGroup(EmComponent):
20 20
         self.check_type('class_id', int)
21 21
         super(EmFieldGroup, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
22 22
 
23
+    @property
24
+    def em_class(self):
25
+        return self.model.component(self.class_id)
26
+
23 27
     ## Check if the EmFieldGroup is valid
24 28
     # @throw EmComponentCheckError if fails
25 29
     def check(self):

+ 9
- 1
EditorialModel/fields.py Ver fichero

@@ -48,6 +48,14 @@ class EmField(EmComponent):
48 48
 
49 49
         super(EmField, self).__init__(model=model, uid=uid, name=name, string=string, help_text=help_text, date_update=date_update, date_create=date_create, rank=rank)
50 50
 
51
+    ## @brief Return the list of relation fields for a rel_to_type
52
+    # @return None if the field is not a rel_to_type else return a list of EmField
53
+    def rel_to_type_fields(self):
54
+        if not self.rel_to_type_id:
55
+            return None
56
+        
57
+        return [ f for f in self.model.components(EmField) if f.rel_field_id == self.uid ]
58
+
51 59
     ## Check if the EmField is valid
52 60
     # @return True if valid False if not
53 61
     def check(self):
@@ -77,4 +85,4 @@ class EmField(EmComponent):
77 85
         if self.fieldtype == 'boolean' and ('nullable' in self.options and self.options['nullable'] == 1):
78 86
             return models.NullBooleanField(**self.options)
79 87
 
80
-        return self.fieldtypes[self.fieldtype](**self.options)
88
+        return self.fieldtypes[self.fieldtype](**self.options)

+ 2
- 2
EditorialModel/model.py Ver fichero

@@ -1,4 +1,4 @@
1
-# -*- coding: utf-8 -*-
1
+#-*- coding: utf-8 -*-
2 2
 
3 3
 ## @file editorialmodel.py
4 4
 # Manage instance of an editorial model
@@ -91,7 +91,7 @@ class Model(object):
91 91
 
92 92
     ## Saves data using the current backend
93 93
     def save(self):
94
-        return self.backend.save()
94
+        return self.backend.save(self)
95 95
 
96 96
     ## Given a EmComponent child class return a list of instances
97 97
     # @param cls EmComponent : A python class

Loading…
Cancelar
Guardar