author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 15 Jul 2009 18:28:57 +0200 | |
branch | stable |
changeset 2378 | 75af50d66b0a |
parent 1977 | 606923dff11b |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
1 |
""" |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
""" |
0 | 8 |
class Schema : |
9 |
||
10 |
def __init__(self, schema) : |
|
11 |
self._schema = schema |
|
12 |
||
13 |
def get_attrs(self, entity) : |
|
14 |
return self._schema[entity][0] |
|
15 |
||
16 |
def get_relations(self, entity) : |
|
17 |
return self._schema[entity][1] |
|
18 |
||
19 |
def get_attr_index(self, entity, attr) : |
|
20 |
return list(self._schema[entity][0]).index(attr) |
|
21 |
||
22 |
SCHEMA = Schema({'societe': ( ('nom','ville'), |
|
23 |
[('concerne_par','affaire'), |
|
24 |
] ), |
|
25 |
'affaire': ( ('ref',), |
|
26 |
[('concerne','societe'), |
|
27 |
('concerne_par', 'document') |
|
28 |
] ), |
|
29 |
'document':( ('fichier', 'annee','mois','jour','type'), |
|
30 |
[('concerne','affaire'), |
|
31 |
] ), |
|
32 |
}) |
|
33 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
34 |
|
0 | 35 |
|
36 |
DATA = { 'societe': [ ('CETIAD', 'Dijon'), |
|
37 |
('EDF_R&D', 'Clamart'), |
|
38 |
('Logilab', 'Paris'), |
|
39 |
], |
|
40 |
'affaire': [ ('CTIA01', 'CETIAD'), |
|
41 |
('EDFR01', 'EDF_R&D'), |
|
42 |
('EDFR02', 'EDF_R&D'), |
|
43 |
], |
|
44 |
'document':[ ('CTIA01-040906-PRE-1-01.pdf','2004','09','06','PRE','CTIA01'), |
|
45 |
('EDFR01-050201-CLI-1-01.pdf','2005','02','01','CLI','EDFR01'), |
|
46 |
('EDFR01-050322-OFR-1-01.pdf','2005','03','22','OFR','EDFR01'), |
|
47 |
], |
|
48 |
} |
|
49 |
||
50 |
def get_data(entity, where=[]) : |
|
51 |
for value in DATA[entity] : |
|
52 |
for index, val in where : |
|
53 |
if value[index] != val : |
|
54 |
break |
|
55 |
else : |
|
56 |
yield value |
|
57 |
||
58 |
class PathParser : |
|
59 |
||
60 |
def __init__(self, schema, path) : |
|
61 |
self.schema = schema |
|
62 |
self.path = path |
|
63 |
self._components = iter([comp for comp in self.path.split('/') if comp]) |
|
64 |
self._entity = None |
|
65 |
self._attr = None |
|
66 |
self._rel = None |
|
67 |
self._restrictions = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
68 |
|
0 | 69 |
def parse(self) : |
70 |
self._entity = self._components.next() |
|
71 |
try: |
|
72 |
self.process_entity() |
|
73 |
except StopIteration : |
|
74 |
pass |
|
75 |
||
76 |
def process_entity(self) : |
|
77 |
_next = self._components.next() |
|
78 |
if _next in self.schema.get_attrs(self._entity) : |
|
79 |
self._attr = _next |
|
80 |
_next = self._components.next() |
|
81 |
self._restrictions.append( (self._entity, self._attr, _next) ) |
|
82 |
self._attr = None |
|
83 |
self._rel = None |
|
84 |
self.process_entity() |
|
85 |
||
86 |
def get_list(self) : |
|
87 |
if self._rel : |
|
88 |
return |
|
89 |
elif self._attr : |
|
90 |
where = [] |
|
91 |
for e,a,v in self._restrictions : |
|
92 |
i = self.schema.get_attr_index(e, a) |
|
93 |
where.append( (i,v) ) |
|
94 |
i = self.schema.get_attr_index(self._entity, self._attr) |
|
95 |
for values in get_data(self._entity,where) : |
|
96 |
yield values[i]+'/' |
|
97 |
else : |
|
98 |
attr_restrict = [a for e,a,v in self._restrictions] |
|
99 |
for attr in self.schema.get_attrs(self._entity) : |
|
100 |
if attr not in attr_restrict : |
|
101 |
yield attr+'/' |
|
102 |
for data in DATA[self._entity]: |
|
103 |
yield data[0] |
|
104 |
for nom, entity in self.schema.get_relations(self._entity) : |
|
105 |
yield nom+'/' |
|
106 |
yield entity+'/' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
107 |
|
0 | 108 |
def ls(path) : |
109 |
p = PathParser(SCHEMA,path) |
|
110 |
p.parse() |
|
111 |
return list(p.get_list()) |
|
112 |
||
113 |
||
114 |
class SytPathParser : |
|
115 |
||
116 |
def __init__(self, schema, path) : |
|
117 |
self.schema = schema |
|
118 |
self.path = path |
|
119 |
self._components = iter([comp for comp in self.path.split('/') if comp]) |
|
120 |
self._e_type = None |
|
121 |
self._restrictions = [] |
|
122 |
self._alphabet = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
123 |
|
0 | 124 |
def parse(self): |
125 |
self._var = self._alphabet.pop(0) |
|
126 |
self._e_type = self._components.next() |
|
127 |
e_type = self._e_type.capitalize() |
|
128 |
self._restrictions.append('%s is %s' % (self._var, e_type)) |
|
129 |
try: |
|
130 |
self.process_entity() |
|
131 |
except StopIteration : |
|
132 |
pass |
|
133 |
return 'Any %s WHERE %s' % (self._var, ', '.join(self._restrictions)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
134 |
|
0 | 135 |
def process_entity(self) : |
136 |
_next = self._components.next() |
|
137 |
if _next in self.schema.get_attrs(self._e_type) : |
|
138 |
attr = _next |
|
139 |
try: |
|
140 |
_next = self._components.next() |
|
141 |
self._restrictions.append('%s %s %s' % (self._var, attr, _next)) |
|
142 |
except StopIteration: |
|
143 |
a_var = self._alphabet.pop(0) |
|
144 |
self._restrictions.append('%s %s %s' % (self._var, attr, a_var) ) |
|
145 |
self._var = a_var |
|
146 |
raise |
|
147 |
elif _next in [r for r,e in self.schema.get_relations(self._e_type)]: |
|
148 |
rel = _next |
|
149 |
r_var = self._alphabet.pop(0) |
|
150 |
self._restrictions.append('%s %s %s' % (self._var, rel, r_var)) |
|
151 |
self._var = r_var |
|
152 |
try: |
|
153 |
_next = self._components.next() |
|
154 |
self._restrictions.append('%s is %s' % (r_var, _next.capitalize())) |
|
155 |
except StopIteration: |
|
156 |
raise |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
157 |
self.process_entity() |
0 | 158 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
159 |
|
0 | 160 |
def to_rql(path) : |
161 |
p = SytPathParser(SCHEMA,path) |
|
162 |
return p.parse() |
|
163 |