From 219a0a99ca5c5949cf8a63d668ea42c809f15a57 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 9 Dec 2022 23:37:10 +0100 Subject: [PATCH] `main.db` should use the configured `workdir` when not specified. Closes: #234 Reviewed-On: https://git.platypush.tech/platypush/platypush/issues/234 --- CHANGELOG.md | 7 +++++++ platypush/config/__init__.py | 27 ++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1938d79eb..362324d55c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. Given the high speed of development in the first phase, changes are being reported only starting from v0.20.2. +## [Unreleased] + +## Fixed + +- The `main.db` configuration should use the configured `workdir` when no + values are specified. + ## [0.24.1] ### Fixed diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index cb5ab789ba..addba07fad 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -10,6 +10,7 @@ import re import shutil import socket import sys +from urllib.parse import quote from typing import Optional import yaml @@ -72,7 +73,7 @@ class Config: if cfgfile is None: cfgfile = self._create_default_config() - self._cfgfile = os.path.abspath(os.path.expanduser(cfgfile)) + cfgfile = self._cfgfile = os.path.abspath(os.path.expanduser(cfgfile)) self._config = self._read_config_file(self._cfgfile) if 'token' in self._config: @@ -80,6 +81,7 @@ class Config: if 'workdir' not in self._config: self._config['workdir'] = self._workdir_location + self._config['workdir'] = os.path.expanduser(self._config['workdir']) os.makedirs(self._config['workdir'], exist_ok=True) if 'scripts_dir' not in self._config: @@ -94,6 +96,7 @@ class Config: ) os.makedirs(self._config['dashboards_dir'], mode=0o755, exist_ok=True) + # Create a default (empty) __init__.py in the scripts folder init_py = os.path.join(self._config['scripts_dir'], '__init__.py') if not os.path.isfile(init_py): with open(init_py, 'w') as f: @@ -106,15 +109,21 @@ class Config: ) sys.path = [scripts_parent_dir] + sys.path - self._config['db'] = self._config.get( - 'main.db', - { - 'engine': 'sqlite:///' - + os.path.join( - os.path.expanduser('~'), '.local', 'share', 'platypush', 'main.db' + # Initialize the default db connection string + db_engine = self._config.get('main.db', '') + if db_engine: + if isinstance(db_engine, str): + db_engine = { + 'engine': db_engine, + } + else: + db_engine = { + 'engine': 'sqlite:///' + os.path.join( + quote(self._config['workdir']), 'main.db' ) - }, - ) + } + + self._config['db'] = db_engine logging_config = { 'level': logging.INFO,