""" This example prints all database hierarchy """ import sys from ttdb.model import HierarchyElement, Crate, obtain_session def print_hierarchy(element, level): assert isinstance(element, HierarchyElement) #spases to highlight hierarchy level spaces = " "*6*int(level) #if element has children id build semi graphical line to it if element.channel_id: # crate name relation_str = " ---> '{}' -> ".format(element.channel.module.crate.name) #module info module = element.channel.module relation_str += "'module(db={}) SN({}) at slot('{}')' -> ".format(module.id, module.serial, module.slot) #channel info channel = element.channel relation_str += "'channel={} enabled={}'".format(channel.channel, channel.enabled) else: relation_str = "" #print info print ("{} {} ({}) {}".format(spaces, element.name, element.type, relation_str)) #recursively iterate through children if len(element.children): for child in element.children: print_hierarchy(child, level+1) if __name__ == "__main__": #get connection string if len(sys.argv) <= 1: exit("Provide connection string for database. For sqlite use 'sqlite:///' (Note 3 slashes)") session = obtain_session(sys.argv[1]) #select all root level elements elements = session.query(HierarchyElement).filter(HierarchyElement.parent_id == None).all() #iterate it for element in elements: print_hierarchy(element, 0) #print crate->Module info print("--------------------------------------------------") print(" C R A T E M O D U L E ") print("--------------------------------------------------") #select first (by id) crate crate = session.query(Crate).order_by(Crate.id).first() print("{}".format(crate.name)) for module in crate.modules: print(" {} ==> {} (id={})".format(module.slot, module.type, module.id))