add config parser

This commit is contained in:
histausse 2021-10-05 21:27:16 +02:00
parent f544d8eef9
commit 1172db1d95
Signed by: histausse
GPG key ID: 67486F107F62E9E9
4 changed files with 45 additions and 0 deletions

8
config.yaml.exp Normal file
View file

@ -0,0 +1,8 @@
---
username: kassandra
homeserver: https://matrix.org
password: beware of greeks bearing gifts
allert_rooms:
- "#troy:matrix.org"
- "#ithaca:matrix.org"
...

View file

@ -12,6 +12,7 @@ packages = kassandra
python_requires = >=3.9.2 python_requires = >=3.9.2
package_dir = =src package_dir = =src
install_requires = install_requires =
PyYAML>=5.4.1
matrix-bot @ git+https://gitea.auro.re/histausse/matrix-bot.git matrix-bot @ git+https://gitea.auro.re/histausse/matrix-bot.git
[options.package_data] [options.package_data]

14
src/kassandra/__main__.py Normal file
View file

@ -0,0 +1,14 @@
import argparse
import asyncio
from .config import load_config
async def main():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", default="config.yaml")
args = parser.parse_args()
config = load_config(args.config)
print(config)
if __name__ == "__main__":
asyncio.run(main())

22
src/kassandra/config.py Normal file
View file

@ -0,0 +1,22 @@
"""
Config related tools.
"""
import dataclasses
import yaml
@dataclasses.dataclass
class Config:
username: str
homeserver: str
password: str
allert_rooms: list[str]
def load_config(file:str)->Config:
"""
Load the config from the config file.
"""
with open(file, 'r') as f:
data = yaml.load(f, Loader=yaml.loader.SafeLoader)
return Config(**data)