2021-02-16 20:37:51 +01:00
|
|
|
from cbpi.api.dataclasses import Config
|
2018-12-29 00:27:19 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2022-09-09 18:38:08 +02:00
|
|
|
from pathlib import Path
|
2018-12-29 00:27:19 +01:00
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.api.config import ConfigType
|
|
|
|
from cbpi.utils import load_config
|
2021-02-16 20:37:51 +01:00
|
|
|
import json
|
2018-11-18 15:40:10 +01:00
|
|
|
|
2019-07-27 21:08:19 +02:00
|
|
|
class ConfigController:
|
2021-02-16 20:37:51 +01:00
|
|
|
|
2018-11-18 15:40:10 +01:00
|
|
|
|
|
|
|
def __init__(self, cbpi):
|
2018-12-29 00:27:19 +01:00
|
|
|
self.cache = {}
|
|
|
|
self.logger = logging.getLogger(__name__)
|
2018-11-18 15:40:10 +01:00
|
|
|
self.cbpi = cbpi
|
2019-01-02 21:20:44 +01:00
|
|
|
self.cbpi.register(self)
|
2022-02-19 12:33:11 +01:00
|
|
|
self.path = cbpi.config_folder.get_file_path("config.json")
|
|
|
|
self.path_static = cbpi.config_folder.get_file_path("config.yaml")
|
2022-09-12 21:54:51 +02:00
|
|
|
self.logger.info("Config folder path : " + os.path.join(Path(self.cbpi.config_folder.configFolderPath).absolute()))
|
2019-01-07 22:05:52 +01:00
|
|
|
|
2023-04-01 14:13:04 +02:00
|
|
|
def get_state(self):
|
2021-02-16 20:37:51 +01:00
|
|
|
result = {}
|
|
|
|
for key, value in self.cache.items():
|
|
|
|
result[key] = value.to_dict()
|
2023-04-01 14:13:04 +02:00
|
|
|
return result
|
2019-01-07 22:05:52 +01:00
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
async def init(self):
|
2021-02-19 22:24:15 +01:00
|
|
|
self.static = load_config(self.path_static)
|
2021-02-16 20:37:51 +01:00
|
|
|
with open(self.path) as json_file:
|
|
|
|
data = json.load(json_file)
|
|
|
|
for key, value in data.items():
|
2023-04-01 14:13:04 +02:00
|
|
|
self.cache[key] = Config(name=value.get("name"), value=value.get("value"), description=value.get("description"), type=ConfigType(value.get("type", "string")), options=value.get("options", None), source=value.get("source", "craftbeerpi") )
|
2018-12-29 00:27:19 +01:00
|
|
|
|
2018-12-31 00:22:00 +01:00
|
|
|
def get(self, name, default=None):
|
2019-07-27 21:08:19 +02:00
|
|
|
self.logger.debug("GET CONFIG VALUE %s (default %s)" % (name, default))
|
2021-07-13 07:07:55 +02:00
|
|
|
if name in self.cache and self.cache[name].value is not None and self.cache[name].value != "":
|
2018-12-29 00:27:19 +01:00
|
|
|
return self.cache[name].value
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
async def set(self, name, value):
|
|
|
|
if name in self.cache:
|
2021-02-16 20:37:51 +01:00
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
self.cache[name].value = value
|
2021-02-16 20:37:51 +01:00
|
|
|
|
|
|
|
data = {}
|
|
|
|
for key, value in self.cache.items():
|
|
|
|
data[key] = value.to_dict()
|
|
|
|
with open(self.path, "w") as file:
|
|
|
|
json.dump(data, file, indent=4, sort_keys=True)
|
2018-12-29 00:27:19 +01:00
|
|
|
|
2023-04-06 12:40:49 +02:00
|
|
|
async def add(self, name, value, type: ConfigType, description, source="craftbeerpi", options=None):
|
2023-04-02 16:14:33 +02:00
|
|
|
self.cache[name] = Config(name,value,description,type,source,options)
|
2021-02-16 20:37:51 +01:00
|
|
|
data = {}
|
|
|
|
for key, value in self.cache.items():
|
|
|
|
data[key] = value.to_dict()
|
|
|
|
with open(self.path, "w") as file:
|
|
|
|
json.dump(data, file, indent=4, sort_keys=True)
|
2023-04-06 12:40:49 +02:00
|
|
|
|
|
|
|
async def remove(self, name):
|
|
|
|
data = {}
|
|
|
|
self.testcache={}
|
2023-04-07 16:26:03 +02:00
|
|
|
success=False
|
2023-04-06 12:40:49 +02:00
|
|
|
for key, value in self.cache.items():
|
|
|
|
try:
|
|
|
|
if key != name:
|
|
|
|
data[key] = value.to_dict()
|
|
|
|
self.testcache[key] = Config(name=data[key].get("name"), value=data[key].get("value"), description=data[key].get("description"),
|
|
|
|
type=ConfigType(data[key].get("type", "string")), options=data[key].get("options", None),
|
2023-04-07 16:26:03 +02:00
|
|
|
source=data[key].get("source", "craftbeerpi") )
|
|
|
|
success=True
|
2023-04-06 12:40:49 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2023-04-07 16:26:03 +02:00
|
|
|
success=False
|
|
|
|
if success == True:
|
|
|
|
with open(self.path, "w") as file:
|
|
|
|
json.dump(data, file, indent=4, sort_keys=True)
|
|
|
|
self.cache=self.testcache
|
2023-04-06 12:40:49 +02:00
|
|
|
|
2023-04-08 14:15:08 +02:00
|
|
|
async def remove_obsolete(self):
|
|
|
|
result = {}
|
|
|
|
for key, value in self.cache.items():
|
|
|
|
if (value.source not in ('craftbeerpi','steps','hidden')):
|
|
|
|
test = await self.cbpi.plugin.load_plugin_list(value.source)
|
|
|
|
if test == []:
|
|
|
|
update=self.get(str(value.source)+'_update')
|
|
|
|
if update:
|
|
|
|
await self.remove(str(value.source)+'_update')
|
|
|
|
await self.remove(key)
|
|
|
|
result[key] = value.to_dict()
|
|
|
|
return result
|