devtools/dataimport.py
branchstable
changeset 4721 8f63691ccb7f
parent 4613 141a4f613f8a
child 4734 4ae30c9ca11b
equal deleted inserted replaced
4720:ddf4f19eb07a 4721:8f63691ccb7f
   150 def tell(msg):
   150 def tell(msg):
   151     print msg
   151     print msg
   152 
   152 
   153 def confirm(question):
   153 def confirm(question):
   154     """A confirm function that asks for yes/no/abort and exits on abort."""
   154     """A confirm function that asks for yes/no/abort and exits on abort."""
   155     answer = shellutils.ASK.ask(question, ('Y','n','abort'), 'Y')
   155     answer = shellutils.ASK.ask(question, ('Y', 'n', 'abort'), 'Y')
   156     if answer == 'abort':
   156     if answer == 'abort':
   157         sys.exit(1)
   157         sys.exit(1)
   158     return answer == 'Y'
   158     return answer == 'Y'
   159 
   159 
   160 
   160 
   273 
   273 
   274 # base integrity checking functions ############################################
   274 # base integrity checking functions ############################################
   275 
   275 
   276 def check_doubles(buckets):
   276 def check_doubles(buckets):
   277     """Extract the keys that have more than one item in their bucket."""
   277     """Extract the keys that have more than one item in their bucket."""
   278     return [(key, len(value)) for key,value in buckets.items() if len(value) > 1]
   278     return [(k, len(v)) for k, v in buckets.items() if len(v) > 1]
   279 
   279 
   280 def check_doubles_not_none(buckets):
   280 def check_doubles_not_none(buckets):
   281     """Extract the keys that have more than one item in their bucket."""
   281     """Extract the keys that have more than one item in their bucket."""
   282     return [(key, len(value)) for key,value in buckets.items() if key is not None and len(value) > 1]
   282     return [(k, len(v)) for k, v in buckets.items()
       
   283             if k is not None and len(v) > 1]
   283 
   284 
   284 
   285 
   285 # object stores #################################################################
   286 # object stores #################################################################
   286 
   287 
   287 class ObjectStore(object):
   288 class ObjectStore(object):
   428         self.eids[entity.eid] = entity
   429         self.eids[entity.eid] = entity
   429         self.types.setdefault(args[0], []).append(entity.eid)
   430         self.types.setdefault(args[0], []).append(entity.eid)
   430         return entity
   431         return entity
   431 
   432 
   432     def _put(self, type, item):
   433     def _put(self, type, item):
   433         query = ('INSERT %s X: ' % type) + ', '.join(['X %s %%(%s)s' % (key,key) for key in item])
   434         query = ('INSERT %s X: ' % type) + ', '.join('X %s %%(%s)s' % (k, k)
       
   435                                                      for k in item])
   434         return self.rql(query, item)[0][0]
   436         return self.rql(query, item)[0][0]
   435 
   437 
   436     def relate(self, eid_from, rtype, eid_to):
   438     def relate(self, eid_from, rtype, eid_to):
   437         # if reverse relation is found, eids are exchanged
   439         # if reverse relation is found, eids are exchanged
   438         eid_from, rtype, eid_to = super(RQLObjectStore, self).relate(eid_from, rtype, eid_to)
   440         eid_from, rtype, eid_to = super(RQLObjectStore, self).relate(
       
   441             eid_from, rtype, eid_to)
   439         self.rql('SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % rtype,
   442         self.rql('SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % rtype,
   440                   {'x': int(eid_from), 'y': int(eid_to)}, ('x', 'y'))
   443                   {'x': int(eid_from), 'y': int(eid_to)}, ('x', 'y'))
   441 
   444 
   442 
   445 
   443 # the import controller ########################################################
   446 # the import controller ########################################################
   511         nberrors = sum(len(err[1]) for err in self.errors.values())
   514         nberrors = sum(len(err[1]) for err in self.errors.values())
   512         self.tell('\nImport completed: %i entities, %i types, %i relations and %i errors'
   515         self.tell('\nImport completed: %i entities, %i types, %i relations and %i errors'
   513                   % (len(self.store.eids), len(self.store.types),
   516                   % (len(self.store.eids), len(self.store.types),
   514                      len(self.store.relations), nberrors))
   517                      len(self.store.relations), nberrors))
   515         if self.errors:
   518         if self.errors:
   516             if self.askerror==2 or (self.askerror and confirm('Display errors ?')):
   519             if self.askerror == 2 or (self.askerror and confirm('Display errors ?')):
   517                 from pprint import pformat
   520                 from pprint import pformat
   518                 for errkey, error in self.errors.items():
   521                 for errkey, error in self.errors.items():
   519                     self.tell("\n%s (%s): %d\n" % (error[0], errkey, len(error[1])))
   522                     self.tell("\n%s (%s): %d\n" % (error[0], errkey, len(error[1])))
   520                     self.tell(pformat(sorted(error[1])))
   523                     self.tell(pformat(sorted(error[1])))
   521 
   524