dataimport.py
changeset 6427 c8a5ac2d1eaa
parent 6173 eb386e473044
child 6492 47a284c0d012
equal deleted inserted replaced
6426:541659c39f6a 6427:c8a5ac2d1eaa
   649     def _put(self, type, item):
   649     def _put(self, type, item):
   650         raise RuntimeError('use create entity')
   650         raise RuntimeError('use create entity')
   651 
   651 
   652 
   652 
   653 class MetaGenerator(object):
   653 class MetaGenerator(object):
       
   654     META_RELATIONS = (META_RTYPES
       
   655                       - VIRTUAL_RTYPES
       
   656                       - set(('eid', 'cwuri',
       
   657                              'is', 'is_instance_of', 'cw_source')))
       
   658 
   654     def __init__(self, session, baseurl=None):
   659     def __init__(self, session, baseurl=None):
   655         self.session = session
   660         self.session = session
   656         self.source = session.repo.system_source
   661         self.source = session.repo.system_source
   657         self.time = datetime.now()
   662         self.time = datetime.now()
   658         if baseurl is None:
   663         if baseurl is None:
   667         # attributes/relations specific to each entity
   672         # attributes/relations specific to each entity
   668         self.entity_attrs = ['cwuri']
   673         self.entity_attrs = ['cwuri']
   669         #self.entity_rels = [] XXX not handled (YAGNI?)
   674         #self.entity_rels = [] XXX not handled (YAGNI?)
   670         schema = session.vreg.schema
   675         schema = session.vreg.schema
   671         rschema = schema.rschema
   676         rschema = schema.rschema
   672         for rtype in META_RTYPES:
   677         for rtype in self.META_RELATIONS:
   673             if rtype in ('eid', 'cwuri') or rtype in VIRTUAL_RTYPES:
       
   674                 continue
       
   675             if rschema(rtype).final:
   678             if rschema(rtype).final:
   676                 self.etype_attrs.append(rtype)
   679                 self.etype_attrs.append(rtype)
   677             else:
   680             else:
   678                 self.etype_rels.append(rtype)
   681                 self.etype_rels.append(rtype)
   679         if not schema._eid_index:
       
   680             # test schema loaded from the fs
       
   681             self.gen_is = self.test_gen_is
       
   682             self.gen_is_instance_of = self.test_gen_is_instanceof
       
   683 
   682 
   684     @cached
   683     @cached
   685     def base_etype_dicts(self, etype):
   684     def base_etype_dicts(self, etype):
   686         entity = self.session.vreg['etypes'].etype_class(etype)(self.session)
   685         entity = self.session.vreg['etypes'].etype_class(etype)(self.session)
   687         # entity are "surface" copied, avoid shared dict between copies
   686         # entity are "surface" copied, avoid shared dict between copies
   708     def gen_creation_date(self, entity):
   707     def gen_creation_date(self, entity):
   709         return self.time
   708         return self.time
   710     def gen_modification_date(self, entity):
   709     def gen_modification_date(self, entity):
   711         return self.time
   710         return self.time
   712 
   711 
   713     def gen_is(self, entity):
       
   714         return entity.e_schema.eid
       
   715     def gen_is_instance_of(self, entity):
       
   716         eids = []
       
   717         for etype in entity.e_schema.ancestors() + [entity.e_schema]:
       
   718             eids.append(entity.e_schema.eid)
       
   719         return eids
       
   720 
       
   721     def gen_created_by(self, entity):
   712     def gen_created_by(self, entity):
   722         return self.session.user.eid
   713         return self.session.user.eid
   723     def gen_owned_by(self, entity):
   714     def gen_owned_by(self, entity):
   724         return self.session.user.eid
   715         return self.session.user.eid
   725 
       
   726     # implementations of gen_is / gen_is_instance_of to use during test where
       
   727     # schema has been loaded from the fs (hence entity type schema eids are not
       
   728     # known)
       
   729     def test_gen_is(self, entity):
       
   730         return eschema_eid(self.session, entity.e_schema)
       
   731     def test_gen_is_instanceof(self, entity):
       
   732         eids = []
       
   733         for eschema in entity.e_schema.ancestors() + [entity.e_schema]:
       
   734             eids.append(eschema_eid(self.session, eschema))
       
   735         return eids