Added argument parser

Just for config file path. Defaults to environment variable
"MASTOPOSTER_CONFIG_FILE".
This commit is contained in:
Anna “CyberTailor” 2023-05-10 07:28:21 +05:00
parent d740ddb0b1
commit 65d0cc5b4d
No known key found for this signature in database
GPG Key ID: E7B76EDC50864BB1
1 changed files with 28 additions and 8 deletions

View File

@ -13,6 +13,7 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
""" """
from argparse import ArgumentParser
from asyncio import run from asyncio import run
from configparser import ConfigParser, ExtendedInterpolation from configparser import ConfigParser, ExtendedInterpolation
from logging import ( from logging import (
@ -23,14 +24,21 @@ from logging import (
getLevelName, getLevelName,
getLogger, getLogger,
) )
from sys import argv, stdout from os import getenv
from mastoposter import execute_integrations, load_integrations_from from sys import stdout
from mastoposter.integrations import FilteredIntegration
from mastoposter.sources import websocket_source
from typing import AsyncGenerator, Callable, List from typing import AsyncGenerator, Callable, List
from mastoposter.types import Account, Status
from httpx import Client, HTTPTransport 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 from mastoposter.utils import normalize_config
@ -95,10 +103,22 @@ async def listen(
await execute_integrations(status, drains) await execute_integrations(status, drains)
def main(config_path: str = argv[1]): def main():
conf = ConfigParser(interpolation=ExtendedInterpolation()) parser = ArgumentParser(
conf.read(config_path) 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"))) init_logger(getLevelName(conf["main"].get("loglevel", "INFO")))
normalize_config(conf) normalize_config(conf)