65 lines
1.4 KiB
Python
65 lines
1.4 KiB
Python
import json
|
|
import os
|
|
|
|
|
|
config_path = 'build/meadow-config.json'
|
|
config_data = None
|
|
|
|
|
|
class Config:
|
|
def load():
|
|
global config_data
|
|
|
|
if not os.path.exists(config_path):
|
|
config_data = {}
|
|
return
|
|
|
|
with open(config_path, mode='r') as f:
|
|
config_data = json.load(f)
|
|
|
|
|
|
def save():
|
|
global config_data
|
|
|
|
with open(config_path, mode='w') as f:
|
|
json.dump(config_data, f, indent=4)
|
|
|
|
|
|
def get(path):
|
|
parts = path.split('.')
|
|
container = config_data
|
|
|
|
for p in parts[:-1]:
|
|
if p not in container:
|
|
return None
|
|
|
|
if not isinstance(container[p], dict):
|
|
print('Err: element "{}" in config key "{}" is not a dictionary'.format(p, path))
|
|
return
|
|
|
|
container = container[p]
|
|
|
|
|
|
if parts[-1] in container:
|
|
return container[parts[-1]]
|
|
|
|
return None
|
|
|
|
|
|
def set(path, value):
|
|
parts = path.split('.')
|
|
container = config_data
|
|
|
|
for p in parts[:-1]:
|
|
if p not in container:
|
|
container[p] = {}
|
|
|
|
if not isinstance(container[p], dict):
|
|
print('Err: element "{}" in config key "{}" is not a dictionary'.format(p, path))
|
|
return
|
|
|
|
container = container[p]
|
|
|
|
|
|
container[parts[-1]] = value
|