From a1c21cb27bbe957d3f1bf2d10f8b2fa3d01afd90 Mon Sep 17 00:00:00 2001 From: Ameer Arsala Date: Sun, 13 Oct 2024 13:14:42 -0700 Subject: [PATCH] formatting --- ranlibx/authentication.py | 3 ++- ranlibx/cli/main.py | 29 ++++++++++++++--------------- ranlibx/flow.py | 9 +++++---- ranlibx/server.py | 9 ++++----- ranlibx/state.py | 2 ++ 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/ranlibx/authentication.py b/ranlibx/authentication.py index 5f69460..d148349 100644 --- a/ranlibx/authentication.py +++ b/ranlibx/authentication.py @@ -1,6 +1,7 @@ import json -import typer + import httpx +import typer from ranlibx.api.schemas.token import AuthToken from ranlibx.constants import RAN_API_SERVER_URL, RAN_AUTH_TOKEN_FILEPATH_JSON diff --git a/ranlibx/cli/main.py b/ranlibx/cli/main.py index 8b31f34..7d88b37 100644 --- a/ranlibx/cli/main.py +++ b/ranlibx/cli/main.py @@ -1,16 +1,14 @@ +import time + import typer import uvicorn -from ranlibx import authentication, server, state, flow - +from ranlibx import authentication, flow, server, state from ranlibx.api.schemas.token import AuthToken from ranlibx.cli.subcmds import install from ranlibx.server import UvicornServerProcess from ranlibx.state import AuthFlowState -import time - - # CLI App app = typer.Typer(rich_markup_mode="rich") @@ -30,10 +28,7 @@ def open_auth_server(host: str = "127.0.0.1", port: int = 8000, verbose: bool = # Create the server config = uvicorn.Config( - "ranlibx.api.main:app", - host=host, - port=port, - log_level=("info" if verbose else "critical") + "ranlibx.api.main:app", host=host, port=port, log_level=("info" if verbose else "critical") ) fastapi_server = uvicorn.Server(config) @@ -43,29 +38,33 @@ def open_auth_server(host: str = "127.0.0.1", port: int = 8000, verbose: bool = # Start it server.active_uvicorn_server_process.start(verbose=verbose) - + # Send a message in terminal telling the user to go to the browser cli login typer.echo("Go to https://ran.so/login/cli to log in (you'll come back here, dw)") # Stall while in progress browser_auth_successful: bool = flow.wait_for_browser_auth(verbose=verbose) - + # Kill server state.kill_server(verbose=verbose) - + if not browser_auth_successful: # Tell the user to paste in their API Token - typer.echo("Authentication Failed. Fear not, just paste in your API token if something went wrong (it shows in the browser)") + typer.echo( + "Authentication Failed. Fear not, just paste in your API token if something went wrong (it shows in the browser)" + ) # Do it manually manual_auth_successful: bool = flow.await_manual_api_token_auth() - + if not manual_auth_successful: typer.echo("Max tries reached. Auth unsuccessful. Just try again") return # "Yay! We are done and the user is logged in!" - typer.echo("You have successfully logged into RAN!\n:rocket: [orange]Skyrocket[/orange] your Research from Theory to Experiment") + typer.echo( + "You have successfully logged into RAN!\n:rocket: [orange]Skyrocket[/orange] your Research from Theory to Experiment" + ) @app.command() diff --git a/ranlibx/flow.py b/ranlibx/flow.py index 7b22b0d..721ce98 100644 --- a/ranlibx/flow.py +++ b/ranlibx/flow.py @@ -1,7 +1,8 @@ import time + import typer -from ranlibx import state, authentication +from ranlibx import authentication, state from ranlibx.api.schemas.token import AuthToken from ranlibx.state import AuthFlowState @@ -14,7 +15,7 @@ def wait_for_browser_auth(verbose: bool = False) -> bool: time.sleep(0.75) if verbose: typer.echo(f"Stalling x{(i := i + 1)}") - + # On exit success: bool = state.get_auth_flow_state() == AuthFlowState.SUCCESS @@ -24,14 +25,14 @@ def wait_for_browser_auth(verbose: bool = False) -> bool: # -1 max_tries = infinity def await_manual_api_token_auth(max_tries: int = -1) -> bool: i: int = 0 - + # Loop until they get it right while True: api_token: str = str(typer.prompt("Paste your API Token here:")) # 2. Authenticate the API Token success: bool = authentication.authenticate(AuthToken(token=api_token)) - + if success: return True diff --git a/ranlibx/server.py b/ranlibx/server.py index 7d74364..31fc847 100644 --- a/ranlibx/server.py +++ b/ranlibx/server.py @@ -1,11 +1,10 @@ -from typing import Optional, Union - import asyncio import threading import time +from typing import Optional, Union -import uvicorn import typer +import uvicorn def stop_server(userver: uvicorn.Server): @@ -22,10 +21,10 @@ def __init__(self, server: uvicorn.Server): def start(self, verbose: bool = False): # Set it self.server_thread = threading.Thread(target=self.server.run) - + # Start it self.server_thread.start() - + # not sure why this has to be included but it works and breaks without it time.sleep(0.5) diff --git a/ranlibx/state.py b/ranlibx/state.py index 7a6e7b6..dd2ce68 100644 --- a/ranlibx/state.py +++ b/ranlibx/state.py @@ -14,9 +14,11 @@ class AuthFlowState(Enum): # Getters and Setters + def get_auth_flow_state() -> AuthFlowState: return _auth_flow_state + # Yes, this IS necessary to set the actual variable above from the outside def set_auth_flow_state(st: AuthFlowState): global _auth_flow_state