From aabe1fa683f0f47741a683147c357928de652f5c Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Thu, 14 Jan 2021 22:36:03 +0100 Subject: [PATCH] `bot.conf` -> `$name.conf` Closes https://git.autonomic.zone/decentral1se/xbotlib/issues/3. --- .gitignore | 4 ++-- CHANGELOG.md | 1 + xbotlib.py | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 40647c1..1ad7dd3 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,8 @@ .mypy_cache/ .tox/ __pycache__ -bot.conf -bot.py +*.conf +echobot.py build/ dist/ pip-wheel-metadata/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 97d0343..d0c42b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Arrange precedence logic for config loading ([#14](https://git.autonomic.zone/decentral1se/xbotlib/issues/14)) - Remove `--no-input` option and detect it automatically ([#14](https://git.autonomic.zone/decentral1se/xbotlib/issues/14)) - Refer to `jid` as `account` from now on both internally and externally ([#14](https://git.autonomic.zone/decentral1se/xbotlib/issues/14)) +- `bot.conf` -> `$name.conf` ([#3](https://git.autonomic.zone/decentral1se/xbotlib/issues/3)) # xbotlib 0.7.1 (2021-01-13) diff --git a/xbotlib.py b/xbotlib.py index 677cddd..0acf7dd 100644 --- a/xbotlib.py +++ b/xbotlib.py @@ -46,9 +46,10 @@ class SimpleMessage: class Config: """Bot file configuration.""" - def __init__(self, config): + def __init__(self, name, config): + self.name = name self.config = config - self.section = config["bot"] if "bot" in config else {} + self.section = config[self.name] if self.name in config else {} @property def account(self): @@ -66,9 +67,10 @@ class Config: class Bot(ClientXMPP): """XMPP bots for humans.""" - CONFIG_FILE = "bot.conf" - def __init__(self): + self.name = type(self).__name__.lower() + self.CONFIG_FILE = f"{self.name}.conf" + self.parse_arguments() self.setup_logging() self.read_config() @@ -130,7 +132,7 @@ class Bot(ClientXMPP): if exists(config_file_path): config.read(config_file_path) - self.config = Config(config) + self.config = Config(self.name, config) def generate_config_interactively(self): """Generate bot configuration.""" @@ -139,12 +141,12 @@ class Bot(ClientXMPP): nick = input("Nickname: ") config = ConfigParser() - config["bot"] = {"account": account, "password": password} + config[self.name] = {"account": account, "password": password} if nick: - config["bot"]["nick"] = nick + config[self.name]["nick"] = nick - with open("bot.conf", "w") as file_handle: + with open(self.CONFIG_FILE, "w") as file_handle: config.write(file_handle) def init_bot(self):