From 2ee5237388397f52fcf16ed400b54c55664de8a0 Mon Sep 17 00:00:00 2001 From: Mattias Eggen <mattias.a.eggen@gmail.com> Date: Mon, 7 Mar 2022 10:12:02 +0100 Subject: [PATCH] tmp --- bbcli/Node.py | 17 ++++++++++++----- bbcli/__init__.py | 1 + bbcli/endpoints.py | 16 +++++++++------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/bbcli/Node.py b/bbcli/Node.py index 0a1ce93..e9f253c 100644 --- a/bbcli/Node.py +++ b/bbcli/Node.py @@ -1,5 +1,12 @@ -class Node: - def __init__(self, data, children=[]): - self.root = root - self.children = children - +class Node(object): + def __init__(self, data, children=None): + self.data = data + self.children = [] + if children is not None: + for child in children: + self.add_child(child) + def __repr__(self): + return self.name + def add_child(self, node): + assert isinstance(node, Node) + self.children.append(node) \ No newline at end of file diff --git a/bbcli/__init__.py b/bbcli/__init__.py index 75ad523..0e60c83 100644 --- a/bbcli/__init__.py +++ b/bbcli/__init__.py @@ -4,6 +4,7 @@ __app_name__ = "bbcli" __version__ = "0.1.0" from .endpoints import * +from .Node import * ( SUCCESS, diff --git a/bbcli/endpoints.py b/bbcli/endpoints.py index 124faef..6dd235a 100644 --- a/bbcli/endpoints.py +++ b/bbcli/endpoints.py @@ -7,9 +7,10 @@ import typer import click from typing import Optional from dotenv import load_dotenv -from anytree import Node, RenderTree import os +from bbcli import Node + app = typer.Typer() @@ -90,16 +91,14 @@ def get_course_contents(course_id: str = '_27251_1'): # for d in data: # typer.echo(d['title']) -def get_children(data, url, acc, count: int = 0): +def get_children(data, url, acc, count: int = 0, worklist=[]): count = count + 1 typer.echo(f'kommer hit: {count}') # print("The acc is: ", acc) key = 'hasChildren' - if key not in data or data[key] == False: - typer.echo('nei') + if len(worklist) == 0: return acc else: - typer.echo('ja') id = data['id'] old = f'{url}/{id}/children' typer.echo(url) @@ -110,8 +109,10 @@ def get_children(data, url, acc, count: int = 0): return acc else: child = response.json()['results'] + worklist.append(child) acc = acc + child - return get_children(child, url, acc, count) + parent = worklist.pop() + return get_children(parent, url, acc, count, worklist) def get_children2(d, url): key = 'hasChildren' @@ -139,7 +140,8 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course # name = data[8]['title'] # parent = Node(parent_id+name) # print(parent['id']) - res = get_children(data[8], url, []) + root = Node(data[8]) + res = get_children(root, url, []) # res = get_children2(data[2], url) for i in res: -- GitLab