Example #1
0
class Compartments:
    def __init__(self, tenancy):
        self.config = tenancy.config
        self.identity = tenancy.identity
        self.report_dir = tenancy.report_dir
        self.availability_domains = get_availability_domains(
            self.identity, self.config["tenancy"])
        self.compartments = []
        self.compartment_tree = Tree()
        self.instances = []

    def get_all(self, current=None):
        comp_id = self.config["tenancy"] if current is None else current["_id"]
        """ for ad in self.availability_domains:
            self.get_instances(comp_id, ad) """
        children = self.get_compartments(comp_id)
        if len(children) is 0: return
        children = list(
            map(
                lambda curr: dict([(k, v) for (k, v) in curr.items()
                                   if (k != "attribute_map" and k !=
                                       "swagger_types")]), children))
        for child in children:
            self.compartments.append(child)
            self.get_all(child)
        return

    @backoff.on_exception(backoff.expo, oci.exceptions.ServiceError)
    def get_compartments(self, comp_id):
        return [
            c.__dict__ for c in list(
                self.identity.list_compartments(comp_id,
                                                access_level="ANY").data)
            if c.__dict__["_lifecycle_state"] != "DELETED"
        ]

    """ @backoff.on_exception(backoff.expo, oci.exceptions.ServiceError)
    def get_instances(self, comp_id, ad):
        instances = [c.__dict__ for c in list(self.compute.list_instances(
            comp_id, availability_domain=ad, lifecycle_state="RUNNING").data)]
        instances = list(map(lambda curr: dict([(k, v) for (k, v) in curr.items(
        ) if (k != "metadata" and k != "attribute_map" and k != "swagger_types")]), instances))
        self.instances.extend(instances) """

    def generate_tree(self):
        self.compartment_tree.create_node(
            f"{self.config['tenancy_name']} (root)", self.config['tenancy'])
        for compartment in self.compartments:
            self.compartment_tree.create_node(
                compartment["_name"],
                compartment["_id"],
                parent=compartment["_compartment_id"])
        print(f"******************************************************")
        print(
            f"**       {self.config['tenancy_name']} Compartment Layout        **"
        )
        print(f"******************************************************\n")
        self.compartment_tree.show()
        print("\n")
        """ filename = f"{self.config['tenancy_name']}_compartment_tree.txt"
Example #2
0
def visualizeCtree(c_tree):
    #bottom up build tree
    tree = Tree()
    tree.create_node("root", "root")
    levels = sorted(c_tree.keys())
    for level in levels:
        for node_id, cluster in c_tree[level].items():
            node_id = "{}.{}".format(level, node_id)
            tree.create_node("{}".format(cluster["pattern"]), node_id, parent="root")
            if level == 0:
                for data in cluster["data"]:
                    tree.create_node("log.{}".format(data), "log.{}".format(data), parent=node_id)
            else:
                for data in cluster["data"]:
                    tree.move_node("{}.{}".format(level-1, data), node_id)
    tree.show()
    tree.save2file("./tree")


## How to use
##1. for log in logs:updateCTree, updatePatterns
##2. get the c_tree
##3. train all the logs to get the max level and estimate the best level we keep.
##4. train the cluster on different level
##要考虑到unmerged list的同步问题, 到时production考虑将c_tree存在redis里,然后同时flush到库里
Example #3
0
    def tree_print(self):
        print("#--------Syntax Tree--------#")
        resultss = copy.deepcopy(self.result)
        root_stack = []
        tree = Tree()
        print()
        while (resultss):
            pro = resultss.pop()
            node_id = pro[0] + str(uuid.uuid4()).replace('-', '')[:9]
            if root_stack:
                parent_id = root_stack.pop()
                tree.create_node(pro[0],
                                 copy.deepcopy(node_id),
                                 parent=parent_id)
            else:
                tree.create_node(pro[0], copy.deepcopy(node_id))
            proo = pro[1:]
            proo.reverse()
            for node in proo:
                temp = node + str(uuid.uuid4()).replace('-', '')[:9]
                tree.create_node(node, temp, parent=node_id)
                if node in self.non_ts:
                    root_stack.append(temp)

        tree.show()
        print("#--------Syntax Tree--------#")
def main():
  try:
    conf = open(args.config, 'r')
    tempConf = yaml.load_all(conf)

    for line in tempConf:
      list_path = line["ListPath"]
      write_missed = line["WriteMissed"]

    pack_list_file = open(list_path, "r+")
    pack_list = json.load(pack_list_file)

    checked = check(pack_list, write_missed)

    tree = Tree()
    tree.create_node(cur_time, "root")
    generate_tree(checked, tree, "root")

    print "\n"
    tree.show()
    print "\n"

  except KeyboardInterrupt:
    print '\nThe process was interrupted by the user'
    raise SystemExit
Example #5
0
def deph_first_search(start_state, target_state):
    id = 0
    tree = Tree()
    current_node = tree.create_node(start_state, id)
    lifo_stack = []
    lifo_stack.insert(0, current_node)

    while id < 200000:
        if len(lifo_stack) == 0:
            tree.show()
            print("failed to reach the target state")
            return 1

        current_node = lifo_stack[0]
        if current_node.tag == target_state:
            tree.show()
            print("the target state " + str(current_node.tag) + " with id = " +
                  str(current_node.identifier) + "has been reached!")
            return 0

        del (lifo_stack[0])
        if not duplicate_node_path_check(tree, id):
            for elem in reachable_states(current_node.tag):
                id += 1
                new_elem = tree.create_node(elem[0],
                                            id,
                                            parent=current_node.identifier)
                lifo_stack.append(new_elem)
                print("time limit exceeded")
Example #6
0
def uniform_cost_reach2(start_state, target_state):
    id = 0
    fifo_queue = []

    tree = Tree()
    fifo_queue.append([start_state, 0, id])
    tree.create_node([start_state, 0, id], id)
    while True:
        print(fifo_queue)
        tree.show()
        if len(fifo_queue) == 0:
            print("failed to reach the target state")
            return 1
        temp = fifo_queue[0]
        if temp[0] == target_state:
            print("the target state " + temp[0] + " has been reached after " + str(temp[1]) + " kms!")
            return 0
        del (fifo_queue[0])
        if not (dupilcate_node_path_check(tree, temp[2])):
            for elem in reachable_states(temp):
                id += 1
                new_elem = [elem[0], temp[1] + elem[1], id]
                fifo_queue.append(new_elem)
                fifo_queue.sort(key=lambda x: x[1])
                tree.create_node(new_elem, id, parent=temp[2])
Example #7
0
    def print_tree(root):
        tree = Tree()
        first = True

        for dirpath, _, files in os.walk(root):
            dirpath = Path(dirpath)

            if first:
                parent = None
            else:
                parent = dirpath.parent

            tree.create_node(
                tag=f"{dirpath if first else dirpath.name}/",
                identifier=dirpath,
                parent=parent,
            )

            first = False

            for f in natsorted(files):
                filepath = dirpath / f
                tree.create_node(
                    tag=f"{filepath.name} ({time.ctime(filepath.lstat().st_mtime)})",
                    identifier=filepath,
                    parent=dirpath,
                )

        tree.show()
Example #8
0
 def test_show_data_property(self):
     new_tree = Tree()
     class Flower(object):
         def __init__(self, color):
             self.color = color
     new_tree.create_node("Jill", "jill", data=Flower("white"))
     new_tree.show(data_property="color")
Example #9
0
def lookup():
    """ """
    w0 = prompt(u'关键字:')
    level = readLevel()
    qs = db.query('select w0, w1, count(*) n from relation group by w0, w1')
    df = DataFrame(list(qs))
    tree = Tree()
    tree.create_node(w0, w0)
    appendList = []

    def append(w0, level=5):
        if w0 in appendList or level == 0:
            return
        appendList.append(w0)
        for i, row in df[df['w0'] == w0].iterrows():
            w1 = row['w1']
            n = row['n']
            # print w0, '-->', w1
            if w1 not in tree:
                title = '%s[%d]' % (w1, n)
                tree.create_node(title, w1, parent=w0)
            else:
                # 出现循环
                title = '%s[%d](*)' % (w1, n)
                tree.create_node(title, i, parent=w0)
            append(w1, level - 1)

    append(w0, level)
    tree.show()
Example #10
0
class TreePipeline(object):

    def open_spider(self, spider):
        self.tree = Tree()
        self.tree.create_node("root", "root")

    def process_item(self, item, spider):
        lst = item['text']
        lst = [x.strip() for x in [y.replace('...', '') for y in lst]]
        item['pagetitle'] = item['pagetitle'].replace('...', '')
        lst[-1] = item['pagetitle']
        for idx, elem in enumerate(lst):
            if idx == 0:
                previous = "root"
            else:
                previous = "|".join(lst[:idx])
            elem = "|".join(lst[:idx + 1])
            # elem = elem.replace('...', '')
            elem = elem.encode('utf-8').decode('utf-8')
            if not self.tree.contains(elem):
                print "Adding node %s" % elem
                self.tree.create_node(elem, elem, parent=previous)
                # self.tree.show()
        return item

    def close_spider(self, spider):
        self.tree.show()
        with open(makepath('data/cats/tree.json'), 'w') as outfile:
            outfile.write(self.tree.to_json())
        self.tree.save2file(makepath('data/cats/tree.tree'))
Example #11
0
    def create_from_project_tree(self, dtree: Tree):
        ## dispatch methods based on class
        self._project_tree = dtree

        if isinstance(self._project_tree, GDriveTree):
            self._create_from_gdrivetree()
        dtree.show(idhidden=False)
def load_from_text(filepath):
	with open(filepath,"r") as saves:
		vals = saves.readlines()

	# print(vals)
	# tree.create_node(course,course,parent="papa")
	# tree.create_node(pr,course+""+pre+""+pr,parent=course+""+pre)
	tree = Tree()
	root = vals[0].split(",")
	root_name = root[0]
	root_id = root[1].rstrip("\n")
	tree.create_node(root_name,root_id)

	i =1
	for node in vals[1:]:
		print(node)
		if(node == "END\n"):
			index = i+1
			break
		split = node.split(",")
		name = split[0]
		tree_id = split[1].rstrip("\n")
		parent = split[2].rstrip("\n")
		i+=1
		tree.create_node(name,tree_id,parent=parent)
		# time.sleep(1)
	courses_array = []
	for line in range(i+1,len(vals)):
		courses_array.append(vals[line])

	os.system('cls' if os.name == 'nt' else 'clear')
	tree.show()
	print(f"Created tree from {i} nodes")
	return tree,courses_array
def main():
    try:
        conf = open(args.config, 'r')
        tempConf = yaml.load_all(conf)

        for line in tempConf:
            list_path = line["ListPath"]
            write_missed = line["WriteMissed"]

        pack_list_file = open(list_path, "r+")
        pack_list = json.load(pack_list_file)

        checked = check(pack_list, write_missed)

        tree = Tree()
        tree.create_node(cur_time, "root")
        generate_tree(checked, tree, "root")

        print "\n"
        tree.show()
        print "\n"

    except KeyboardInterrupt:
        print '\nThe process was interrupted by the user'
        raise SystemExit
Example #14
0
def breadth_first_search2(start_state, target_state):
    id = 0
    fifo_queue = []

    tree = Tree()
    fifo_queue.append([start_state, 0, id])
    tree.create_node([start_state, 0, id], id)
    while True:
        print(fifo_queue)
        tree.show()
        if len(fifo_queue) == 0:
            print("failed to reach the target state")
            return 1
        temp = fifo_queue[0]

        if temp[0] == target_state:
            print("the target state " + temp[0] + " has been reached after " +
                  str(temp[1]) + " kms!")
            return 0
        del (fifo_queue[0])
        for elem in reachable_states(temp):
            id += 1
            new_elem = [elem[0], temp[1] + elem[1], id]
            fifo_queue.append(new_elem)
            tree.create_node(new_elem, id, parent=temp[2])
Example #15
0
class Xml2Obj:
    '''transform XML to Object'''

    def __init__(self):
        self.root = None
        self.nodeStack = []
        self.numberCount = 0
        self.tree = Tree()

    def StartElement(self, name, attributes):
        'Expat start element event handler'
        # put the element into stack and make it become child_element
        if self.nodeStack:
            parent = self.nodeStack[-1]
            # make instance of class
            element = Element(name, attributes, self.tree,
                              self.numberCount, parent.number)
            parent.addChild(element)
            self.tree = parent.getTree()
        else:
            element = self.root = Element(
                name, attributes, self.tree, self.numberCount, None)
            self.tree.create_node(self.root.name, self.root.number)
        self.numberCount += 1
        self.nodeStack.append(element)

    def EndElement(self, name):
        'Expat end element event handler'
        if self.nodeStack[-1].cdata:
            self.tree.get_node(
                self.nodeStack[-1].number).tag += self.nodeStack[-1].cdata
        self.nodeStack.pop()

    def CharacterData(self, data):
        '''Expat character data event handler'''
        if data.strip():
            element = self.nodeStack[-1]
            element.cdata += data

    def showTree(self):
        self.tree.show(key=lambda node:node.identifier)
        # In https://github.com/caesar0301/treelib/pull/180 :
        # self.tree.show(sorting=False)

    def toDot(self, file):
        self.tree.to_graphviz(file)
        # In https://github.com/caesar0301/treelib/pull/179 :
        # self.tree.to_graphviz(file, sorting=False)

    def Parse(self, filename):
        # create Expat analyzer
        Parser = expat.ParserCreate()
        # Set the Expat event handlers to our methods
        Parser.StartElementHandler = self.StartElement
        Parser.EndElementHandler = self.EndElement
        Parser.CharacterDataHandler = self.CharacterData
        # analyz XML file
        Parser.Parse(open(filename).read(), 1)
        return self.root
Example #16
0
    def configure_tree_topology(self, root, degree=2, remove=False):
        """Configures the cluster's network topology as a tree.

        The tree consists of the specified root node and the nodes,
        which build the subtrees. The childrens are incrementally chosen,
        in other words, sequentially as specified in the config file.

        Arguments:
            root {integer} -- The tree's root node.

        Keyword Arguments:
            degree {integer} -- The maximum number of children (default: {2})
            remove {boolean} -- Remove the configuration (default: {False})
        """

        self.logger.info("Configuring tree topology...")
        tree = Tree()
        root_node = self.topology.get_node(root)
        tree.create_node(root_node.name, root_node.node_id)
        parent_node = root
        for nodex in self.topology.nodes:
            if nodex.node_id == root_node.node_id:
                continue
            if len(tree.children(parent_node)) >= degree:
                if parent_node == root and root != 0:
                    parent_node = 0
                elif parent_node + 1 == root:
                    parent_node += 2
                else:
                    parent_node += 1
            tree.create_node(nodex.name, nodex.node_id, parent_node)

        self.logger.info("The following tree will be configured:")
        tree.show()

        for nodex in self.topology.nodes:
            self.logger.debug("%s:", nodex.name)
            subtree = tree.subtree(nodex.node_id)
            for nodey in self.topology.nodes:
                if nodex.node_id == nodey.node_id:
                    continue
                if subtree.contains(nodey.node_id):
                    children = tree.children(nodex.node_id)
                    for child in children:
                        if (child.identifier == nodey.node_id
                                or tree.is_ancestor(child.identifier,
                                                    nodey.node_id)):
                            nodex.add_forwarding(
                                nodey,
                                self.topology.get_node(child.identifier))
                            break
                elif tree.parent(nodex.node_id) != None:
                    nodex.add_forwarding(
                        nodey,
                        self.topology.get_node(
                            tree.parent(nodex.node_id).identifier))

        if not self.testing:
            self.topology.send_forwarding_tables(remove)
Example #17
0
def create_trees(BP, Roots, NT, n):

    for bp in BP[n-1,0]:
        if bp.name in Roots:
            print '\nTree'
            t = Tree()
            create_tree(t, bp, 0, 1)
            t.show(key=lambda x: x.identifier)
Example #18
0
def _print_graph(nodes):
    tree = Tree()
    for node in nodes:
        if node.parent_id == None:
            tree.create_node(node.id, node.id)
        else:
            tree.create_node(node.id, node.id, parent=node.parent_id)
    tree.show()
Example #19
0
def list(ctx, sport):
    """ [bookie] list the entire thing
    """
    from .ui import maplist2dict
    from tqdm import tqdm
    from treelib import Node, Tree
    tree = Tree()
    tree.create_node("sports", "root")

    def formatname(o):
        if "name" in o:
            name = o.get("name")
        elif "description" in o:
            name = o.get("description")
        else:
            name = []
        return "{} ({})".format(
            maplist2dict(name).get("en"),
            o["id"]
        )

    if sport:
        sports = [Sport(sport, peerplays_instance=ctx.peerplays)]
    else:
        sports = Sports()

    for sport in tqdm(sports):
        tree.create_node(
            formatname(sport),
            sport["id"],
            parent="root")

        for eg in tqdm(sport.eventgroups):
            tree.create_node(
                formatname(eg),
                eg["id"],
                parent=sport["id"])

            for e in tqdm(eg.events):
                tree.create_node(
                    formatname(e),
                    e["id"],
                    parent=eg["id"])

                for bmg in tqdm(e.bettingmarketgroups):
                    tree.create_node(
                        formatname(bmg),
                        bmg["id"],
                        parent=e["id"])

                    for bm in tqdm(bmg.bettingmarkets):
                        tree.create_node(
                            formatname(bm),
                            bm["id"],
                            parent=bmg["id"])

    tree.show()
Example #20
0
    def test_show_data_property(self):
        new_tree = Tree()

        class Flower(object):
            def __init__(self, color):
                self.color = color

        new_tree.create_node("Jill", "jill", data=Flower("white"))
        new_tree.show(data_property="color")
Example #21
0
 def show(
     self,
     *,
     key=lambda node: node.identifier,
     idhidden=False,
     **kwargs,
 ):
     """Show pretty tree."""
     _Tree.show(self, key=key, idhidden=idhidden, **kwargs)
Example #22
0
def show_tree_of_riad_group(riad_group, mopdb):
    from treelib import Node, Tree
    tree = Tree()

    ## combine ORG RIAD codes and GH RIAD codes in one DF
    ##  DF to be used as input to get MFI data
    riad_as_input = riad_group[["ORG_RIAD_CODE", "ORG_ORGUNIT_NAME"]].copy()
    tmp = riad_group[["GH_RIAD_CODE", "GH_ORGUNIT_NAME"]].copy()
    tmp.rename(columns={
        "GH_RIAD_CODE": "ORG_RIAD_CODE",
        "GH_ORGUNIT_NAME": "ORG_ORGUNIT_NAME"
    },
               inplace=True)
    riad_as_input = riad_as_input.append(tmp)
    riad_as_input.drop_duplicates(inplace=True)
    riad_as_input.reset_index(drop=True, inplace=True)

    mfi_obj = mfis(riad_as_input, mopdb)
    mfi_data = mfi_obj.data

    #root:
    tree.create_node(
        riad_group["GH_ORGUNIT_NAME"][0],
        riad_group["GH_RIAD_CODE"][0],
        data=mfi(
            mfi_data[mfi_data['RIAD_CODE'] == riad_group["GH_RIAD_CODE"][0]]))

    i = 0
    for index, row in riad_group.iterrows():
        i = i + 1
        #if i==500:
        #tree.show(data_property="summary",  line_type="ascii-em")
        #break
        try:
            tree.create_node(
                row["ORG_ORGUNIT_NAME"],
                row["ORG_RIAD_CODE"],
                parent=row["DP_RIAD_CODE"],
                data=mfi(
                    mfi_data[mfi_data['RIAD_CODE'] == row["ORG_RIAD_CODE"]]))
        except:
            missing_dp_id = row["DP_RIAD_CODE"]
            add_missing_node(tree, riad_group, missing_dp_id, mfi_data)

    f = open('D:/tree.txt', "w+", encoding="utf8")
    f.write("")
    f.close()
    tree.show(data_property="summary", line_type="ascii-em")
    tree.save2file(filename='D:/tree.txt',
                   data_property="summary",
                   line_type="ascii-em")
    f = open('D:/tree.txt', "r", encoding="utf8")
    contents = f.readlines()
    f.close()

    return contents
Example #23
0
def test_session_tree(beacon, capsys):
  session = beacon.get("test_session2")
  session.sessions_tree.show()
  out1, err1 = capsys.readouterr() 
  t = Tree()
  t.create_node("test_session2", "test_session2")
  t.create_node("test_session", "test_session", parent="test_session2")
  t.show()
  out2, err2 = capsys.readouterr()
  assert out1 == out2
Example #24
0
def test_session_tree(beacon, capsys):
    session = beacon.get("test_session2")
    session.sessions_tree.show()
    out1, err1 = capsys.readouterr()
    t = Tree()
    t.create_node("test_session2", "test_session2")
    t.create_node("test_session", "test_session", parent="test_session2")
    t.show()
    out2, err2 = capsys.readouterr()
    assert out1 == out2
Example #25
0
def visualise_tree_from_slice(data):
    tree = Tree()
    for index, item in data.iterrows():
        if not tree.contains(item['id']):
            if item['parent_id'] == 1 or item['parent_id'] ==2:
                tree.create_node(item['answer'], item['id'])
            else:
                tree.create_node(item['answer'], item['id'], parent=item['parent_id'])
    tree.show()
    return tree
    pass
Example #26
0
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)
Example #27
0
def Test1():
    tree = Tree()
    tree.create_node("NodeRoot", "root", data="root data",
                     parent=None)  # root node
    tree.create_node("Branch1", "node1", data="node1 data", parent="root")
    tree.create_node("Branch2", "node2", parent="root")
    tree.create_node("Branch11", "node11", parent="node1")
    tree.create_node("Branch121", "node12", parent="node1")
    tree.create_node("Branch111", "node111", parent="node12")

    data = [{
        "name":
        "root",
        "value":
        1,
        "children": [{
            "name": "root-child1",
            "value": 2
        }, {
            "name": "root-child2",
            "value": 3,
            "children": [{
                "name": "root-child2-child1",
                "value": 4
            }]
        }]
    }]
    data3 = [{
        'name':
        'A',
        'children': [{
            'name':
            'B',
            'children': [{
                'name': 'bar',
                'children': [{
                    'name': 'bar'
                }, {
                    'name': 'Bar'
                }]
            }, {
                'name': 'Bar'
            }]
        }, {
            'name': 'C'
        }]
    }]

    tree.show()
    # tree.
    # print(tree)
    store(tree, "test.dat")
    return tree
Example #28
0
    def retrieve_dependencies(self, jarName):
        if jarName is None:
            root = self.tree.get_node(self.tree.root)
            root = root.data.jarName
        else:
            root = jarName

        tgfOutput = subprocess.Popen('dosocs2 dependencies ' + root, stdout=subprocess.PIPE, shell=True)
        count = 0
        tree = Tree()
        dependencies = []
        relationships = []
        while True:
            line = tgfOutput.stdout.readline()

            if not line:
                break

            match = re.match(r"(\d+) - (.*)", line)
            if match:
                if count == 0:
                    count = count + 1
                    tree.create_node(match.group(2), match.group(1))
                else:
                    dependencies.append((match.group(2), match.group(1)))

            match = re.match(r"(\d+) (\d+)", line)

            if match:
                relationships.append((match.group(1), match.group(2)))

        if not relationships:
            print("No child relationships for " + jarName)
            return None

        while relationships:
            for item in relationships:
                node = tree.get_node(item[0])

                if node is not None:
                    rel = [item for item in relationships if int(item[0]) == int(node.identifier)]
                    if rel is not None:
                        rel = rel[0]
                        dep = [item for item in dependencies if int(item[1]) == int(rel[1])]
                        if dep is not None:
                            dep = dep[0]
                            tree.create_node(dep[0], dep[1], parent=node.identifier)
                            relationships.remove(rel)
                            dependencies.remove(dep)

        tree.show()
        if jarName is None:
            os.chdir(os.pardir)
def treeify_full_path(q: Queryable) -> None:
    tree = Tree()
    events = q.to_list()
    for event in events:
        path = list(reversed(event.data.full_path))
        for idx, node in enumerate(path):
            if tree.get_node(node):
                continue
            parent = path[idx - 1] if idx > 0 else None
            parent_node = tree.get_node(parent)
            tree.create_node(node, node, parent_node)
    tree.show(line_type="ascii-em")
Example #30
0
def collatz_tree(n):
    tree = Tree()
    tree.create_node("1", 1)
    step_list = [n]
    while (n != 1):
        n = collatz(n)
        step_list.insert(0, n)
    for i in range(1, len(step_list)):
        tree.create_node(str(step_list[i]),
                         step_list[i],
                         parent=step_list[i - 1])
    tree.show()
Example #31
0
class Chain:
    def __init__(self):
        self.root = Tree()
        self.root.create_node(0, 0)  # Genesis block

    # Aggiunge un blocco ad una catena
    def add_block(self, block):
        node = self.root.create_node(block.epoch, hash(block), block.hash)
        node.data = block

    # Stampa graficamente la catena in forma di albero
    def print_chain(self):
        self.root.show()

    # restituisce i blocchi di tutta la blockchain, sottoforma di lista
    def return_nodes_data(self):
        nodes = self.root.all_nodes()
        nodes_data = []
        for i in range(1, len(nodes)):
            nodes_data.append(nodes[i].data)
        if len(nodes_data) == 0:
            nodes_data.append(0)
        return nodes_data

    # Resituisce la lista degli identificativi (numeri) delle foglie dell'albero (catena)
    def leaves(self):
        leaves = self.root.leaves(nid=None)
        leaves_identifiers = []
        for i in range(len(leaves)):
            leaves_identifiers.append(leaves[i].identifier)
        return leaves_identifiers

    # ritorna tutte le epoche associate ai nodi della blockchain
    '''
    def return_nodes(self):
        nodes = self.root.all_nodes()
        nodes_identifiers = []
        for i in range(len(nodes)):
            nodes_identifiers.append(nodes[i].tag)
        return nodes_identifiers
    '''

    # restituisce il blocco (oggetto) con l'epoca maggiore nella Chain
    def block_max_epoch(self):
        nodes = self.root.all_nodes()
        max_epoch = 0
        block_max = 0
        for i in range(len(nodes)):
            if nodes[i].tag > max_epoch:
                max_epoch = nodes[i].tag
                block_max = nodes[i].data
        return block_max
Example #32
0
def toy_demo():
    
    tree_dict_list = []

    tree_dict_list.append({None : 'A' , 'A' : ['B', 'C'], 'B': 'D', 'C':'E',\
                            'E': ['F','G'],\
                            'D' : '1', 'F' : '2', 'G':'3'})

    tree_dict_list.append({None : 'A' , 'A' : ['B', 'C'], 'B':['D','E'],\
                            'C' : '1', 'D' : '2', 'E':'3'})
    
    tree_dict_list.append({None : 'A' , 'A' : ['B','C', 'D','E'],\
                            'B' : '1', 'C' : '2', 'D' : '3', 'E' : '4'})


    tree_dict_list.append({None : 'A' , 'A' : ['B','C', 'D'], \
                            'B' : ['E', 'F', 'G'], 'C' : ['H', 'I'],\
                            'D' : ['J', 'K', 'L', 'M'], 'E' : ['N', 'O'], \
                            'N' : '1', 'O': '2', 'F': '3', 'G': '4', 'H': '5',\
                            'I': '6', 'J' :'7', 'K': '8', 'L': '9', 'M': '0'})

    tree_dict_list.append({None : 'A' , 'A' : 'B', 'B' : ['C', 'D', 'E'],\
                            'C' : ['H', 'I'], 'D' : ['J', 'K', 'L', 'M'], \
                            'E' : ['N', 'O']})


    for tree_dict in tree_dict_list[0:1]:
        tree = Tree()
        idx = 0
        ids = {None : None}
        for parents ,children in sorted(tree_dict.items()):
            for child in children: 
                ids[child] = idx 
                tree.create_node(child, idx, parent = ids[parents], data = nRange(0,0))
                idx += 1 

        tree.show(idhidden = False)
        genRange(tree, tree.root, 0, 1)
        tree.show(data_property="mRange")

        tree_dep_dict = {ids['1'] : ids['A'], ids['2'] : ids['3'], ids['3'] : ids['1'] }
        hieght_dict =  gen_height_list(tree, tree_dep_dict)
        print hieght_dict

            #hieght_dict = {ids['1']: 3, ids['2'] : 3, ids['3']: 1 }
#        hieght_dict = {ids['1']: 1, ids['2'] : 1, ids['3']: 1, ids['4']: 2 }
#        hieght_dict = { ids['1'] : 1, ids['2'] : 3, ids['3']: 1, ids['4']: 1, \
#                        ids['5'] : 3, ids['6'] : 1, ids['7']: 1, ids['8']: 2, \
#                        ids['9'] : 1, ids['0'] : 1 }
        path_dict    = {}
        gen_suptag(tree, tree.root, hieght_dict, path_dict)
        print sorted([(tree[k].tag, v) for k,v in path_dict.items()])
Example #33
0
def _get_tree_as_string(tree: Tree) -> str:
    import sys
    from io import StringIO

    keep = sys.stdout
    msg = ""
    try:
        sys.stdout = StringIO()
        tree.show(key=lambda n: n.order)
        msg = sys.stdout.getvalue().strip()
    finally:
        sys.stdout = keep
    return msg
Example #34
0
 def run(self, args):
     tree = self._login_info_manager.tree()
     if not args[0] in tree:
         return
     tmp_tree = Tree()
     tmp_tree.create_node(args[0], '')
     has_leef = False
     for tree_node in tree.children(args[0]):
         tmp_tree.paste('', tree.subtree(tree_node.identifier))
         has_leef = True
     if not has_leef:
         tmp_tree.get_node('').tag = tree.get_node(args[0]).tag
     tmp_tree.show()
Example #35
0
def create_tree():
    # It builds the tree starting from the root node (n1) with the key 1
    tree = Tree()
    tree.create_node('root(n1)', 1, data=[keys['1']])  # root node
    for a in range(int(levels)):
        for n in range(pow(2, a + 1)):
            id = pow(2, a + 1) + n
            tree.create_node('n' + str(id),
                             id,
                             parent=int(id / 2),
                             data=[keys[str(id)]])
    tree.show()
    return tree
def build_test_tree(sequences):
	
	"""
		Args:
			sequences = a list that contanis the set of sequences. Format [[seq1], [seq2], [...]]
	"""

	tree = Tree() #test tree
	
	#add root to the tree
	node = tree.create_node('empty', 0)

	#get the root id to identify some parents nodes bellow
	root_id = node.identifier

	#just a counter to mantain a id to each node in the tree. 
	i = 0

	#sequence is as list with the inputs off each sequence 
	for sequence in sequences:

		#as tree already have one element, begin the counter at 1.
		i = i + 1

		#get the first element of the sequence
		init = sequence.pop(0)

		#add to the tree
		node = tree.create_node(init, i, parent = root_id)

		#parent to the nexts childs
		parent_id = node.identifier

		#now go through every input in the sequence. Every input will be a node into the teste tree
		for s in sequence:

			#increment the ID counter
			i = i + 1

			#add s node to the tree
			node = tree.create_node(s, i, parent = parent_id)

			#update parent for next iteration
			parent_id = node.identifier

	#to vizualize a representation of the tree in terminal uncomment the line bellow
	if verbose: tree.show()

	return tree
Example #37
0
    def test_show_data_property(self):
        new_tree = Tree()

        sys.stdout = open(os.devnull, "w")  # stops from printing to console

        try:
            new_tree.show()

            class Flower(object):
                def __init__(self, color):
                    self.color = color
            new_tree.create_node("Jill", "jill", data=Flower("white"))
            new_tree.show(data_property="color")
        finally:
            sys.stdout.close()
            sys.stdout = sys.__stdout__  # stops from printing to console
Example #38
0
def FpGrowth(fName):
    
    readFile(fName)
    Cone = getSizeOneItemSet(globOriginalList)
    priorityDict = priorityDic(Cone)
    #print(priorityDict)
    tree = Tree()   
    tree.create_node("{}", "root")
    #reconstruct the whole transction database based on the priority
    counter = 0
    for set in globOriginalList:
        temp = dict()
        for element in set:
            priority = priorityDict.get(element)
            temp.update({element:priority})
            sorted_temp = sorted(temp.items(), key=operator.itemgetter(1))
            sorted_temp.reverse()
        #print(sorted_temp)
        # construct Fp tree
        root = "root"
        for tuple in sorted_temp:
            if(not tree.contains(tuple[0])):
                tree.create_node(tuple[0], tuple[0], root, 0)
                root = tuple[0]
            else: 
                if tuple[0] in tree.is_branch(root):
                    #print("node already in this branch, don't know what to do")
                    #print("going down")
                    root = tuple[0]
                    #print(root)
                else:
                    #print("should create a duplicate node")
                    tree.create_node(tuple[0], counter, root, 0)
                    root = counter
                    counter += 1
                # I need to decide whether to create a new node or not
                # the condition is under this branch if this node exist
                # so I should check the root
    tree.show()
Example #39
0
def tree_test():
  test_titles = [
    "deletion_mapping", 
    "orthotheca", 
    "genetically_modified_sperm", 
    "category:intelligence"
  ]
  titles = dict((e, idx) for idx, e in enumerate(test_titles))

  # Tree testing
  t = Tree()
  t.create_node("deletion_mapping",15)
  t.create_node("orthotheca",14, parent=15)
  t.create_node("genetically_modified_sperm",13, parent=14)
  t.create_node("category:intelligence",12, parent=14)
  t.show()

  json = t.to_json()
  print "\nAs JSON:"
  print json

  print "\nAnd parsed back into a tree."
  t2 = from_json(json, titles)
  t2.show()
Example #40
0
def load(path):

	gtype = None
	graph = None
	nodes = []

	#TEMP: LOAD FILE FROM PATH#
	file = open(path,"r",0)
	#TEMP#

	gtype = file.next() 
	#print isInt(gtype) #ASSERT LINE 0 IS AN INTEGER
	gtype = int(gtype)
	#print gtype

	if (gtype == 0):
		graph = Tree()
		nodes = []
		print 'GRAPH TYPE: TREE'

	if (gtype == 1):
		graph = digraph()		
		print 'GRAPH TYPE: EWD'

	#print type(graph)
	for linenum,line in enumerate(file):
		if (linenum == 0): 
			parts = line.split()
			#print parts[0]

			nodeAttrs = parts[1].split(';')
			numNodeAttrs = nodeAttrs.pop(0)
			#print numNodeAttrs
			#print isInt(numNodeAttrs)

			#print numNodeAttrs, nodeAttrs
			continue

		if (linenum == 1):
			for node in line.split():
				tokens = node.split(';')
				index = tokens.pop(0)
				#print index #in final, assert numNodes == index
				#print isInt(index)
				if (isInt(index)):
					index = int(index)
					if (gtype == 0):
						nodes.append(Node(identifier=index,data=tokens))
						if (index == 0):
							#print "ZERO"
							#n = nodes[0]
							#print type(n)
							graph.add_node(nodes[0])
							#graph.show()
							#print graph.get_node(0)
					if (gtype == 1):
						graph.add_node(index, attrs=tokens)
					#print tokens
					continue

		if (linenum == 2):
			if (gtype == 0):
				#print isInt(line) #ASSERT LINE IS A SINGLE INT
				numEdges = int(line)
			if (gtype == 1):
				parts = line.split()
				#print parts[0]

				lineAttrs = parts[1].split(';')
				numLineAttrs = lineAttrs.pop(0)
				#print numLineAttrs
				#print isInt(numLineAttrs)

				#print numLineAttrs, lineAttrs

				continue

		if (linenum > 2):
			#CHECK THAT PARTS ARE INT
			parts = line.split()
			tail = int(parts[0])
			head = int(parts[1])

			if (gtype == 0):
				graph.add_node(nodes[head],tail)

			if (gtype == 1):
				attributes = parts[2].split(';')
				weight = attributes.pop(0)
				#check head and tail are integers

				#check number attributes

				#print (tail,head), weight, attributes
				graph.add_edge((tail,head),weight,attrs=attributes)

	if (gtype == 0):
		graph.show()
	if (gtype == 1):
		for node in graph.nodes():
			print node, graph.node_attr[node]
		for edge in graph.edges():
			print edge, graph.edge_weight(edge),  graph.edge_attr[edge]
 def compare_actual_folder_with_tree_with_output(self, root_path: path, expected_music_folder_tree: Tree):
     print("Expecting the following music folder:")
     expected_music_folder_tree.show()
     self.compare_actual_folder_with_tree(root_path, expected_music_folder_tree)
def create_dummy_download_folder_with_output(root_path: path, downloads_source_tree: Tree) -> path:
    print("Creating test downloads folder:")
    downloads_source_tree.show()
    created_download_root_folder = create_dummy_download_folder(root_path, downloads_source_tree)
    return created_download_root_folder
Example #43
0
class SyntaxAnalyzer:

    def __init__(self, lexemas, identifiers, constants):
        self.lexemas = lexemas
        self.identifiers = identifiers
        self.constants = constants
        self.tree = Tree()
        self.branch = []
        self.errors = []

    def pretty_print(self):
        print("\nSyntax analyzer result:")
        self.tree.show(reverse=False)
        for error in self.errors:
            print(error)

    def error(self, text):
        error = Error(text, "Syntax", self.lexeme.row, self.lexeme.column)
        self.tree.create_node(str(error), str(uuid.uuid1()), parent=self.branch[-1])
        self.errors.append(error)
        raise SyntaxAnalizerError

    def expect(self, keyword, scan=False):
        if scan:
            self.lexeme = self.lexemas.pop(0)
        if self.lexeme != keyword:
            self.error("Expected %s" % keyword.upper())

    def __getattribute__(self, name):
        # Decorate every class method
        obj = object.__getattribute__(self, name)
        excluded_methods = ['error', 'analyze', 'expect', 'pretty_print']
        if callable(obj) and obj.__name__ not in excluded_methods:
            return syntax_tree_node(obj, self)
        else:
            return obj

    def analyze(self):
        try:
            self.program()
        except SyntaxAnalizerError:
            pass

    def program(self):
        if self.lexeme == "program":
            self.procedure_identifier()
            self.expect(";")
            self.block()
            self.expect(".", scan=True)
        elif self.lexeme == "procedure":
            self.procedure_identifier()
            self.parameters_list()
            self.expect(";")
            self.block()
            self.expect(";")
        else:
            self.error("Expected PROGRAM or PROCEDURE keyword")

    def block(self):
        self.declarations(scan=False)
        self.expect("begin")
        self.statements_list()
        self.expect("end", scan=True)

    def declarations(self):
        self.constant_declarations(scan=False)
        self.variable_declarations()
        self.math_function_declarations()
        self.procedure_declaration()
        self.lexeme = self.lexemas.pop(0)

    def constant_declarations(self):
        if self.lexeme != "const":
            return "<empty>"
        self.constant_declarations_list()

    def constant_declarations_list(self):
        if self.lexemas[0] == "=":
            self.constant_declaration(scan=False)
        else:
            return "<empty>"
        while self.lexemas[1] == "=":
            self.constant_declarations_list()

    def constant_declaration(self):
        self.constant_identifier(scan=False)
        self.expect("=")
        self.constant()
        self.expect(";")

    def constant(self):
        if self.lexeme == '\'':
            self.complex_constant(scan=False)
        else:
            self.sign(scan=False)
            self.unsigned_constant()

    def variable_declarations(self):
        if self.lexeme == "var":
            self.declarations_list()
        else:
            return "<empty>"

    def declarations_list(self):
        if self.lexemas[0] == ',':
            self.declaration(scan=False)
            self.declarations_list()
        else:
            return "<empty>"

    def declaration(self):
        self.variable_identifier(scan=False)
        self.identifiers_list(scan=False)
        self.expect(":")
        self.attribute()
        self.attributes_list()

    def identifiers_list(self):
        if self.lexeme == ',':
            self.expect(',')
            self.variable_identifier()
            self.identifiers_list(scan=False)

    def attributes_list(self):
        possible_types = ['signal', 'complex', 'integer',
                          'float', 'blockfloat', 'ext', '[']
        if self.lexemas[0].value not in possible_types:
            return "<empty>"
        self.attribute()
        self.attributes_list()

    def attribute(self):
        possible_types = ['complex', 'integer', 'float',
                          'blockfloat']
        # <range>
        if self.lexeme == "[":
            self.range()
            self.expect("]")
            self.ranges_list()
        elif self.lexeme == "signal":
            return self.lexeme.value + " " + self.lexemas.pop(0).value
        elif self.lexeme.value not in possible_types:
            self.error("Wrong variable type!")
        else:
            return self.lexeme

    def ranges_list(self):
        if self.lexeme != "[":
            return "<empty>"
        self.range()
        self.ranges_list()

    def range(self):
        self.unsigned_integer(scan=False)
        self.expect("..")
        self.unsigned_integer()

    def math_function_declarations(self):
        if self.lexeme == "deffunc":
            self.function_list()
        else:
            return "<empty>"

    def function_list(self):
        if self.lexemas[0] == "=":
            self.function(scan=False)
            self.function_list()
        else:
            return "<empty>"

    def function(self):
        self.function_identifier(scan=False)
        self.expect("=")
        self.expression()
        self.function_characteristic(scan=False)
        self.expect(";")

    def function_characteristic(self):
        self.expect("\\")
        self.unsigned_integer()
        self.expect(",")
        self.unsigned_integer()

    def procedure_declaration(self):
        if self.lexeme == "procedure":
            self.procedure(scan=False)
            self.procedure_declaration()
        else:
            return "<empty>"

    def procedure(self):
        self.expect("procedure")
        self.procedure_identifier()
        self.parameters_list(scan=False)
        self.expect(";")

    def parameters_list(self):
        if self.lexeme == "(":
            self.declarations_list()
            self.expect(")", scan=True)
            self.lexeme = self.lexemas.pop(0)
        else:
            return "<empty>"

    def statements_list(self):
        if self.lexeme == "link":
            self.statement(scan=False)
            self.statements_list()
        else:
            return "<empty>"

    def statement(self):
        self.expect("link")
        self.variable_identifier()
        self.type(scan=False)
        self.unsigned_integer()
        self.expect(";")

    def type(self):
        return self.lexeme

    def complex_constant(self):
        self.expect("'")
        self.complex_number()
        self.expect("'")
        self.lexeme = self.lexemas.pop(0)

    def unsigned_constant(self):
        self.unsigned_number(scan=False)

    def complex_number(self):
        self.left_part()
        self.right_part()

    def left_part(self):
        self.expression(scan=False)

    def right_part(self):
        self.expression(scan=False)

    def expression(self):
        result = ""
        while self.lexeme != '\\':
            result += self.lexeme.value
            self.lexeme = self.lexemas.pop(0)
        return result

    def constant_identifier(self):
        self.identifier(scan=False)

    def variable_identifier(self):
        self.identifier(scan=False)

    def procedure_identifier(self):
        self.identifier(scan=False)

    def function_identifier(self):
        self.identifier(scan=False)

    def identifier(self):
        result = self.lexeme
        self.lexeme = self.lexemas.pop(0)
        return result

    def unsigned_number(self):
        self.integer_part(scan=False)
        self.fractional_part()

    def integer_part(self):
        self.unsigned_integer(scan=False)

    def fractional_part(self):
        if self.saved_lexeme == "#":
            self.sign(scan=False)
            self.unsigned_integer()
        else:
            return "<empty>"

    def unsigned_integer(self):
        result = self.lexeme
        self.lexeme = self.lexemas.pop(0)
        return result

    def sign(self):
        if self.lexeme != '-' and self.lexeme != '+':
            return "<empty>"
        return self.lexeme
Example #44
0
class WikipediaResolver(object):
    """ WikipediaResolver class
    """
    _base_wiki_url = 'https://pl.wikipedia.org'
    _base_wiki_url_search = _base_wiki_url + '/wiki/'

    def __init__(self, start_from, finish_at):
        """
        :param start_from: Wikipedia entry like "Chrześcijaństwo"
        :param finish_at: Wikipedia entry like "Biblia"
        :return: WikipediaResolver instance
        """
        self._tree = Tree()
        self._finish_at_url = requests.get(self._base_wiki_url_search + finish_at).url
        self._to_check_list = [(None, self._base_wiki_url_search + start_from)]
        self._known_resources = set()

    def solve(self):
        """
        :return: returns ordered list (start point first) of found links (steps)
        """
        while True:
            found_node = self._process_level()
            if found_node:
                break

        # logging.debug("Formating tree... This may take a while.")
        # self._show_tree()

        path = self._backtrack_path()

        return path

    def _process_level(self):
        """
        :return: Bool. True if target link was found in dispached breadth graph search iteration, else False
        """
        to_check_list = []
        while self._to_check_list:
            parent_url, url = self._to_check_list.pop(0)
            self._known_resources.add(url)

            if not parent_url:
                self._tree.create_node(url, url)  # this is root node

            set_to_check = set((self._base_wiki_url + a.attr('href') for a in self._get_a_items(requests.get(url).text) if a.attr('href')))

            set_to_check -= self._known_resources

            map((lambda _url: self._tree.create_node(_url, _url, parent=url)), set_to_check)

            if self._finish_at_url in set_to_check:
                return True

            to_check_list += [(url, _url) for _url in set_to_check]
            self._known_resources.update(set_to_check)

        self._to_check_list += to_check_list

    def _show_tree(self):
        """ This simply print current state of the tree
        :return: Nothing
        """
        self._tree.show()

    def _backtrack_path(self):
        """ Backtracks found link among the tree.
        :return: returns ordered list (start point first) of found links (steps)
        """
        node = self._tree.get_node(self._finish_at_url)
        nodes = [node]

        while node.bpointer:
            nodes.append(self._tree.get_node(node.bpointer))
            node = self._tree.get_node(node.bpointer)

        return list(reversed([node.identifier for node in nodes]))

    @staticmethod
    def _get_a_items(html):
        dom = PQ(html)  # this parses html into tree
        return dom.find('a[href^="/wiki/"]:not([href*=":"])').items()  # Neat jquery style selectors. #PDK
Example #45
0
class Route(object):
    def __init__(self, universe):
        self.route    = Tree()
        self.universe = universe
        self.max_hops = 4

    def show(self):
        self.route.show()
    
    def asString(self):
        return (','.join([self.route[node].tag for node in self.route.expand_tree(mode=Tree.DEPTH)]))

    def getRoute(self):
        return self.route
        
    def byScore_key(self, s):
        return s.score
            
    def findRoute(self, start):
        parent = self.universe.findSystem(start)
        self.route.create_node(start, start, data=parent)
        systems = self.findNextSystems(start, start) 
        self.buildRoute(systems, start)
        
        return self.route

    def buildRoute(self, systems, parent):
        for s in systems:
            n = s.name
            h = 0
            
            if (self.route.contains(n) == False):  
                self.route.create_node(n, n, parent=parent, data=s)
        
                hop = h + self.route.depth(n)
                
                if (hop < self.max_hops):
                    sub_systems = self.findNextSystems(parent, n)                        
                    self.buildRoute(sub_systems, n)
            else:
                 n = parent + ' --> ' + n
                 self.route.create_node(n, n, parent=parent, data=s)

    def getSystemId(self, name, i=0):
        if (self.route.contains(name) == False):
            return name
        else:
            i += 1
            n = name + '(' + str(i) + ')'
            return self.getSystemId(n)

    def findNextSystems(self, parent, start):
        systems = []
        optimal = self.universe.distances.findOptimalSystems(start)
        
        for s in sorted(set(optimal)):
            if (s != parent):
                i = self.universe.findSystem(s)
                if (i.permit == False):
                    systems.append(i)
            
        s = sorted(systems, key = self.byScore_key)

        return s[:self.max_hops]

# http://xiaming.me/treelib/examples.html
#
# class SystemTree(object):
#     def __init__(self):
#         self.tree = Tree()
#         
#     def addNode(self, id, o):
#         self.tree.create_node(o, id)
#         
#     def addChildNode(self, p, id, o):
#         self.tree.create_node(o, id, parent=p)
#     
#     def getNode(self, id):
#         return self.tree.subtree(id)
#         
#     def __repr__(self):
#         return self.tree.to_json(with_data=True)
# 
# 
# t = SystemTree()
# t.addNode('Aerial', 'Aerial')
# t.addChildNode('Aerial', 'Jotun', 'Jotun')
# t.addChildNode('Jotun', 'Rusani', 'Rusani')
# n = t.getNode('Jotun')
# print(n)
# n = t.tree.contains('Invalid')
# print(n)
# t.tree.show()
Example #46
0
from treelib import Tree, Node

## Create the family tree
tree = Tree()
tree.create_node("Harry", "harry")  # root node
tree.create_node("Jane", "jane", parent="harry")
tree.create_node("Bill", "bill", parent="harry")
tree.create_node("Diane", "diane", parent="jane")
tree.create_node("George", "george", parent="diane")
tree.create_node("Mary", "mary", parent="diane")
tree.create_node("Jill", "jill", parent="george")
tree.create_node("Mark", "mark", parent="jane")

print("#"*4 + "Breakdown of out family")
tree.show()
print('\n') 

print("#"*4 + "All family members in DEPTH mode")
for node in tree.expand_tree(mode=Tree.DEPTH):
    print tree[node].tag
print('\n') 

print("#"*4 + "All family members without Diane sub-family")
for node in tree.expand_tree(filter=lambda x: x != 'diane', mode=Tree.DEPTH):
    print tree[node].tag
print('\n') 

print("#"*4 + "Let me introduce Diane family only")
sub_t = tree.subtree('diane')
sub_t.show()
class WarcFileSystem( LoggingMixIn, Operations ):
	"""Filesystem built on a WARC's URI paths."""
	def __init__( self, warc ):
		self.warc = warc
		logger.debug( "Mounting %s" % self.warc )
		self.fh = WarcRecord.open_archive( warc, gzip="auto", mode="rb" )
		self.tree = Tree()
		self._get_records()

	def _get_records( self ):
		"""Parses a WARC, building a hierarchical tree."""
		statinfo = os.stat( self.warc )
		self.gid = statinfo.st_gid
		self.uid = statinfo.st_uid
		self.tree.create_node( self.warc, "/" )
		self.records = {}
		bar = progressbar.ProgressBar( maxval=statinfo.st_size, widgets=[ progressbar.Bar( "=", "[", "]"), " ", progressbar.Percentage() ] )
		bar.start()
		for( offset, record, errors ) in self.fh.read_records( limit=None ):
			if record is not None and record.type != WarcRecord.WARCINFO:
				parent = "/"
				segments = [ record.type ] + re.split( "/+", record.url )
				for e in segments:
					identifier = "/".join( [ parent, e ] )
					if not self.tree.contains( identifier ):
						node = WarcRecordNode( record, offset, tag=e, identifier=identifier )
						self.tree.add_node( node, parent=parent )
					parent = identifier
				self.records[ record.url ] = ( offset, record )
				bar.update( offset )
		bar.finish()
		logger.debug( self.tree.show() )

