#!/usr/bin/env python import typer import os import httpx import rich #from rich.syntax import Syntax import pygments.lexers import pygments.formatters def post(cmd: str, **data) -> httpx.Response: return httpx.post("https://www.pvv.ntnu.no/~andreasd/cards/command.php", data=dict( access_token = ACCESS_TOKEN, cmd = cmd, **data )) def print_code(html: str, lexer: str): #rich.print(Syntax(html, "html")) # is shit if not os.isatty(1): print( html ) else: print( pygments.highlight( html, pygments.lexers.get_lexer_by_name(lexer), pygments.formatters.TerminalFormatter(), #pygments.formatters.Terminal256Formatter(), #pygments.formatters.TerminalTrueColorFormatter(), ) ) app = typer.Typer( no_args_is_help = True, ) @app.command() def get_card_groups(): rich.print_json(data=post("get_my_cardgroups").json()) @app.command() def get_card_group(group_id: int): rich.print_json(data=post("get_cards_in_group", **locals()).json()) @app.command() def get_card_rendered(card_id: int): print_code(post("get_card_rendered", **locals()).text, "html") @app.command() def get_card(card_id: int): print_code(post("get_card_xml", **locals()).text, "xml") @app.command() def set_card(card_id: int, file: typer.FileText): value = file.read() del file print_code(post("set_card_xml", **locals()).text, "html") @app.command() def get_styles(): # this one fails due to trailing comma rich.print_json(data=post("get_my_styles").json()) #rich.print(post("get_my_styles").text) @app.command() def get_style(style_id: int): print_code(post("get_style_data", **locals()).text, "html") @app.command() def get_style_dimensions(style_id: int): rich.print_json(data=post("get_style_dimensions", **locals()).json()) @app.command() def set_style(style_id: int, file: typer.FileText): value = file.read() del file print_code(post("set_style_data", **locals()).text, "html") if __name__ == "__main__": if "CARDS_ACCESS_TOKEN" not in os.environ: print("ERROR: environment variable 'CARDS_ACCESS_TOKEN' not set") exit(1) ACCESS_TOKEN = os.environ["CARDS_ACCESS_TOKEN"] app()