Fixed that weird behavior on forms setting
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import Union
|
||||
from typing import Any, Union
|
||||
from ujson import JSONDecodeError as JSONDecodeError
|
||||
from ujson import loads, dumps
|
||||
|
||||
@@ -32,13 +32,27 @@ def jsonSave(contents, filename):
|
||||
return
|
||||
|
||||
|
||||
def configSet(key: str, value, *args: str, file: str = "config"):
|
||||
"""Set key to a value
|
||||
def nested_set(dic, keys, value, create_missing=True):
|
||||
d = dic
|
||||
for key in keys[:-1]:
|
||||
if key in d:
|
||||
d = d[key]
|
||||
elif create_missing:
|
||||
d = d.setdefault(key, {})
|
||||
else:
|
||||
return dic
|
||||
if keys[-1] in d or create_missing:
|
||||
d[keys[-1]] = value
|
||||
return dic
|
||||
|
||||
def configSet(keys: list, value: Any, file: str = "config", create_missing=True):
|
||||
"""Set config's value to provided one
|
||||
|
||||
Args:
|
||||
* key (str): The last key of the keys path.
|
||||
* value (str/int/float/list/dict/None): Some needed value.
|
||||
* *args (str): Path to key like: dict[args][key].
|
||||
* file (str): User ID to save. Saved to config if not provided. Defaults to "config".
|
||||
* keys (list): List of keys from the highest one to target
|
||||
* value (Any): Needed value
|
||||
* file (str, optional): File (if not config). Defaults to "config".
|
||||
* create_missing (bool, optional): Create missing items on the way. Defaults to True.
|
||||
"""
|
||||
if file == "config":
|
||||
filepath = ""
|
||||
@@ -52,18 +66,8 @@ def configSet(key: str, value, *args: str, file: str = "config"):
|
||||
else:
|
||||
filepath = f"data{sep}users{sep}"
|
||||
this_dict = jsonLoad(f"{filepath}{file}.json")
|
||||
string = "this_dict"
|
||||
for arg in args:
|
||||
string += f'["{arg}"]'
|
||||
if type(value) in [str]:
|
||||
value.replace("'", "\'").replace('"', '\"')
|
||||
#if len(value) < 30:
|
||||
# string += f'["{key}"] = "{value}"'
|
||||
#else:
|
||||
string += f'["{key}"] = """{value}"""'
|
||||
else:
|
||||
string += f'["{key}"] = {value}'
|
||||
exec(string)
|
||||
|
||||
this_dict = nested_set(this_dict, keys, value, create_missing=create_missing)
|
||||
jsonSave(this_dict, f"{filepath}{file}.json")
|
||||
return
|
||||
|
||||
|
Reference in New Issue
Block a user