Update
This commit is contained in:
74
config.py
Normal file
74
config.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from argparse import Namespace
|
||||
from os.path import exists
|
||||
import json
|
||||
try:
|
||||
from functools import cached_property
|
||||
except Exception:
|
||||
cached_property = property
|
||||
|
||||
|
||||
class Config:
|
||||
def __init__(self, cfg_path: str):
|
||||
self._path = cfg_path
|
||||
self._data = {}
|
||||
self._args = Namespace()
|
||||
if exists(cfg_path):
|
||||
with open(cfg_path, 'r', encoding='UTF-8') as f:
|
||||
self._data = json.load(f)
|
||||
else:
|
||||
self._data['db'] = 'cwm.db'
|
||||
self._data['save_to_config'] = True
|
||||
self._data['export_chapter_template'] = 'exported/<book_id>/<chapter_id>.txt' # noqa: E501
|
||||
self.save()
|
||||
|
||||
def add_args(self, args: Namespace):
|
||||
self._args = args
|
||||
|
||||
def get_arg(self, key: str, default):
|
||||
x = getattr(self._args, key, None)
|
||||
if x is not None:
|
||||
if self.save_to_config:
|
||||
self._data[key] = x
|
||||
return x
|
||||
if key in self._data:
|
||||
return self._data[key]
|
||||
else:
|
||||
return default
|
||||
|
||||
@cached_property
|
||||
def booksnew(self):
|
||||
return self.get_arg('booksnew', None)
|
||||
|
||||
@cached_property
|
||||
def chapter_id(self):
|
||||
return getattr(self._args, 'cid', None)
|
||||
|
||||
@cached_property
|
||||
def cwmdb(self):
|
||||
return self.get_arg('cwmdb', None)
|
||||
|
||||
@cached_property
|
||||
def db(self):
|
||||
return self.get_arg('db', 'cwm.db')
|
||||
|
||||
@cached_property
|
||||
def export_chapter_template(self):
|
||||
return self.get_arg('export_chapter_template', 'exported/<book_id>/<chapter_id>.txt') # noqa: E501
|
||||
|
||||
def get_export_chapter(self, chapter):
|
||||
temp = self.export_chapter_template
|
||||
for k in chapter.keys():
|
||||
temp = temp.replace(f'<{k}>', str(chapter[k]))
|
||||
return temp
|
||||
|
||||
@cached_property
|
||||
def key(self):
|
||||
return self.get_arg('key', None)
|
||||
|
||||
@cached_property
|
||||
def save_to_config(self):
|
||||
return getattr(self._args, 'save_to_config', True)
|
||||
|
||||
def save(self):
|
||||
with open(self._path, 'w', encoding='UTF-8') as f:
|
||||
json.dump(self._data, f, ensure_ascii=False)
|
||||
Reference in New Issue
Block a user