2022-02-15 09:13:45 +05:30
|
|
|
"""Module to update `setting.json`."""
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
# ba_meta require api 6
|
|
|
|
|
# (see https://ballistica.net/wiki/meta-tag-system)
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
from __future__ import annotations
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
from functools import lru_cache
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
import json
|
|
|
|
|
import _ba
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
pass
|
2021-11-10 17:26:07 +05:30
|
|
|
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
SETTINGS_PATH = _ba.env().get("python_directory_user", "") + "/setting.json"
|
2021-04-17 13:22:30 +05:30
|
|
|
|
|
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
@lru_cache(maxsize=None)
|
|
|
|
|
def get_settings_data() -> dict:
|
|
|
|
|
"""Returns the dictionary of settings related to the server.
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
dict
|
|
|
|
|
settings related to server
|
|
|
|
|
"""
|
|
|
|
|
with open(SETTINGS_PATH, mode="r", encoding="utf-8") as data:
|
|
|
|
|
return json.load(data)
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2023-06-19 21:58:35 +05:30
|
|
|
def refresh_cache() -> None:
|
|
|
|
|
get_settings_data.cache_clear()
|
|
|
|
|
# lets cache it again
|
|
|
|
|
get_settings_data()
|
2021-04-17 13:22:30 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
def commit(data: dict) -> None:
|
|
|
|
|
"""Commits the data in setting file.
|
2021-04-03 20:54:32 +05:30
|
|
|
|
2022-02-15 09:13:45 +05:30
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
data : dict
|
|
|
|
|
data to be commited
|
|
|
|
|
"""
|
|
|
|
|
with open(SETTINGS_PATH, mode="w", encoding="utf-8") as setting_file:
|
|
|
|
|
json.dump(data, setting_file, indent=4)
|
2023-06-19 21:58:35 +05:30
|
|
|
# settings updated ok now update the cache
|
|
|
|
|
refresh_cache()
|