#	def access( self, path, amode ):
#		logger.debug( path )
#		raise FuseOSError( EPERM )

	def chmod( self, path, mode ):
		raise FuseOSError( EPERM )

	def chown( self, path, uid, gid ):
		raise FuseOSError( EPERM )

	def create( self, path, mode ):
		raise FuseOSError( EPERM )

	def destroy( self, path ):
		self.fh.close()

#	def flush( self, path, fh ):
#		raise FuseOSError( EPERM )

	def fsync( self, path, datasync, fh ):
		raise FuseOSError( EPERM )
		
	def fsyncdir( self, path, datasync, fh ):
		raise FuseOSError( EPERM )

	def getattr( self, path, fh=None ):
		"""Returns stat info for a path in the tree."""
		logger.debug( path )
		if path == "/":
			stat = os.stat( self.warc )
			return dict( [
				( "st_mode", ( S_IFDIR | 0444 ) ),
				( "st_ino", stat.st_ino ),
				( "st_dev", stat.st_dev ),
				( "st_nlink", stat.st_nlink ),
				( "st_uid", stat.st_uid ),
				( "st_gid", stat.st_gid ),
				( "st_size", stat.st_size ),
				( "st_ctime", stat.st_ctime ),
				( "st_mtime", stat.st_mtime ),
				( "st_atime", stat.st_atime )
			] )
		else:
			return self.name_to_attrs( "/%s" % path )

	def getxattr( self, path, name, position=0 ):
		"""Returns the value for an extended attribute."""
		if path != "/":
			path = "/%s" % path

		node = self.tree.get_node( path )
		if node is None:
			raise FuseOSError( ENOENT )

		try:
			return node.xattrs[ name ]
		except KeyError:
			raise FuseOSError( ENODATA )

	def init( self, path ):
		pass

	def link( self, target, source ):
		raise FuseOSError( EPERM )

	def listxattr( self, path ):
		"""Returns a list of extended attribute names."""
		if path != "/":
			path = "/%s" % path

		node = self.tree.get_node( path )
		if node is None:
			raise FuseOSError( ENOENT )
		return node.xattrs.keys()

	def mkdir( self, path, mode ):
		raise FuseOSError( EPERM )

	def mknod( self, path, mode, dev ):
		raise FuseOSError( EPERM )

	def open( self, path, flags ):
		"""Should return numeric filehandle; returns file offset for convenience."""
		if path != "/":
			path = "/%s" % path

		node = self.tree.get_node( path )
		if node is None:
			raise FuseOSError( ENOENT )

		return node.offset

