Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeerArsala committed Oct 13, 2024
1 parent 66e4687 commit a1c21cb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
3 changes: 2 additions & 1 deletion ranlibx/authentication.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 14 additions & 15 deletions ranlibx/cli/main.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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)

Expand All @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions ranlibx/flow.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
9 changes: 4 additions & 5 deletions ranlibx/server.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions ranlibx/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a1c21cb

Please sign in to comment.