# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.""""""classSchema:def__init__(self,schema):self._schema=schemadefget_attrs(self,entity):returnself._schema[entity][0]defget_relations(self,entity):returnself._schema[entity][1]defget_attr_index(self,entity,attr):returnlist(self._schema[entity][0]).index(attr)SCHEMA=Schema({'societe':(('nom','ville'),[('concerne_par','affaire'),]),'affaire':(('ref',),[('concerne','societe'),('concerne_par','document')]),'document':(('fichier','annee','mois','jour','type'),[('concerne','affaire'),]),})DATA={'societe':[('CETIAD','Dijon'),('EDF_R&D','Clamart'),('Logilab','Paris'),],'affaire':[('CTIA01','CETIAD'),('EDFR01','EDF_R&D'),('EDFR02','EDF_R&D'),],'document':[('CTIA01-040906-PRE-1-01.pdf','2004','09','06','PRE','CTIA01'),('EDFR01-050201-CLI-1-01.pdf','2005','02','01','CLI','EDFR01'),('EDFR01-050322-OFR-1-01.pdf','2005','03','22','OFR','EDFR01'),],}defget_data(entity,where=[]):forvalueinDATA[entity]:forindex,valinwhere:ifvalue[index]!=val:breakelse:yieldvalueclassPathParser:def__init__(self,schema,path):self.schema=schemaself.path=pathself._components=iter([compforcompinself.path.split('/')ifcomp])self._entity=Noneself._attr=Noneself._rel=Noneself._restrictions=[]defparse(self):self._entity=self._components.next()try:self.process_entity()exceptStopIteration:passdefprocess_entity(self):_next=self._components.next()if_nextinself.schema.get_attrs(self._entity):self._attr=_next_next=self._components.next()self._restrictions.append((self._entity,self._attr,_next))self._attr=Noneself._rel=Noneself.process_entity()defget_list(self):ifself._rel:returnelifself._attr:where=[]fore,a,vinself._restrictions:i=self.schema.get_attr_index(e,a)where.append((i,v))i=self.schema.get_attr_index(self._entity,self._attr)forvaluesinget_data(self._entity,where):yieldvalues[i]+'/'else:attr_restrict=[afore,a,vinself._restrictions]forattrinself.schema.get_attrs(self._entity):ifattrnotinattr_restrict:yieldattr+'/'fordatainDATA[self._entity]:yielddata[0]fornom,entityinself.schema.get_relations(self._entity):yieldnom+'/'yieldentity+'/'defls(path):p=PathParser(SCHEMA,path)p.parse()returnlist(p.get_list())classSytPathParser:def__init__(self,schema,path):self.schema=schemaself.path=pathself._components=iter([compforcompinself.path.split('/')ifcomp])self._e_type=Noneself._restrictions=[]self._alphabet=list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')defparse(self):self._var=self._alphabet.pop(0)self._e_type=self._components.next()e_type=self._e_type.capitalize()self._restrictions.append('%s is %s'%(self._var,e_type))try:self.process_entity()exceptStopIteration:passreturn'Any %s WHERE %s'%(self._var,', '.join(self._restrictions))defprocess_entity(self):_next=self._components.next()if_nextinself.schema.get_attrs(self._e_type):attr=_nexttry:_next=self._components.next()self._restrictions.append('%s%s%s'%(self._var,attr,_next))exceptStopIteration:a_var=self._alphabet.pop(0)self._restrictions.append('%s%s%s'%(self._var,attr,a_var))self._var=a_varraiseelif_nextin[rforr,einself.schema.get_relations(self._e_type)]:rel=_nextr_var=self._alphabet.pop(0)self._restrictions.append('%s%s%s'%(self._var,rel,r_var))self._var=r_vartry:_next=self._components.next()self._restrictions.append('%s is %s'%(r_var,_next.capitalize()))exceptStopIteration:raiseself.process_entity()defto_rql(path):p=SytPathParser(SCHEMA,path)returnp.parse()