def main(): parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('--json', help='filename') parser.add_argument('--dotoutput', help='filename') parser.add_argument('--pngoutput', help='filename', default='output.png') url_prefix = os.getenv('URL_PREFIX') parser.add_argument('--url', help='filename', default=url_prefix) args = parser.parse_args() tree = Tree() data = read_json_data(args.json) if 'nodes' in data: for x in data['nodes']: name = x['name'] key = '' parent = None if 'key' in x: key = get_key(name, x['key']) else: key = get_key(name) if 'parent' in x: parent = get_key(x['parent']) #print (name, key, parent, x['parent']) tree.create_node(name, key, parent) print(map_keys) tree.show() x = recursive(tree.to_dict()) if 'edges' in data: for d in data['edges']: src = get_key(d['src']) dst = get_key(d['dst']) color = 'black' if 'color' in d: color = d['color'] x.edge(src, dst, color=color) #print (x.source) if args.dotoutput: #print (json.dumps(data)) with open(args.dotoutput, 'w') as f: f.write(x.source) if args.url: url = '%s' % (args.url) r = requests.post(url, json={"datatype": "dotty", "data": x.source}) js = json.loads(r.content) id = js['uuid'] url = '%s/%s/png' % (args.url, id) print(url) r = requests.get(url, stream=True) print(r) if r.status_code == 200: with open(args.pngoutput, 'wb') as out_file: shutil.copyfileobj(r.raw, out_file)
def buildTree(dictOfDatas): tree = Tree() used_datas = [] while dictOfDatas: for key, value in dictOfDatas.items(): if value in used_datas: tree.create_node(key, key, parent=value) used_datas.append(key) dictOfDatas.pop(key) break elif value is None: tree.create_node(key, key) used_datas.append(key) dictOfDatas.pop(key) break tree.show() return tree.to_dict()
def spaces(self, output="dictionary"): spaces = self.get_all_spaces() page_labels = self.get_page_labels() spaces_tree = Tree() #Add PDF name as top node spaces_tree.create_node(self.file_name, self.file_name) #Add pages nodes for key, value in page_labels.items(): spaces_tree.create_node(value, value, parent=self.file_name) spaces_tree = self.spacestree(spaces[key], spaces_tree, value) if output == "tree": return (spaces_tree) elif output == "hierarchy": #Getting a master space hierarchy by column #Max depth starts calculating outside the doc name and sheet name max_depth = max([len(i) for i in spaces_tree.paths_to_leaves()]) - 2 space_depth = [] for level in range(max_depth): space_depth.append("Space " + str(level + 1)) #Create the dictionary to hold the values space_dict = {} for idx, level in enumerate(space_depth): space_dict[level] = [] #Loop through all space items for item in spaces_tree.paths_to_leaves(): for level, space in enumerate(item[2:]): res = list(space_dict.keys())[level] if str(space)[7:-6] not in space_dict[res]: space_dict[res].append(str(space)[7:-6]) return (space_dict) else: # Return the Python dictionary spaces_dict = {'spaces': []} spaces_dict['spaces'].append(spaces_tree.to_dict()) return (spaces_dict)
def do_md_section(sysid, levelid, connection_str, output_format, output_file, compact, doc_handler, dotree): """ Given the MD ids, dump the content in the output file and produce the product tree diagrams :param sysid: MagicDraw subsystem id :param levelid: MagicDraw level id (package id containing the tree to extract) :param connection_str: MagicDraw encoded connection string :param output_format: OutputFormat :param output_file: File to dump :param compact: True if the portrait diagrams have to be in compact form :param doc_handler: the document id :param sub_name: name of the subsystem :return: none """ global template_path global productTree global tree_dict productTree = Tree() products = [] tree_dict = {} # get the information from MagicDraw mdr = build_md_tree(sysid, levelid, connection_str) print("\n Product tree depth:", productTree.depth()) nodes = productTree.expand_tree(key=lambda x: x.data.index, sorting=True) for n in nodes: products.append(productTree[n].data) tree_dict[productTree[n].data.id] = productTree[n].data print(f" Found {{np}} products (including container folders).".format( np=len(tree_dict))) envs = Environment(loader=ChoiceLoader([ FileSystemLoader(Config.TEMPLATE_DIRECTORY), PackageLoader('ptree', 'templates') ]), lstrip_blocks=True, trim_blocks=True, autoescape=None) # dump a csv file if dotree: do_csv(products, output_file) # create the diagrams tex files do_trees_diagrams(productTree, output_file, products[0].shortname, compact) # define the template template_path = f"section.{Config.TEMPLATE_LANGUAGE}.jinja2" else: # no diagrams are created # use a simplified template template_path = f"notree_section.{Config.TEMPLATE_LANGUAGE}.jinja2" # sort tree dictionary based mdp = productTree.to_dict(with_data=False) # get ordered dictionary template = envs.get_template(template_path) new_mdpt = dict() for k0 in mdp: new_mdpt[k0] = order_tree_level(mdp[k0]) # dump the tex section metadata = dict() metadata["template"] = template.filename text = template.render(metadata=metadata, mdrev=mdr, filename=output_file, doc_handler=doc_handler, mdt_dict=tree_dict, mdp=new_mdpt, mdps=products) tex_file_name = output_file + ".tex" file = open(tex_file_name, "w") print(_as_output_format(text, output_format), file=file) file.close() return tree_dict
add_node(tree, None, kraken_data) previous = tree.get_node(kraken_data['ncbi_taxonomy_id']) elif kraken_data['depth'] > tree.depth(previous): add_node(tree, previous, kraken_data) previous = tree.get_node(kraken_data['ncbi_taxonomy_id']) elif kraken_data['depth'] == tree.depth(previous): add_node(tree, tree.parent(previous.identifier), kraken_data) previous = tree.get_node(kraken_data['ncbi_taxonomy_id']) elif kraken_data['depth'] < tree.depth(previous): previous_search = previous while(tree.depth(previous_search) > kraken_data['depth']): previous_search = tree.parent(previous_search.identifier) add_node(tree, tree.parent(previous_search.identifier), kraken_data) previous = tree.get_node(kraken_data['ncbi_taxonomy_id']) tree_dict = tree.to_dict(with_data=True) def transform(tree_dict): for key in tree_dict.keys(): for data_key in tree_dict[key]['data'].keys(): tree_dict[data_key] = tree_dict[key]['data'][data_key] tree_dict[key].pop('data', None) tree_dict.pop('depth', None) if 'children' in tree_dict[key]: for child in tree_dict[key]['children']: transform(child) tree_dict['children'] = tree_dict[key]['children'] tree_dict[key].pop('children', None) tree_dict.pop(key, None) tree_dict.pop('root', None)