#	def opendir( self, path ):
#		raise FuseOSError( EPERM )

	def read( self, path, size, offset, fh ):
		"""Reads 'size' data from 'path', starting at 'offset'."""
		logger.debug( "read %s from %s at %s " % ( size, path, offset ) )

		if path != "/":
			path = "/%s" % path

		node = self.tree.get_node( path )
		if node is None:
			raise FuseOSError( ENOENT )

		offset += node.payload_offset
		mime, data = node.record.content
		end = offset + size
		return data[ offset:end ]

	def name_to_attrs( self, name ):
		"""Retrieves attrs for a path name."""
		logger.debug( name )
		node = self.tree.get_node( name )
		if node is None:
			raise FuseOSError( ENOENT )

		if node.is_leaf():
			st_mode = ( S_IFREG | 0444 )
			size = node.record.content_length
			try:
				timestamp = time.mktime( parse( node.record.date ).timetuple() )
			except ValueError as v:
				logger.warning( "Error parsing time: %s [%s]" % ( node.record.date, str( v ) ) )
				timestamp = time.mktime( datetime.fromtimestamp( 0 ).timetuple() )
		else:
			st_mode = ( S_IFDIR | 0555 )
			size = 0
			timestamp = time.time()
		return dict( [
			( "st_mode", st_mode ),
			( "st_ino", 0 ),
			( "st_dev", 0 ),
			( "st_nlink", 0 ),
			( "st_uid", self.uid ),
			( "st_gid", self.gid ),
			( "st_size", size ), 
			( "st_ctime", timestamp ),
			( "st_mtime", timestamp ),
			( "st_atime", timestamp )
		] )

	def readdir( self, path, fh ):
		"""Returns a tuple of all files in path."""
		logger.debug( path )
		if path != "/":
			path = "/%s" % path
		if self.tree.contains( path ):
			names = []

			for c in self.tree.get_node( path ).fpointer:
				child = self.tree.get_node( c )
				names.append( ( child.tag, self.name_to_attrs( child.identifier ), 0  ) )
			return names
		else:
			raise FuseOSError( ENOENT )

	def readlink( self, path ):
		raise FuseOSError( EPERM )

