Skip to content

Commit

Permalink
More expts on the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcampbell committed Nov 20, 2023
1 parent d773757 commit f8ad6a6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [


[project.scripts]
nsls2api = "nsls2api.cli.main:app"
nsls2api = "nsls2api.cli.cli:app"

[tool.black]
line-length = 80
Expand Down
2 changes: 2 additions & 0 deletions src/nsls2api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__app_name__ = "nsls2api"
__app_version__ = "0.0.1"
1 change: 1 addition & 0 deletions src/nsls2api/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__logged_in_username = None
10 changes: 10 additions & 0 deletions src/nsls2api/cli/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from nsls2api import __app_name__
from nsls2api.cli import cli


def main():
cli.app(prog=__app_name__)


if __name__ == "__main__":
main()
13 changes: 8 additions & 5 deletions src/nsls2api/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from fastapi import requests

BASE_URL = "http://localhost:8080"
__logged_in_username = None

global __logged_in_username

app = typer.Typer()

Expand Down Expand Up @@ -42,9 +42,9 @@ def login():
print("No API token found")
token = getpass.getpass(prompt="Please enter your API token:")
if len(token) == 0:
__logged_in_username = SpecialUsers.anonymous
globals()[__logged_in_username]: SpecialUsers = SpecialUsers.anonymous
print("Logged in as anonymous")
return __logged_in_username
return


# Let's test this token
Expand All @@ -54,14 +54,17 @@ def login():
headers = {"Authorization": f"{token}"}
response = client.get(url, headers=headers)
response.raise_for_status()
print(response.json())
globals()[__logged_in_username] = response.json()
# nsls2api.cli.__logged_in_username = response.json()
# LOGGED_IN_USERNAME = response.json()
except httpx.RequestError as exc:
print(f"An error occurred while trying to login {exc}")
raise

print("Logging in...")



@app.command()
def logout():
print("Logging you out...")
Expand All @@ -70,5 +73,5 @@ def logout():
@app.command()
def status():
print(
f"You might be logged in, or you might not be - {rich.emoji.Emoji('person_shrugging')}"
f"You might be logged in as {__logged_in_username} or {LOGGED_IN_USERNAME}, or you might not be - {rich.emoji.Emoji('person_shrugging')}"
)
23 changes: 21 additions & 2 deletions src/nsls2api/cli/main.py → src/nsls2api/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import typer

from nsls2api.cli import admin
Expand All @@ -7,6 +9,8 @@
from nsls2api.cli import facility
from nsls2api.cli import proposal

__app_name__ = "nsls2api-cli"
__version__ = "0.1.0"

app = typer.Typer()
app.add_typer(admin.app, name="admin", help="Do powerful admin level magic")
Expand All @@ -17,6 +21,21 @@
app.add_typer(proposal.app, name="proposal", help="Stuff about Proposals")


def _version_callback(value: bool) -> None:
if value:
typer.echo(f"{__app_name__} v{__version__}")
raise typer.Exit()


if __name__ == "__main__":
app()
@app.callback()
def main(
version: Optional[bool] = typer.Option(
None,
"--version",
"-v",
help="Show the application's version and exit.",
callback=_version_callback,
is_eager=True,
)
) -> None:
return
2 changes: 1 addition & 1 deletion src/nsls2api/services/proposal_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def fetch_proposals(): # -> Optional[list[ProposalSummary]]:
# proposal_list = [ProposalSummary(**p) for p in proposals]
proposal_list = []
for proposal in proposals:
proposal_summary = ProposalSummary(proposal**)
# proposal_summary = ProposalSummary(proposal**)
proposal_list.append()
return proposal_list

Expand Down

0 comments on commit f8ad6a6

Please sign in to comment.