misc/cwfs/cwfs.py
changeset 0 b97547f5f1fa
child 1802 d628defebc17
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 class Schema :
       
     2 
       
     3     def __init__(self, schema) :
       
     4         self._schema = schema
       
     5 
       
     6     def get_attrs(self, entity) :
       
     7         return self._schema[entity][0]
       
     8 
       
     9     def get_relations(self, entity) :
       
    10         return self._schema[entity][1]
       
    11 
       
    12     def get_attr_index(self, entity, attr) :
       
    13         return list(self._schema[entity][0]).index(attr)
       
    14 
       
    15 SCHEMA = Schema({'societe': ( ('nom','ville'),
       
    16                               [('concerne_par','affaire'),
       
    17                                ] ),
       
    18                  'affaire': ( ('ref',),
       
    19                               [('concerne','societe'),
       
    20                                ('concerne_par', 'document')
       
    21                                ] ),
       
    22                  'document':( ('fichier', 'annee','mois','jour','type'),
       
    23                               [('concerne','affaire'),
       
    24                                ] ),
       
    25                  })
       
    26 
       
    27     
       
    28 
       
    29 DATA = { 'societe': [ ('CETIAD', 'Dijon'),
       
    30                       ('EDF_R&D', 'Clamart'),
       
    31                       ('Logilab', 'Paris'),
       
    32                       ],
       
    33          'affaire': [ ('CTIA01', 'CETIAD'),
       
    34                       ('EDFR01', 'EDF_R&D'),
       
    35                       ('EDFR02', 'EDF_R&D'),
       
    36                       ],
       
    37          'document':[ ('CTIA01-040906-PRE-1-01.pdf','2004','09','06','PRE','CTIA01'),
       
    38                       ('EDFR01-050201-CLI-1-01.pdf','2005','02','01','CLI','EDFR01'),
       
    39                       ('EDFR01-050322-OFR-1-01.pdf','2005','03','22','OFR','EDFR01'),
       
    40                       ],
       
    41          }
       
    42 
       
    43 def get_data(entity, where=[]) :
       
    44     for value in DATA[entity] :
       
    45         for index, val in where :
       
    46             if value[index] != val :
       
    47                 break
       
    48         else :
       
    49             yield value
       
    50 
       
    51 class PathParser :
       
    52 
       
    53     def __init__(self, schema, path) :
       
    54         self.schema = schema
       
    55         self.path = path
       
    56         self._components = iter([comp for comp in self.path.split('/') if comp])
       
    57         self._entity = None
       
    58         self._attr = None
       
    59         self._rel = None
       
    60         self._restrictions = []
       
    61         
       
    62     def parse(self) :
       
    63         self._entity = self._components.next()
       
    64         try:
       
    65             self.process_entity()
       
    66         except StopIteration :
       
    67             pass
       
    68 
       
    69     def process_entity(self) :
       
    70         _next = self._components.next()
       
    71         if _next in self.schema.get_attrs(self._entity) :
       
    72             self._attr = _next
       
    73             _next = self._components.next()
       
    74             self._restrictions.append( (self._entity, self._attr, _next) )
       
    75             self._attr = None
       
    76             self._rel = None
       
    77             self.process_entity()
       
    78 
       
    79     def get_list(self) :
       
    80         if self._rel :
       
    81             return
       
    82         elif self._attr :
       
    83             where = []
       
    84             for e,a,v in self._restrictions :
       
    85                 i = self.schema.get_attr_index(e, a)
       
    86                 where.append( (i,v) )
       
    87             i = self.schema.get_attr_index(self._entity, self._attr)
       
    88             for values in get_data(self._entity,where) :
       
    89                 yield values[i]+'/'
       
    90         else :
       
    91             attr_restrict = [a for e,a,v in self._restrictions]
       
    92             for attr in self.schema.get_attrs(self._entity) :
       
    93                 if attr not in attr_restrict :
       
    94                     yield attr+'/'
       
    95             for data in DATA[self._entity]:
       
    96                 yield data[0]
       
    97             for nom, entity in self.schema.get_relations(self._entity) :
       
    98                 yield nom+'/'
       
    99                 yield entity+'/'
       
   100     
       
   101 def ls(path) :
       
   102     p = PathParser(SCHEMA,path)
       
   103     p.parse()
       
   104     return list(p.get_list())
       
   105 
       
   106 
       
   107 class SytPathParser :
       
   108 
       
   109     def __init__(self, schema, path) :
       
   110         self.schema = schema
       
   111         self.path = path
       
   112         self._components = iter([comp for comp in self.path.split('/') if comp])
       
   113         self._e_type = None
       
   114         self._restrictions = []
       
   115         self._alphabet = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
       
   116         
       
   117     def parse(self):
       
   118         self._var = self._alphabet.pop(0)
       
   119         self._e_type = self._components.next()
       
   120         e_type = self._e_type.capitalize()
       
   121         self._restrictions.append('%s is %s' % (self._var, e_type))
       
   122         try:
       
   123             self.process_entity()
       
   124         except StopIteration :
       
   125             pass
       
   126         return 'Any %s WHERE %s' % (self._var, ', '.join(self._restrictions))
       
   127     
       
   128     def process_entity(self) :
       
   129         _next = self._components.next()
       
   130         if _next in self.schema.get_attrs(self._e_type) :
       
   131             attr = _next
       
   132             try:
       
   133                 _next = self._components.next()
       
   134                 self._restrictions.append('%s %s %s' % (self._var, attr, _next))
       
   135             except StopIteration:
       
   136                 a_var = self._alphabet.pop(0)
       
   137                 self._restrictions.append('%s %s %s' % (self._var, attr, a_var) )
       
   138                 self._var = a_var
       
   139                 raise
       
   140         elif _next in [r for r,e in self.schema.get_relations(self._e_type)]:
       
   141             rel = _next
       
   142             r_var = self._alphabet.pop(0)
       
   143             self._restrictions.append('%s %s %s' % (self._var, rel, r_var))
       
   144             self._var = r_var
       
   145             try:
       
   146                 _next = self._components.next()
       
   147                 self._restrictions.append('%s is %s' % (r_var, _next.capitalize()))
       
   148             except StopIteration:
       
   149                 raise
       
   150         self.process_entity()            
       
   151 
       
   152         
       
   153 def to_rql(path) :
       
   154     p = SytPathParser(SCHEMA,path)
       
   155     return p.parse()
       
   156