Exemplo n.º 1
0
 def __contents_as_string__(self,config,cls=None,base_dir=None,keys=None,sep=','):
     if base_dir == None:
         base_dir = config.get('Common_directories','mockdb')
     if cls == None:
         cls = self.__class__
     if keys == None:
         db_fname = db_file_name(KeyedObject,cls,base_dir)
         if os.path.isfile(db_fname):
             ks = extract_db_keys(db_fname)
             if ks == []:
                 ks.extend(self.__dict__.keys().sort())
             #self_ks = self.__dict__.keys()
             #if set(self_keys).issubset(set(keys)):
             #    pass
             #else:
             #    raise FormattingError("Keys are not a subset of attibutes.")
         else:
             ks = self.__dict__.keys()
             ks.sort()
     else:
          ks = list(keys)
     string = ''
     ks.remove('key')
     ks = ['key'] + list(ks)
     header = sep.join(ks)
     for k in ks:
         if string != '':
             string += ','
         try:
             string += str(getattr(self,k))
         except AttributeError:
             string += 'None'
     return header, string
Exemplo n.º 2
0
 def __load__(self,config,no_children = True, base_dir = None, key=None):
     if base_dir == None:
         base_dir = config.get('Common_directories','mockdb')
     first_clses =  progenitor_classes(self.cls)
     if len(first_clses) > 1:
         raise TypeError("The class {0} cannot be made into a SetOf class.\n".format(cls.__name__))
     #Some keyed classes might have children classes.  Load these as well unless no_children is flagged
     if no_children is True:
         clses = [self.cls]
     else:
         clses = [self.cls] + child_classes(self.cls,config=config)
     for c in clses:
         db_fname = db_file_name(first_clses[0],self.cls,base_dir)
         #sys.stderr.write("%s\n" % db_fname)
         if os.path.isfile(db_fname):
             keys = extract_db_keys(db_fname)
             key_index=keys.index('key')
             with open(db_fname,'r') as f:
                 f.readline() #Gets past header to contents.
                 for line in f:
                     vals = line.rstrip().split(',')
                     if len(vals) != len(keys):
                         raise FormattingError("The formatting in {0} is incorrect.  The number of keys and the number of values are different in the following line:\n{1}\n".format(db_fname,str(line)))
                     instance = self.cls(config)
                     for i in range(0,len(vals)):
                         if vals[i] == 'None':
                             setattr(instance,keys[i],None)
                             continue
                         if vals[i] == 'True':
                             setattr(instance,keys[i],True)
                             continue
                         if vals[i] == 'False':
                             setattr(instance,keys[i],False)
                             continue
                         if keys[i] == 'sample_key' or self.cls.__name__ == 'Sample':
                             setattr(instance,keys[i],vals[i])
                             continue
                         try:
                             int_val = int(vals[i])
                             setattr(instance,keys[i],int_val)
                         except ValueError:
                             setattr(instance,keys[i],vals[i])
                     if key != None:
                         if instance.key not in [key]:
                             continue
                     #Load all keys if key is none.  Elsewise, just that key.
                     self.objects[instance.key] = instance
Exemplo n.º 3
0
 def __save__(self,config,base_dir=None):
     if base_dir == None:
         base_dir = config.get('Common_directories','mockdb')
     if self.objects.keys() == []:
         return 1
     obj_lines = {}
     header = {}
     clses = self.__class_set__()
     for cls in clses:
         obj_lines[cls] = {}
         header[cls] = ""
     for key, instance in self.objects.iteritems():
         #try:
         cls = instance.__class__
         head, obj_lines[cls][key] = instance.__contents_as_string__(config,base_dir=base_dir)
         if header[cls] == "":
             header[cls] = head
         if header[cls] != head:
             raise FormattingError("Different objects of the same class have different keys.")
         #except FormattingError:
         #    self.__overwrite__(base_dir=base_dir)
     for cls in obj_lines.keys():
         db_fname = db_file_name(KeyedObject, cls, base_dir)
         if not os.path.isfile(db_fname):
             with open(db_fname,'w') as f:
                 f.write(header[cls] + "\n")
                 for key in obj_lines[cls].keys():
                     f.write(obj_lines[cls][key] + "\n")
             continue
         #Can get rid of this save function (inheritance) if I instead write extract_db_keys for SetOfKeyedObject and SetOfNumberObject 
         db_ids = set(extract_db_keys(db_fname))
         obj_ids = set(obj_lines[cls].keys())
         intersection = db_ids.intersection(obj_ids)
         other = self.__class__(self.cls)
         other.__load__(config,key=intersection)
         if self.__is_contained__(other,*intersection):
             with open(db_fname,'a') as f:
                 f.write(header[cls] + "\n")
                 for key in obj_lines[cls].keys():
                     f.write(obj_lines[cls][key] + "\n")
         else:
             self.__overwrite__(config,base_dir=base_dir)
     return 1
Exemplo n.º 4
0
def test_extract_db_keys():
    db_file=os.path.join(os.getcwd(),'mockdb/test_data/keyed_object/test_load_class.db')
    keys = extract_db_keys(db_file)
    correct_keys = ['key', 'obj_type', 'attr1', 'attr2']
    if set(keys) != set(correct_keys):
        print 'extract_db_keys failed.'