mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 19:44:15 +02:00
[web] refactoring the config reading code
This commit is contained in:
27
bauh/commons/config.py
Normal file
27
bauh/commons/config.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from bauh.commons import util
|
||||
|
||||
|
||||
def read_config(file_path: str, template: dict, update_file: bool = False) -> dict:
|
||||
if not os.path.exists(file_path):
|
||||
Path(file_path).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with open(file_path, 'w+') as f:
|
||||
f.write(yaml.dump(template))
|
||||
|
||||
else:
|
||||
with open(file_path) as f:
|
||||
local_config = yaml.safe_load(f.read())
|
||||
|
||||
if local_config:
|
||||
util.deep_update(template, local_config)
|
||||
|
||||
if update_file:
|
||||
with open(file_path, 'w+') as f:
|
||||
f.write(yaml.dump(template))
|
||||
|
||||
return template
|
||||
@@ -1,10 +1,4 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from bauh.api.constants import CONFIG_PATH
|
||||
from bauh.commons import util
|
||||
from bauh.commons.config import read_config as read
|
||||
from bauh.gems.web import CONFIG_FILE
|
||||
|
||||
|
||||
@@ -15,20 +9,6 @@ def read_config(update_file: bool = False) -> dict:
|
||||
'electron': {'version': None}
|
||||
}
|
||||
}
|
||||
if not os.path.exists(CONFIG_FILE):
|
||||
Path(CONFIG_PATH).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with open(CONFIG_FILE, 'w+') as f:
|
||||
f.write(yaml.dump(default_config))
|
||||
return read(CONFIG_FILE, default_config, update_file)
|
||||
|
||||
else:
|
||||
with open(CONFIG_FILE) as f:
|
||||
local_config = yaml.safe_load(f.read())
|
||||
|
||||
util.deep_update(default_config, local_config)
|
||||
|
||||
if update_file:
|
||||
with open(CONFIG_FILE, 'w+') as f:
|
||||
f.write(yaml.dump(default_config))
|
||||
|
||||
return default_config
|
||||
|
||||
Reference in New Issue
Block a user