mirror of
https://github.com/Websoft9/websoft9.git
synced 2025-01-25 11:58:44 +08:00
51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
|
from api.utils.helper import Singleton
|
||
|
|
||
|
|
||
|
__all__ = ['settings']
|
||
|
|
||
|
|
||
|
class Settings(object):
|
||
|
|
||
|
__metaclass__ = Singleton
|
||
|
|
||
|
def __init__(self):
|
||
|
self._config = {}
|
||
|
self.config_file = '/usr/src/app/config/settings.conf'
|
||
|
|
||
|
def init_config_from_file(self, config_file: str=None):
|
||
|
if config_file:
|
||
|
self.config_file = config_file
|
||
|
try:
|
||
|
with open(config_file, 'r') as f:
|
||
|
data = f.readlines()
|
||
|
except Exception:
|
||
|
data = []
|
||
|
for i in data:
|
||
|
i = i.replace('\n', '').replace('\r\n', '')
|
||
|
key, value = i.split('=')
|
||
|
if self._config.get(key) != value:
|
||
|
self._config[key] = value
|
||
|
|
||
|
def update_setting(self, key: str, value: str):
|
||
|
self._config[key] = value
|
||
|
self.flush_config()
|
||
|
|
||
|
def get_setting(self, key: str, default=None):
|
||
|
return self._config.get(key, default)
|
||
|
|
||
|
def list_all_settings(self) -> list:
|
||
|
return self._config
|
||
|
|
||
|
def delete_setting(self, key: str, value: str):
|
||
|
if key in self._config:
|
||
|
del self._config[key]
|
||
|
|
||
|
def flush_config(self):
|
||
|
with open(self.config_file, 'w') as f:
|
||
|
for key, value in self._config.items():
|
||
|
f.write(f'{key}={value}\n')
|
||
|
|
||
|
|
||
|
|
||
|
settings = Settings()
|