craftbeerpi4-pione/cbpi/controller/config_controller.py

95 lines
3.9 KiB
Python
Raw Permalink Normal View History

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
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)
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():
self.cache[key] = Config(name=value.get("name"), value=value.get("value"), description=value.get("description"), type=ConfigType(value.get("type", "string")), source=value.get("source", "craftbeerpi"), options=value.get("options", None))
2018-12-29 00:27:19 +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))
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={}
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),
source=data[key].get("source", "craftbeerpi") )
success=True
2023-04-06 12:40:49 +02:00
except Exception as e:
print(e)
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
async def obsolete(self, remove=False):
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:
2023-04-16 17:48:19 +02:00
result[str(value.source)+'_update']={"value": update}
if remove:
await self.remove(str(value.source)+'_update')
if remove:
await self.remove(key)
result[key] = value.to_dict()
return result