From 7b5954cb46b2593f0ae064262bef5355e6a3e0e9 Mon Sep 17 00:00:00 2001 From: John Burwell Date: Mon, 24 Apr 2023 00:30:11 -0500 Subject: [PATCH] handle config.yaml files and message db file better --- rsbbs/bbs.py | 30 ++++++++++++++----- ...config.yaml.sample => config_default.yaml} | 0 setup.py | 1 + 3 files changed, 23 insertions(+), 8 deletions(-) rename rsbbs/{config.yaml.sample => config_default.yaml} (100%) diff --git a/rsbbs/bbs.py b/rsbbs/bbs.py index 3c1044e..7c81742 100644 --- a/rsbbs/bbs.py +++ b/rsbbs/bbs.py @@ -29,6 +29,8 @@ from sqlalchemy.orm import Session from rsbbs.message import Message, Base from rsbbs.parser import Parser +import platformdirs + # Main BBS class @@ -58,21 +60,32 @@ class BBS(): # Load the config file def load_config(self, config_file): + config_dir = platformdirs.user_config_dir(appname='rsbbs', ensure_exists=True) + + config_path = os.path.join(config_dir, 'config.yaml') + config_template_path = os.path.join(os.path.dirname(__file__), 'config_default.yaml') + + if not os.path.exists(config_path): + with open(config_template_path, 'r') as f: + config_template = yaml.load(f, Loader=yaml.FullLoader) + with open(config_path, 'w') as f: + yaml.dump(config_template, f) + try: - with open(config_file, 'r') as stream: - config = yaml.safe_load(stream) + with open(config_path, 'r') as f: + config = yaml.load(f, Loader=yaml.FullLoader) except yaml.YAMLError as e: - print("Could not load configuration file. Error: {}".format(e)) + print(f"Could not load configuration file. Error: {e}") exit(1) except FileNotFoundError as e: - print('Configuration file full path: {}'.format(os.path.abspath(config_file))) - print("Configuration file {} could not be found. Error: {}".format(config_file, e)) + print(f'Configuration file full path: {os.path.abspath(config_file)}') + print(f"Configuration file {config_file} could not be found. Error: {e}") exit(1) except Exception as msg: - print("Error while loading configuration file {}. Error: {}".format(config_file)) + print(f"Error while loading configuration file {config_file}. Error: {e}") exit(1) - logging.info("Configuration file was successfully loaded. File name: {}".format(config_file)) + logging.info(f"Configuration file was successfully loaded. File name: {config_file}") return config @@ -116,7 +129,8 @@ class BBS(): # Database def init_engine(self): - engine = create_engine('sqlite:///messages.db', echo=self.sysv_args.debug) + db_path = os.path.join(platformdirs.user_data_dir(appname='rsbbs', ensure_exists=True), 'messages.db') + engine = create_engine('sqlite:///' + db_path, echo=self.sysv_args.debug) Base.metadata.create_all(engine) return engine diff --git a/rsbbs/config.yaml.sample b/rsbbs/config_default.yaml similarity index 100% rename from rsbbs/config.yaml.sample rename to rsbbs/config_default.yaml diff --git a/setup.py b/setup.py index b4d44fc..7f6b57b 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ setup( url='https://git.b-wells.us/jmbwell/rsbbs', license=license, packages=find_packages(exclude=('tests', 'docs')), + data_files=[('', ['config_default.yaml'])], entry_points = ''' [console_scripts] rsbbs=rsbbs.rsbbs:main