mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Refactored configuration system
This commit is contained in:
31
src/backend/lib/config.py
vendored
31
src/backend/lib/config.py
vendored
@@ -17,6 +17,7 @@ class Config:
|
||||
Attributes
|
||||
----------
|
||||
root : str() stores root.
|
||||
config_structure : dict() Default Configuration Structure.
|
||||
_fp : str() file pointer to main configuration.
|
||||
_cp : Path() object of configuration file.
|
||||
_data : dict() parsed json of _fp.
|
||||
@@ -48,13 +49,32 @@ class Config:
|
||||
def __init__(self, root):
|
||||
"""Initialize main configuration options."""
|
||||
self.root = root
|
||||
self.config_structure = {
|
||||
"TITLE": "pyShelf E-Book Server",
|
||||
"VERSION": "0.7.0",
|
||||
"BOOKPATH": "/books",
|
||||
"DB_HOST": "localhost",
|
||||
"DB_PORT": "5432",
|
||||
"DB_ENGINE": "sqlite",
|
||||
"DATABASE": "pyshelf",
|
||||
"USER": "pyshelf",
|
||||
"PASSWORD": "pyshelf",
|
||||
"BOOKSHELF": "data/shelf.json",
|
||||
"ALLOWED_HOSTS": [
|
||||
"localhost",
|
||||
"127.0.0.1",
|
||||
"[::1]",
|
||||
"0.0.0.0"
|
||||
],
|
||||
"BUILD_MODE": "development"
|
||||
}
|
||||
env = os.environ.copy()
|
||||
self._fp = "config.json"
|
||||
try:
|
||||
self._cp = pathlib.Path.joinpath(root, self._fp)
|
||||
except AttributeError:
|
||||
self._cp = pathlib.Path(root, self._fp)
|
||||
self._data = self.open_file()
|
||||
self._data = self.init_config()
|
||||
try:
|
||||
self.logger
|
||||
except AttributeError:
|
||||
@@ -78,6 +98,15 @@ class Config:
|
||||
self.db_pass = env.get("PASSWORD", self._data["PASSWORD"])
|
||||
self.build_mode = env.get("BUILD_MODE", self._data["BUILD_MODE"])
|
||||
|
||||
def init_config(self):
|
||||
try:
|
||||
return self.open_file()
|
||||
except FileNotFoundError:
|
||||
with open(self._fp, 'w') as _config_file:
|
||||
json.dump(self.config_structure, _config_file)
|
||||
_config_file.close()
|
||||
return self.open_file()
|
||||
|
||||
def get_logger(self):
|
||||
"""Instantiate logging system."""
|
||||
_logger = logger
|
||||
|
||||
7
src/backend/lib/storage.py
vendored
7
src/backend/lib/storage.py
vendored
@@ -49,12 +49,7 @@ class Storage:
|
||||
str : sqlalchemy Connection String
|
||||
"""
|
||||
if self.config.db_engine == "sqlite":
|
||||
if os.path.exists(f"{self.config.root}/pyshelf.db"):
|
||||
return f"sqlite:////{self.config.root}/pyshelf.db"
|
||||
else:
|
||||
sqlite_file = open(f'{self.config.root}/pyshelf.db', 'w')
|
||||
sqlite_file.close()
|
||||
return f"sqlite://{self.config.root}/pyshelf.db"
|
||||
return f"sqlite:////{self.config.root}/pyshelf.db"
|
||||
elif self.config.db_engine == "psql":
|
||||
return f"postgresql://{self.user}:{self.password}\
|
||||
@{self.db_host}:{self.db_port}/{self.sql}"
|
||||
|
||||
Reference in New Issue
Block a user