Files
@ a84a39f18784
Branch filter:
Location: ChaosStuff/stockcli/stockcli/cli.py
a84a39f18784
3.3 KiB
text/x-python
Add books
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import json
import logging
import logging.config
from datetime import timedelta
from operator import itemgetter
import click
import requests
import requests_cache
from rich.panel import Panel
from rich.table import Table
from .books import add_book_by_barcode, update_isbn
from .console import DEFAULT_PADDING, console, int_prompt, prompt
from .stock import (
add_by_barcode,
get_info_by_barcode,
transfer_by_barcode,
update_by_barcode,
)
from .utils import prepare_barcode
TASK_MAP = {
"1": ("Transfer stock", transfer_by_barcode),
"2": ("Add stock", add_by_barcode),
"3": ("Update stock", update_by_barcode),
"4": ("Get product info", get_info_by_barcode),
"5": ("Add book", add_book_by_barcode),
}
@click.command(context_settings={"help_option_names": ("-h", "--help", "-?")})
@click.option(
"-c",
"--config",
"configfile",
default="/etc/stockcli.json",
type=click.File("r"),
help="Config file to load",
)
@click.pass_context
def stockcli(ctx: click.Context, configfile: click.File) -> None:
config = json.load(configfile) # type: ignore
logging.config.dictConfig(config["logging"])
ctx.ensure_object(dict)
ctx.obj["request_session"] = requests.Session()
ctx.obj["request_session"].headers.update(
{
"accept": "application/json",
"GROCY-API-KEY": config["grocy"]["apikey"],
}
)
ctx.obj["cached_session"] = requests_cache.CachedSession(
"stockcli",
ignored_parameters=["GROCY-API-KEY"],
expire_after=timedelta(days=1),
cache_control=True,
allowable_methods=["GET", "POST"],
allowable_codes=[200, 400],
match_headers=True,
stale_if_error=True,
)
ctx.obj["cached_session"].headers.update(
{
"accept": "application/json",
"GROCY-API-KEY": config["grocy"]["apikey"],
}
)
ctx.obj["base_url"] = f"{config['grocy']['url']}/api/"
menu = Table.grid(padding=DEFAULT_PADDING)
menu.add_column(justify="left", style="green", no_wrap=True)
menu.add_column(justify="left", style="cyan", no_wrap=True)
for task_id, task in sorted(TASK_MAP.items(), key=itemgetter(0)):
menu.add_row(task_id, task[0])
while True:
click.clear()
console.print(Panel(menu, title="[green bold]Menu[/green bold]"))
choice = prompt.ask(
"Enter a number to select a task", choices=list(TASK_MAP.keys())
)
logging.debug(f"User selected task: {choice}")
selected_task = TASK_MAP[choice][1]
rerun = True
while rerun:
barcode = prompt.ask("Please scan the barcode")
barcode = prepare_barcode(barcode)
try:
selected_task(barcode)
except (
requests.Timeout,
requests.ConnectionError,
requests.HTTPError,
requests.TooManyRedirects,
):
rerun = False
console.input("Continue")
else:
rerun = bool(
int_prompt.ask(
"Do the same with another product",
choices=["0", "1"],
default=0,
)
)
if __name__ == "__main__":
stockcli()
|