Read config from environment and use argparse
Related to https://git.autonomic.zone/decentral1se/xbotlib/issues/7.
This commit is contained in:
43
xbotlib.py
43
xbotlib.py
@ -1,7 +1,9 @@
|
||||
"""XMPP bots for humans."""
|
||||
|
||||
from argparse import ArgumentParser, BooleanOptionalAction
|
||||
from configparser import ConfigParser
|
||||
from getpass import getpass
|
||||
from os import environ
|
||||
from os.path import exists
|
||||
from pathlib import Path
|
||||
|
||||
@ -39,23 +41,42 @@ class Bot(ClientXMPP):
|
||||
CONFIG_FILE = "bot.conf"
|
||||
|
||||
def __init__(self):
|
||||
self.parse_arguments()
|
||||
self.read_config()
|
||||
self.init_bot()
|
||||
self.register_xmpp_event_handlers()
|
||||
self.register_xmpp_plugins()
|
||||
self.run()
|
||||
|
||||
def parse_arguments(self):
|
||||
"""Parse command-line arguments."""
|
||||
self.parser = ArgumentParser()
|
||||
self.parser.add_argument(
|
||||
"--input",
|
||||
help="Read configuration from environment",
|
||||
action=BooleanOptionalAction,
|
||||
default=True,
|
||||
)
|
||||
self.args = self.parser.parse_args()
|
||||
|
||||
def read_config(self):
|
||||
"""Read configuration for running bot."""
|
||||
config_file_path = Path(self.CONFIG_FILE).absolute()
|
||||
|
||||
if not exists(config_file_path):
|
||||
self.generate_config()
|
||||
|
||||
self.config = ConfigParser()
|
||||
self.config.read(config_file_path)
|
||||
|
||||
def generate_config(self):
|
||||
config_file_path = Path(self.CONFIG_FILE).absolute()
|
||||
if not exists(config_file_path) and self.args.input:
|
||||
self.generate_config_interactively()
|
||||
|
||||
if exists(config_file_path):
|
||||
self.config.read(config_file_path)
|
||||
|
||||
if self.args.input is False:
|
||||
self.read_config_from_env()
|
||||
|
||||
if "bot" not in self.config:
|
||||
raise RuntimeError("Failed to configure bot")
|
||||
|
||||
def generate_config_interactively(self):
|
||||
"""Generate bot configuration."""
|
||||
jid = input("XMPP address (e.g. foo@bar.com): ") or "foo@bar.com"
|
||||
password = (
|
||||
@ -75,6 +96,14 @@ class Bot(ClientXMPP):
|
||||
with open("bot.conf", "w") as file_handle:
|
||||
config.write(file_handle)
|
||||
|
||||
def read_config_from_env(self):
|
||||
"""Read configuration from the environment."""
|
||||
self.config["bot"] = {}
|
||||
self.config["bot"]["jid"] = environ.get("XBOT_JID")
|
||||
self.config["bot"]["password"] = environ.get("XBOT_PASSWORD")
|
||||
self.config["bot"]["room"] = environ.get("XBOT_ROOM")
|
||||
self.config["bot"]["nick"] = environ.get("XBOT_NICK")
|
||||
|
||||
def init_bot(self):
|
||||
"""Initialise bot with connection details."""
|
||||
jid = self.config["bot"]["jid"]
|
||||
|
||||
Reference in New Issue
Block a user