GoldyBot.config

 1import json
 2import GoldyBot
 3
 4MODULE_NAME = "CONFIG"
 5
 6class Config(object):
 7    """A class that acts as an interface to manage enabling and disabling configs in json files."""
 8    
 9    def __init__(self, config_file:GoldyBot.files.File):
10        self.config_file = config_file
11
12        if not (self.config_file.read())[0:1] == "{":
13            with self.config_file.get_file() as file:
14                data = {}
15                file.seek(0)
16                json_string = json.dumps(data, indent=4)
17                file.write(json_string)
18                file.truncate()
19
20    def write(self, config_name:str, value) -> None:
21        """Method that edits a config's value or creates one with the value."""
22        try:
23            # Try to edit value of config.
24            with self.config_file.get_file() as file:
25                data = json.load(file)
26                data[config_name.lower()] = value
27                file.seek(0)
28                json.dump(data, file, indent=4)
29                file.truncate()
30
31            GoldyBot.logging.print_and_log(None, f"[{MODULE_NAME}] Written to config '{config_name.lower()}' in '{self.config_file.file_path}' with the value '{value}'.")
32        
33        except Exception:
34             GoldyBot.logging.print_and_log("error", f"[{MODULE_NAME}] Couldn't edit the config '{config_name}' in '{self.config_file.file_path}' with the value '{value}'!")
35
36    def read(self, config_name:str, convert_to_int:bool=False, convert_to_string:bool=False):
37        """Method that returns the value of a config."""
38        try:
39            with self.config_file.get_file() as file:
40                data = json.loads(file.read())
41
42                if convert_to_int: return int(data[config_name.lower()])
43                if convert_to_string: return str(data[config_name.lower()])
44
45                return data[config_name.lower()]
46
47        except KeyError:
48            return None
49
50        except Exception:
51            GoldyBot.logging.print_and_log("warn", f"[{MODULE_NAME}] Couldn't read value in the config '{config_name.lower()}' from '{self.config_file.file_path}', so were returning 'None'.")
52            return None
53
54    def remove(self, config_name:str):
55        """This method removes the config completly from the json file."""
56        try:
57            with self.config_file.get_file() as file:
58                data = json.load(file)
59                del data[config_name.lower()]
60                file.seek(0)
61                json.dump(data, file, indent=4)
62                file.truncate()
63
64            GoldyBot.logging.print_and_log(None, f"[{MODULE_NAME}] Removed the config '{config_name.lower()}' from '{self.config_file.file_path}'!")
65
66        except Exception:
67            GoldyBot.logging.print_and_log("error", f"[{MODULE_NAME}] Couldn't remove the config from the json file!")
class Config:
 7class Config(object):
 8    """A class that acts as an interface to manage enabling and disabling configs in json files."""
 9    
10    def __init__(self, config_file:GoldyBot.files.File):
11        self.config_file = config_file
12
13        if not (self.config_file.read())[0:1] == "{":
14            with self.config_file.get_file() as file:
15                data = {}
16                file.seek(0)
17                json_string = json.dumps(data, indent=4)
18                file.write(json_string)
19                file.truncate()
20
21    def write(self, config_name:str, value) -> None:
22        """Method that edits a config's value or creates one with the value."""
23        try:
24            # Try to edit value of config.
25            with self.config_file.get_file() as file:
26                data = json.load(file)
27                data[config_name.lower()] = value
28                file.seek(0)
29                json.dump(data, file, indent=4)
30                file.truncate()
31
32            GoldyBot.logging.print_and_log(None, f"[{MODULE_NAME}] Written to config '{config_name.lower()}' in '{self.config_file.file_path}' with the value '{value}'.")
33        
34        except Exception:
35             GoldyBot.logging.print_and_log("error", f"[{MODULE_NAME}] Couldn't edit the config '{config_name}' in '{self.config_file.file_path}' with the value '{value}'!")
36
37    def read(self, config_name:str, convert_to_int:bool=False, convert_to_string:bool=False):
38        """Method that returns the value of a config."""
39        try:
40            with self.config_file.get_file() as file:
41                data = json.loads(file.read())
42
43                if convert_to_int: return int(data[config_name.lower()])
44                if convert_to_string: return str(data[config_name.lower()])
45
46                return data[config_name.lower()]
47
48        except KeyError:
49            return None
50
51        except Exception:
52            GoldyBot.logging.print_and_log("warn", f"[{MODULE_NAME}] Couldn't read value in the config '{config_name.lower()}' from '{self.config_file.file_path}', so were returning 'None'.")
53            return None
54
55    def remove(self, config_name:str):
56        """This method removes the config completly from the json file."""
57        try:
58            with self.config_file.get_file() as file:
59                data = json.load(file)
60                del data[config_name.lower()]
61                file.seek(0)
62                json.dump(data, file, indent=4)
63                file.truncate()
64
65            GoldyBot.logging.print_and_log(None, f"[{MODULE_NAME}] Removed the config '{config_name.lower()}' from '{self.config_file.file_path}'!")
66
67        except Exception:
68            GoldyBot.logging.print_and_log("error", f"[{MODULE_NAME}] Couldn't remove the config from the json file!")

