Merge pull request #473 from bunkerity/dev

Merge branch "dev" into branch "ui"
This commit is contained in:
Théophile Diot 2023-05-10 13:12:32 -04:00 committed by GitHub
commit dcdb43cf05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 39 deletions

View File

@ -366,7 +366,10 @@ class Database:
.all()
)
db_ids = [service.id for service in db_services]
services = config.get("SERVER_NAME", "").split(" ")
services = config.pop("SERVER_NAME", [])
if isinstance(services, str):
services = services.split(" ")
if db_services:
missing_ids = [
@ -376,13 +379,14 @@ class Database:
]
if missing_ids:
# Remove plugins that are no longer in the list
# Remove services that are no longer in the list
session.query(Services).filter(
Services.id.in_(missing_ids)
).delete()
for key, value in deepcopy(config).items():
suffix = 0
original_key = deepcopy(key)
if self.suffix_rx.search(key):
suffix = int(key.split("_")[-1])
key = key[: -len(str(suffix)) - 1]
@ -475,8 +479,8 @@ class Database:
Services_settings.method: method,
}
)
elif f"{key}_{suffix}" not in global_values:
global_values.append(f"{key}_{suffix}")
elif setting and original_key not in global_values:
global_values.append(original_key)
global_value = (
session.query(Global_values)
.with_entities(
@ -489,9 +493,6 @@ class Database:
.first()
)
if not setting:
continue
if global_value is None:
if value == setting.default or (
not value.strip() and setting.default is None
@ -622,12 +623,6 @@ class Database:
to_put = []
endl = "\n"
for custom_config in custom_configs:
# config = {
# "data": custom_config["value"].replace("\\\n", "\n").encode("utf-8")
# if isinstance(custom_config["value"], str)
# else custom_config["value"].replace(b"\\\n", b"\n"),
# "method": method,
# }
config = {
"data": custom_config["value"].encode("utf-8")
if isinstance(custom_config["value"], str)
@ -801,6 +796,11 @@ class Database:
}
)
servers = " ".join(service.id for service in session.query(Services).all())
config["SERVER_NAME"] = (
servers if methods is False else {"value": servers, "method": "default"}
)
return config
def get_custom_configs(self) -> List[Dict[str, Any]]:

View File

@ -511,6 +511,9 @@ if __name__ == "__main__":
# check if the config have changed since last time
tmp_env = db.get_config()
tmp_env["DATABASE_URI"] = environ.get(
"DATABASE_URI", tmp_env["DATABASE_URI"]
)
if env != tmp_env:
logger.info("Config changed, generating ...")
logger.debug(f"{tmp_env=}")

View File

@ -21,32 +21,6 @@ class Config:
self.__db = db
def __env_to_dict(self, filename: str) -> dict:
"""Converts the content of an env file into a dict
Parameters
----------
filename : str
the path to the file to convert to dict
Returns
-------
dict
The values of the file converted to dict
"""
if not Path(filename).is_file():
return {}
data = {}
for line in Path(filename).read_text().split("\n"):
if not "=" in line:
continue
var = line.split("=")[0]
val = line.replace(f"{var}=", "", 1)
data[var] = val
return data
def __dict_to_env(self, filename: str, variables: dict) -> None:
"""Converts the content of a dict into an env file