Read config from environment and use argparse

Related to https://git.autonomic.zone/decentral1se/xbotlib/issues/7.
This commit is contained in:
Luke Murphy 2021-01-12 21:39:50 +01:00
parent 06325fa5aa
commit 4f6f102d1e
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
4 changed files with 54 additions and 8 deletions

View File

@ -1,5 +1,9 @@
# xbotlib x.x.x (UNRELEASED) # xbotlib x.x.x (UNRELEASED)
# xbotlib 0.3.1 (2021-01-12)
- Support `--no-input` flag and read configuration from the environment
# xbotlib 0.3.0 (2021-01-10) # xbotlib 0.3.0 (2021-01-10)
- Error out if you don't provide a `react` implementation - Error out if you don't provide a `react` implementation

View File

@ -88,6 +88,19 @@ Attributes:
- **nickname**: the nickname of the sender - **nickname**: the nickname of the sender
- **type**: the type of message (`chat` or `groupchat`) - **type**: the type of message (`chat` or `groupchat`)
## Configure your bot
### Using the environment
You can pass the `--no-input` option to your script invocation (e.g. `python bot.py --no-input`).
`xbotlib` will try to read the following configuration values from the environment.
- **XBOT_JID**
- **XBOT_PASSWORD**
- **XBOT_ROOM**
- **XBOT_NICK**
## Roadmap ## Roadmap
See the [issue tracker](https://git.autonomic.zone/decentral1se/xbotlib/issues). See the [issue tracker](https://git.autonomic.zone/decentral1se/xbotlib/issues).

View File

@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
[tool.poetry] [tool.poetry]
name = "xbotlib" name = "xbotlib"
version = "0.3.0" version = "0.3.1"
description = "XMPP bots for humans" description = "XMPP bots for humans"
authors = ["decentral1se <lukewm@riseup.net>"] authors = ["decentral1se <lukewm@riseup.net>"]
maintainers = ["decentral1se <lukewm@riseup.net>"] maintainers = ["decentral1se <lukewm@riseup.net>"]

View File

@ -1,7 +1,9 @@
"""XMPP bots for humans.""" """XMPP bots for humans."""
from argparse import ArgumentParser, BooleanOptionalAction
from configparser import ConfigParser from configparser import ConfigParser
from getpass import getpass from getpass import getpass
from os import environ
from os.path import exists from os.path import exists
from pathlib import Path from pathlib import Path
@ -39,23 +41,42 @@ class Bot(ClientXMPP):
CONFIG_FILE = "bot.conf" CONFIG_FILE = "bot.conf"
def __init__(self): def __init__(self):
self.parse_arguments()
self.read_config() self.read_config()
self.init_bot() self.init_bot()
self.register_xmpp_event_handlers() self.register_xmpp_event_handlers()
self.register_xmpp_plugins() self.register_xmpp_plugins()
self.run() 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): def read_config(self):
"""Read configuration for running bot.""" """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 = 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.""" """Generate bot configuration."""
jid = input("XMPP address (e.g. foo@bar.com): ") or "foo@bar.com" jid = input("XMPP address (e.g. foo@bar.com): ") or "foo@bar.com"
password = ( password = (
@ -75,6 +96,14 @@ class Bot(ClientXMPP):
with open("bot.conf", "w") as file_handle: with open("bot.conf", "w") as file_handle:
config.write(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): def init_bot(self):
"""Initialise bot with connection details.""" """Initialise bot with connection details."""
jid = self.config["bot"]["jid"] jid = self.config["bot"]["jid"]