From ce4c23cd6b5a7911672a197a55964224ed849d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Mon, 2 Dec 2024 15:17:31 +0100 Subject: [PATCH] bin/: use python typing --- bin/yunohost | 16 +++++++++------- bin/yunohost-api | 2 +- bin/yunohost-portal-api | 2 +- bin/yunomdns | 8 ++++---- bin/yunopaste | 12 ++++++------ 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/bin/yunohost b/bin/yunohost index 2a48681813..0f92b65594 100755 --- a/bin/yunohost +++ b/bin/yunohost @@ -25,7 +25,7 @@ import sys import yunohost -def _parse_cli_args(): +def _parse_cli_args() -> tuple[argparse.ArgumentParser, argparse.Namespace, list[str]]: """Parse additional arguments for the cli""" parser = argparse.ArgumentParser(add_help=False) parser.add_argument( @@ -71,7 +71,7 @@ def _parse_cli_args(): elif opts.json: opts.output_as = "json" - return (parser, opts, args) + return parser, opts, args # Stupid PATH management because sometimes (e.g. some cron job) PATH is only /usr/bin:/bin ... @@ -82,12 +82,10 @@ if os.environ["PATH"] != default_path: # Main action ---------------------------------------------------------- -if __name__ == "__main__": + +def main() -> None: if os.geteuid() != 0: - sys.stderr.write( - "\033[1;31mError:\033[0m yunohost command must be " - "run as root or with sudo.\n" - ) + print("\033[1;31mError:\033[0m yunohost command must be run as root or with sudo.", file=sys.stderr) sys.exit(1) parser, opts, args = _parse_cli_args() @@ -104,3 +102,7 @@ if __name__ == "__main__": args=args, parser=parser, ) + + +if __name__ == "__main__": + main() diff --git a/bin/yunohost-api b/bin/yunohost-api index 5fb9289703..fe6b00282b 100755 --- a/bin/yunohost-api +++ b/bin/yunohost-api @@ -27,7 +27,7 @@ DEFAULT_HOST = "localhost" DEFAULT_PORT = 6787 -def _parse_api_args(): +def _parse_api_args() -> argparse.Namespace: """Parse main arguments for the api""" parser = argparse.ArgumentParser( add_help=False, diff --git a/bin/yunohost-portal-api b/bin/yunohost-portal-api index 7356abeee3..d01aed8f1f 100755 --- a/bin/yunohost-portal-api +++ b/bin/yunohost-portal-api @@ -27,7 +27,7 @@ DEFAULT_HOST = "localhost" DEFAULT_PORT = 6788 -def _parse_api_args(): +def _parse_api_args() -> argparse.Namespace: """Parse main arguments for the api""" parser = argparse.ArgumentParser( add_help=False, diff --git a/bin/yunomdns b/bin/yunomdns index 28cd7e644e..c3cfb7caad 100755 --- a/bin/yunomdns +++ b/bin/yunomdns @@ -63,17 +63,17 @@ def get_network_local_interfaces() -> Dict[str, Dict[str, List[str]]]: # Listener class, to detect duplicates on the network # Stores the list of servers in its list property class Listener: - def __init__(self): + def __init__(self) -> None: self.list = [] - def remove_service(self, zeroconf, type, name): + def remove_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: info = zeroconf.get_service_info(type, name) self.list.remove(info.server) - def update_service(self, zeroconf, type, name): + def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: pass - def add_service(self, zeroconf, type, name): + def add_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: info = zeroconf.get_service_info(type, name) self.list.append(info.server[:-1]) diff --git a/bin/yunopaste b/bin/yunopaste index 85f491858e..5d2a58756b 100755 --- a/bin/yunopaste +++ b/bin/yunopaste @@ -27,23 +27,23 @@ SERVER_URL = "https://paste.yunohost.org" TIMEOUT = 3 -def create_snippet(data): +def create_snippet(data: str) -> str: try: - url = SERVER_URL + "/documents" + url = f"{SERVER_URL}/documents" response = requests.post(url, data=data.encode("utf-8"), timeout=TIMEOUT) response.raise_for_status() dockey = json.loads(response.text)["key"] - return SERVER_URL + "/raw/" + dockey + return f"{SERVER_URL}/raw/{dockey}" except requests.exceptions.RequestException as e: - print("\033[31mError: {}\033[0m".format(e)) + print(f"\033[31mError: {e}\033[0m", file=sys.stderr) sys.exit(1) -def main(): +def main() -> None: output = sys.stdin.read() if not output: - print("\033[31mError: No input received from stdin.\033[0m") + print("\033[31mError: No input received from stdin.\033[0m", file=sys.stderr) sys.exit(1) url = create_snippet(output)