Esempio n. 1
0
def test_progenitor_classes():
    import re
    class A(object): pass
    class B: pass
    class C(A): pass
    class D(C): pass
    class E(B,C): pass
    
    a_pro = progenitor_classes(A)
    d_pro = progenitor_classes(D)
    e_pro = set(progenitor_classes(E))
    if a_pro != [A]:
        print 'progenitor_classes failed self progenitor test'
        print "\t" + str(a_pro) + ' should be ' + str([A])
    if d_pro != [A]:
        print 'progenitor_classes failed grand-parent test:'
        print "\t" + str(d_pro) + ' should be ' + str([A])
    if e_pro != set([A,B]):
        print 'progenitor_classes failed dual inheritance test:'
        print "\t" + str(e_pro) + ' should be ' + str(set([A,B]))
def initiate_mockdb(config):
    clses = classes_list(config)
    mockdb={}
    for cls in clses:
        pro_cs = progenitor_classes(cls)
        if len(pro_cs) > 1:
            continue
        if not KeyedObject in pro_cs:
            continue
        anc_cs = ancestor_classes(cls)
        if NumberedObject in anc_cs:
            mockdb[cls.__name__] = SetOfNumberedObjects(cls=cls)
        else:
            mockdb[cls.__name__] = SetOfKeyedObjects(cls=cls)
        mockdb[cls.__name__].__load__(config)
    return mockdb
Esempio n. 3
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