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
 def __overwrite__(self,config,base_dir=None,begin_fresh=False):
     if base_dir == None:
         base_dir = config.get('Common_directories','mockdb')
     clses = self.__class_set__()
     for cls in clses:
         if begin_fresh == True:
             union_ids = set(self.objects.keys())
         else:
             stored_db = self.__class__(cls=cls)
             stored_db.__load__(config)
             stored_ids = set(stored_db.objects.keys())
             current_ids = set(self.objects.keys())
             union_ids = list(current_ids.union(stored_ids))
             stored_keys = stored_db.__key_set__(cls)
             current_keys = self.__key_set__(cls)
             union_keys = list(current_keys.union(stored_keys))
         out_keys = list(union_keys)
         out_keys.remove('key')
         union_ids.sort()
         out_keys.sort()
         out_keys = ['key'] + out_keys
         db_fname = db_file_name(KeyedObject, cls, base_dir)
         with open(db_fname,'w') as f:
             f.write(",".join(out_keys) + "\n")
             for i in union_ids:
                 if i in current_ids:
                     header, string = self.objects[i].__contents_as_string__(config,base_dir=base_dir,cls=cls,keys=out_keys)
                 else:
                     header, string = stored_db.objects[i].__contents_as_string__(config,base_dir=base_dir,cls=cls,keys=out_keys)
                 f.write(string + "\n")
 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
 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