From 54f63432bf58e931b0f62365ab97d97aa45bc0f9 Mon Sep 17 00:00:00 2001 From: williamforbrigd <hansw0701@gmail.com> Date: Thu, 17 Mar 2022 12:03:35 +0100 Subject: [PATCH] switched from typer to click and added setup file --- bbcli/__init__.py | 1 + bbcli/__main__.py | 9 ++-- bbcli/cli.py | 100 ++++++++++++++++++++++++++------------------- bbcli/endpoints.py | 52 +++++++++++------------ setup.py | 15 +++++++ venv/pyvenv.cfg | 4 +- 6 files changed, 107 insertions(+), 74 deletions(-) create mode 100644 setup.py diff --git a/bbcli/__init__.py b/bbcli/__init__.py index f4593cb..0b930ff 100644 --- a/bbcli/__init__.py +++ b/bbcli/__init__.py @@ -7,6 +7,7 @@ __version__ = "0.1.0" from .Node import * from .Utils.utils import * from .Services import login +# from endpoints import get_user, get_course, get_assignments, get_course_contents ( SUCCESS, diff --git a/bbcli/__main__.py b/bbcli/__main__.py index 59b707a..81cea9a 100644 --- a/bbcli/__main__.py +++ b/bbcli/__main__.py @@ -1,10 +1,9 @@ -"""RP To-Do entry point script.""" -# rptodo/__main__.py - -from bbcli import cli, __app_name__ +from bbcli import __app_name__ +from .cli import entry_point def main(): - cli.app(prog_name=__app_name__) + entry_point() + if __name__ == "__main__": main() \ No newline at end of file diff --git a/bbcli/cli.py b/bbcli/cli.py index be155ce..5b780cc 100644 --- a/bbcli/cli.py +++ b/bbcli/cli.py @@ -1,66 +1,82 @@ from typing import Optional -import typer -from bbcli import __app_name__, __version__, endpoints +from pkg_resources import EntryPoint +# import typer +from bbcli import __app_name__, __version__ +from bbcli.endpoints import get_user, get_course, get_course_contents, get_assignments import os from dotenv import load_dotenv -from bbcli import login, check_valid_date +from bbcli import check_valid_date +import click from bbcli.Services import AuthorizationService -app = typer.Typer() -app.add_typer(endpoints.app, name='endpoints', help='Call the endpoints') +@click.group() +def entry_point(): + authorize_user() + pass + +entry_point.add_command(get_user) +entry_point.add_command(get_course) +entry_point.add_command(get_course_contents) +entry_point.add_command(get_assignments) + + +# app = typer.Typer() +# app.add_typer(endpoints.app, name='endpoints', help='Call the endpoints') load_dotenv() cookies = {'BbRouter' : os.getenv("BB_ROUTER")} headers = {'X-Blackboard-XSRF': os.getenv('XSRF')} -def _version_callback(value: bool) -> None: - if value: - typer.echo(f'{__app_name__} v{__version__}') - raise typer.Exit() - -@app.callback() -def main( - version: Optional[bool] = typer.Option( - None, - '--version', - '-v', - help='Show the applications version and exit.', - callback=_version_callback, - is_eager=True, - ) -) -> None: - if check_valid_date(cookies) == False: - AuthorizationService.login() - # load_dotenv() - # cookies['BbRouter'] = os.getenv("BB_ROUTER") - # headers['X-Blackboard-XSRF'] = os.getenv("XSRF") - return +# def _version_callback(value: bool) -> None: +# if value: +# typer.echo(f'{__app_name__} v{__version__}') +# raise typer.Exit() + +# @app.callback() +# def main( +# version: Optional[bool] = typer.Option( +# None, +# '--version', +# '-v', +# help='Show the applications version and exit.', +# callback=_version_callback, +# is_eager=True, +# ) +# ) -> None: +# if check_valid_date(cookies) == False: +# AuthorizationService.login() +# # load_dotenv() +# # cookies['BbRouter'] = os.getenv("BB_ROUTER") +# # headers['X-Blackboard-XSRF'] = os.getenv("XSRF") +# return + #----- AUTHORIZATION MODULE -----# -@app.command(name='login', help='Authorize the user.') +# @app.command(name='login', help='Authorize the user.') def authorize_user(): - AuthorizationService.login() + if check_valid_date(cookies) == False: + AuthorizationService.login() #----- COURSE MODULE -----# -@app.command(name='course') -def course( - course_id: Optional[str] = typer.Argument(None, help='The id of the course you want.'), - favorites: bool = typer.Option(False, help='List only your favorite courses.')): - if course_id != None and favorites == False: - # CODE FOR GETTING SPESIFIC COURSE +# @app.command(name='course') +# def course( +# course_id: Optional[str] = typer.Argument(None, help='The id of the course you want.'), +# favorites: bool = typer.Option(False, help='List only your favorite courses.')): +# if course_id != None and favorites == False: +# # CODE FOR GETTING SPESIFIC COURSE - print('getting spesific course...') - elif course_id != None and favorites == True: - # CODE FOR GETTING SPESIFIC FAVORITE COURSE +# print('getting spesific course...') +# elif course_id != None and favorites == True: +# # CODE FOR GETTING SPESIFIC FAVORITE COURSE - print('getting spesific favorite course...') - else: - # CODE FOR GETTING ALL COURSES +# print('getting spesific favorite course...') +# else: +# # CODE FOR GETTING ALL COURSES - print('getting all courses...') \ No newline at end of file +# print('getting all courses...') \ No newline at end of file diff --git a/bbcli/endpoints.py b/bbcli/endpoints.py index 618c5c5..5e9946c 100644 --- a/bbcli/endpoints.py +++ b/bbcli/endpoints.py @@ -2,31 +2,30 @@ import requests #from requests.auth import HTTPBasicAuth # import json # import pprint -import typer import bbcli.cli as cli #from string_builder import StringBuilder import click -from typing import Optional -from dotenv import load_dotenv +# from typing import Optional +# from dotenv import load_dotenv # from anytree import Node, RenderTree import os -app = typer.Typer() from bbcli import check_response base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/' -@app.command(name='get-user') -def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> None: +@click.command(name='get-user') +@click.argument('user_name', default='') +def get_user(user_name: str): ''' Get the user Specify the user_name as an option, or else it will use the default user_name ''' if user_name == '': - user_name = typer.prompt("What is your user name?") + user_name = click.prompt("What is your user name?") url = f'{base_url}users?userName={user_name}' response = requests.get( url, @@ -35,22 +34,23 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non if check_response(response) == False: return else: - data = x.json()['results'][0] + data = response.json()['results'][0] fn = data['name']['given'] sn = data['name']['family'] id = data['studentId'] - typer.echo(f'Name of the student: {fn} {sn}') - typer.echo(f'The student id: {id}') + click.echo(f'Name of the student: {fn} {sn}') + click.echo(f'The student id: {id}') -@app.command(name='get-course') -def get_course(course_id: str = typer.Argument('', help='Id of the course')): +@click.command(name='get-course') +@click.argument('course_id', default='_27251_1') +def get_course(course_id: str): ''' Get the course ''' if course_id == '': - course_id = typer.prompt("What is the course id?") + course_id = click.prompt("What is the course id?") url = f'{base_url}courses?courseId={course_id}' response = requests.get( url, @@ -58,35 +58,36 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')): if check_response(response) == False: return else: - data = x.json()['results'][0] + data = response.json()['results'][0] name = data['name'] course_url = data['externalAccessUrl'] - typer.echo(name) - typer.echo(f'URL for the course: {course_url}') + click.echo(name) + click.echo(f'URL for the course: {course_url}') -@app.command(name='get-course-contents') -def get_course_contents(course_id: str = '_27251_1'): +@click.command(name='get-course-contents') +@click.argument('course_id', default='_27251_1') +def get_course_contents(course_id: str): ''' Get the course contents ''' url = f'{base_url}courses/{course_id}/contents' - typer.echo(url) + click.echo(url) response = requests.get(url, cookies=cli.cookies) if check_response(response) == False: return else: data = response.json()['results'] - typer.echo('Mapper:') + click.echo('Mapper:') map = dict() for i in range(len(data)): title = data[i]['title'] map[i+1] = data[i]['id'] - typer.echo(f'{i+1} {title}') - typer.echo(map) + click.echo(f'{i+1} {title}') + click.echo(map) def get_children(worklist, url, acc, count: int = 0): count = count + 1 - typer.echo(f'kommer hit: {count}') + click.echo(f'kommer hit: {count}') key = 'hasChildren' if len(worklist) == 0: return acc @@ -108,8 +109,9 @@ def get_children(worklist, url, acc, count: int = 0): -@app.command(name='get-assignments') -def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course id')): +@click.command(name='get-assignments') +@click.argument('course_id', default='_27251_1') +def get_assignments(course_id: str): ''' Get the assignments ''' diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..506f636 --- /dev/null +++ b/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup + +setup( + name='cli', + # version='0.1.0', + # py_modules=['yourscript'], + install_requires=[ + 'Click', + ], + entry_points={ + 'console_scripts': [ + 'bb=bbcli.__main__:main', # the cli() function runs inside the bbcli.py. cli is the command that is used to run the command + ], + }, +) \ No newline at end of file diff --git a/venv/pyvenv.cfg b/venv/pyvenv.cfg index 853404e..f53dac3 100644 --- a/venv/pyvenv.cfg +++ b/venv/pyvenv.cfg @@ -1,3 +1,3 @@ -home = /usr/bin +home = /Library/Frameworks/Python.framework/Versions/3.9/bin include-system-site-packages = false -version = 3.8.10 +version = 3.9.10 -- GitLab