#	def release( self, path, fh ):
#		raise FuseOSError( EPERM )

#	def releasedir( self, path, fh ):
#		raise FuseOSError( EPERM )

	def removexattr( self, path, name ):
		raise FuseOSError( EPERM )

	def rename( self, old, new ):
		raise FuseOSError( EPERM )

	def rmdir( self, path ):
		raise FuseOSError( EPERM )

	def setxattr( self, path, name, value, options, position=0 ):
		raise FuseOSError( EPERM )

	def statfs( self, path ):
		raise FuseOSError( EPERM )

	def symlink( self, target, source ):
		raise FuseOSError( EPERM )

	def truncate( self, path, length, fh=None ):
		raise FuseOSError( EPERM )

	def unlink( self, path ):
		raise FuseOSError( EPERM )

	def utimens( self, path, times=None ):
		raise FuseOSError( EPERM )

	def write( self, path, data, offset, fh ):
		raise FuseOSError( EPERM )
Example #48
0
## Create the family tree
tree = Tree()
tree.create_node("Harry", "harry")  # root node
tree.create_node("Jane", "jane", parent="harry")
tree.create_node("Bill", "bill", parent="harry")
tree.create_node("Diane", "diane", parent="jane")
tree.create_node("George", "george", parent="diane")
tree.create_node("Mary", "mary", parent="diane")
tree.create_node("Jill", "jill", parent="george")
tree.create_node("Mark", "mark", parent="jane")

