Bombsquad-Ballistica-Modded.../dist/ba_root/mods/setting.py

60 lines
1.6 KiB
Python
Raw Normal View History

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
import datetime
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-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
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.
2022-02-15 09:13:45 +05:30
Parameters
----------
data : dict
data to be commited
"""
if is_invalid_time_format(data['autoNightMode']['startTime']) or is_invalid_time_format(data['autoNightMode']['endTime']):
data['autoNightMode']['startTime'] = "18:30"
data['autoNightMode']['endTime'] = "6:30"
print("resetting night mode time")
2022-02-15 09:13:45 +05:30
with open(SETTINGS_PATH, mode="w", encoding="utf-8") as setting_file:
json.dump(data, setting_file, indent=4)
# settings updated ok now update the cache
refresh_cache()
def is_invalid_time_format(time_string, time_format='%H:%M'):
try:
datetime.datetime.strptime(time_string, time_format)
return False
except ValueError:
return True