A class that acts as an interface to manage enabling and disabling configs in json files.

Config(config_file: GoldyBot.files.File)
10    def __init__(self, config_file:GoldyBot.files.File):
11        self.config_file = config_file
12
13        if not (self.config_file.read())[0:1] == "{":
14            with self.config_file.get_file() as file:
15                data = {}
16                file.seek(0)
17                json_string = json.dumps(data, indent=4)
18                file.write(json_string)
19                file.truncate()
def write(self, config_name: str, value) -> None:
21    def write(self, config_name:str, value) -> None:
22        """Method that edits a config's value or creates one with the value."""
23        try:
24            # Try to edit value of config.
25            with self.config_file.get_file() as file:
26                data = json.load(file)
27                data[config_name.lower()] = value
28                file.seek(0)
29                json.dump(data, file, indent=4)
30                file.truncate()
31
32            GoldyBot.logging.print_and_log(None, f"[{MODULE_NAME}] Written to config '{config_name.lower()}' in '{self.config_file.file_path}' with the value '{value}'.")
33        
34        except Exception:
35             GoldyBot.logging.print_and_log("error", f"[{MODULE_NAME}] Couldn't edit the config '{config_name}' in '{self.config_file.file_path}' with the value '{value}'!")

Method that edits a config's value or creates one with the value.

def read( self, config_name: str, convert_to_int: bool = False, convert_to_string: bool = False):
37    def read(self, config_name:str, convert_to_int:bool=False, convert_to_string:bool=False):
38        """Method that returns the value of a config."""
39        try:
40            with self.config_file.get_file() as file:
41                data = json.loads(file.read())
42
43                if convert_to_int: return int(data[config_name.lower()])
44                if convert_to_string: return str(data[config_name.lower()])
45
46                return data[config_name.lower()]
47
48        except KeyError:
49            return None
50
51        except Exception:
52            GoldyBot.logging.print_and_log("warn", f"[{MODULE_NAME}] Couldn't read value in the config '{config_name.lower()}' from '{self.config_file.file_path}', so were returning 'None'.")
53            return None

Method that returns the value of a config.

def remove(self, config_name: str):
55    def remove(self, config_name:str):
56        """This method removes the config completly from the json file."""
57        try:
58            with self.config_file.get_file() as file:
59                data = json.load(file)
60                del data[config_name.lower()]
61                file.seek(0)
62                json.dump(data, file, indent=4)
63                file.truncate()
64
65            GoldyBot.logging.print_and_log(None, f"[{MODULE_NAME}] Removed the config '{config_name.lower()}' from '{self.config_file.file_path}'!")
66
67        except Exception:
68            GoldyBot.logging.print_and_log("error", f"[{MODULE_NAME}] Couldn't remove the config from the json file!")

This method removes the config completly from the json file.