sep = "-" * 20 + "\n"

print(sep + "Tree of the whole family:")
tree.show(key=lambda x: x.tag, reverse=True)

print(sep + "All family members in DEPTH mode:")
for node in tree.expand_tree(mode=Tree.DEPTH):
    print(tree[node].tag)

print(sep + "All family members without Diane sub-family:")
tree.show(idhidden=False, filter=lambda x: x.identifier != "diane")
# for node in tree.expand_tree(filter=lambda x: x.identifier != 'diane', mode=Tree.DEPTH):
#     print tree[node].tag

print(sep + "Let me introduce Diane family only:")
sub_t = tree.subtree("diane")
sub_t.show()

print(sep + "Children of Diane")
Example #49
0
from treelib import Tree, Node

## Create the family tree
tree = Tree()
tree.create_node("Harry", "harry")  # root node
tree.create_node("Jane", "jane", parent="harry")
tree.create_node("Bill", "bill", parent="harry")
tree.create_node("Diane", "diane", parent="jane")
tree.create_node("George", "george", parent="diane")
tree.create_node("Mary", "mary", parent="diane")
tree.create_node("Jill", "jill", parent="george")
tree.create_node("Mark", "mark", parent="jane")


print ("#" * 4 + "Breakdown of out family")
tree.show(cmp=lambda x, y: cmp(x.tag, y.tag), key=None, reverse=True)
# tree.show(key=lambda x: x.tag, reverse=False)
tree.save2file("/home/chenxm/Desktop/tree.txt", idhidden=False)
print ("\n")


print ("#" * 4 + "All family members in DEPTH mode")
for node in tree.expand_tree(mode=Tree.ZIGZAG):
    print tree[node].tag
print ("\n")


print ("#" * 4 + "All family members without Diane sub-family")
tree.show(idhidden=False, filter=lambda x: x.identifier != "diane")
# for node in tree.expand_tree(filter=lambda x: x.identifier != 'diane', mode=Tree.DEPTH):
#     print tree[node].tag