From 65d0cc5b4d21ad24c584bb6bf9308500992fd400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Wed, 10 May 2023 07:28:21 +0500 Subject: [PATCH] Added argument parser Just for config file path. Defaults to environment variable "MASTOPOSTER_CONFIG_FILE". --- mastoposter/__main__.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/mastoposter/__main__.py b/mastoposter/__main__.py index 195b61a..6fd395b 100644 --- a/mastoposter/__main__.py +++ b/mastoposter/__main__.py @@ -13,6 +13,7 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. """ +from argparse import ArgumentParser from asyncio import run from configparser import ConfigParser, ExtendedInterpolation from logging import ( @@ -23,14 +24,21 @@ from logging import ( getLevelName, getLogger, ) -from sys import argv, stdout -from mastoposter import execute_integrations, load_integrations_from -from mastoposter.integrations import FilteredIntegration -from mastoposter.sources import websocket_source +from os import getenv +from sys import stdout from typing import AsyncGenerator, Callable, List -from mastoposter.types import Account, Status + from httpx import Client, HTTPTransport +from mastoposter import ( + execute_integrations, + load_integrations_from, + __version__, + __description__ +) +from mastoposter.integrations import FilteredIntegration +from mastoposter.sources import websocket_source +from mastoposter.types import Account, Status from mastoposter.utils import normalize_config @@ -95,10 +103,22 @@ async def listen( await execute_integrations(status, drains) -def main(config_path: str = argv[1]): - conf = ConfigParser(interpolation=ExtendedInterpolation()) - conf.read(config_path) +def main(): + parser = ArgumentParser( + prog="mastoposter", + description=__description__ + ) + parser.add_argument("config", nargs="?", + default=getenv("MASTOPOSTER_CONFIG_FILE") + ) + parser.add_argument("-v", action="version", version=__version__) + args = parser.parse_args() + if not args.config: + raise RuntimeError("No config file. Aborting") + + conf = ConfigParser(interpolation=ExtendedInterpolation()) + conf.read(args.config) init_logger(getLevelName(conf["main"].get("loglevel", "INFO"))) normalize_config(conf)