def Run(self): for x in self.Query(Banlist).select(): self.data = "" print("Retrieving data from [%s]" % (x.name)) if x.url == 'SPECIAL': function = "self.Get%s()" % (x.name.capitalize()) data = eval(function) else: data = self.GetBasic(x.url) print("Done".ljust(50)) classobj = eval(x.name.capitalize()) #Now that we have the data, delete the previous contents #of the table so we don't have to deal with updates sa.class_mapper(classobj).local_table.drop() sa.class_mapper(classobj).local_table.create() sys.stdout.write("Creating database objects...") sys.stdout.flush() self.InjectIntoDb(classobj, data) print("DONE") sys.stdout.write("Flushing to disk...") sys.stdout.flush() self.session.flush() print("DONE")
def testbasic(self): # Set up activemapper objects foo1, baz1 = self.create_objects() objectstore.flush() objectstore.clear() foo1 = foo.get_by(name='foo1') baz1 = baz.get_by(name='baz1') # Just checking ... assert (foo1.name == 'foo1') assert (baz1.name == 'baz1') # Diagnostics ... # import sys # sys.stderr.write("\nbazrel missing from dir(foo1):\n%s\n" % dir(foo1)) # sys.stderr.write("\nbazrel in foo1 relations:\n%s\n" % foo1.relations) # Optimistically based on activemapper one_to_many test, try to append # baz1 to foo1.bazrel - (AttributeError: 'foo' object has no attribute 'bazrel') print sqlalchemy.class_mapper(foo).props print sqlalchemy.class_mapper(baz).props foo1.bazrel.append(baz1) assert (foo1.bazrel == [baz1])
def process_relationships(klass, was_deferred=False): # first, we loop through all of the relationships defined on the # class, and make sure that the related class already has been # completely processed and defer processing if it has not defer = False for propname, reldesc in klass.relations.items(): found = (reldesc.classname == klass.__name__ or reldesc.classname in __processed_classes__) if not found: defer = True break # next, we loop through all the columns looking for foreign keys # and make sure that we can find the related tables (they do not # have to be processed yet, just defined), and we defer if we are # not able to find any of the related tables if not defer: for col in klass.columns: if col.foreign_key is not None: found = False table_name = col.foreign_key._colspec.rsplit('.', 1)[0] for other_klass in ActiveMapperMeta.classes.values(): if other_klass.table.fullname.lower() == table_name.lower( ): found = True if not found: defer = True break if defer and not was_deferred: __deferred_classes__[klass.__name__] = klass # if we are able to find all related and referred to tables, then # we can go ahead and assign the relationships to the class if not defer: relations = {} for propname, reldesc in klass.relations.items(): reldesc.process(klass, propname, relations) class_mapper(klass).add_properties(relations) if klass.__name__ in __deferred_classes__: del __deferred_classes__[klass.__name__] __processed_classes__[klass.__name__] = klass # finally, loop through the deferred classes and attempt to process # relationships for them if not was_deferred: # loop through the list of deferred classes, processing the # relationships, until we can make no more progress last_count = len(__deferred_classes__) + 1 while last_count > len(__deferred_classes__): last_count = len(__deferred_classes__) deferred = __deferred_classes__.copy() for deferred_class in deferred.values(): process_relationships(deferred_class, was_deferred=True)
def process_relationships(klass, was_deferred=False): # first, we loop through all of the relationships defined on the # class, and make sure that the related class already has been # completely processed and defer processing if it has not defer = False for propname, reldesc in klass.relations.items(): found = (reldesc.classname == klass.__name__ or reldesc.classname in __processed_classes__) if not found: defer = True break # next, we loop through all the columns looking for foreign keys # and make sure that we can find the related tables (they do not # have to be processed yet, just defined), and we defer if we are # not able to find any of the related tables if not defer: for col in klass.columns: if col.foreign_key is not None: found = False cn = col.foreign_key._colspec table_name = cn[:cn.rindex('.')] for other_klass in ActiveMapperMeta.classes.values(): if other_klass.table.fullname.lower() == table_name.lower(): found = True if not found: defer = True break if defer and not was_deferred: __deferred_classes__[klass.__name__] = klass # if we are able to find all related and referred to tables, then # we can go ahead and assign the relationships to the class if not defer: relations = {} for propname, reldesc in klass.relations.items(): reldesc.process(klass, propname, relations) class_mapper(klass).add_properties(relations) if klass.__name__ in __deferred_classes__: del __deferred_classes__[klass.__name__] __processed_classes__[klass.__name__] = klass # finally, loop through the deferred classes and attempt to process # relationships for them if not was_deferred: # loop through the list of deferred classes, processing the # relationships, until we can make no more progress last_count = len(__deferred_classes__) + 1 while last_count > len(__deferred_classes__): last_count = len(__deferred_classes__) deferred = __deferred_classes__.copy() for deferred_class in deferred.values(): process_relationships(deferred_class, was_deferred=True)
def class_mapper(class_): return sqlalchemy.class_mapper(class_)