tickets_plus.database.config
Module for .json file management.
This module is used to manage the .json files used by the bot. Those are: config.json, secret.json, and runtimeconfig.json. The config.json file stores basic configuration information. The secret.json file stores sensitive information such as the bot token, etc. The runtimeconfig.json file stores advanced configuration information, it is not meant to be edited by less experienced users. Wrong values in this file may cause issues with the bot and/or discord API.
Typical usage example:
from tickets_plus.database import config # Make use of the config module # i.e., import and use the Classes
1"""Module for .json file management. 2 3This module is used to manage the .json files used by the bot. 4Those are: config.json, secret.json, and runtimeconfig.json. 5The config.json file stores basic configuration information. 6The secret.json file stores sensitive information such as the bot token, etc. 7The runtimeconfig.json file stores advanced configuration information, it is 8not meant to be edited by less experienced users. Wrong values in this file 9may cause issues with the bot and/or discord API. 10 11Typical usage example: 12 ```py 13 from tickets_plus.database import config 14 # Make use of the config module 15 # i.e., import and use the Classes 16 ``` 17""" 18# License: EPL-2.0 19# SPDX-License-Identifier: EPL-2.0 20# Copyright (c) 2021-present The Tickets+ Contributors 21# This Source Code may also be made available under the following 22# Secondary Licenses when the conditions for such availability set forth 23# in the Eclipse Public License, v. 2.0 are satisfied: GPL-3.0-only OR 24# If later approved by the Initial Contributor, GPL-3.0-or-later. 25 26import json 27import logging 28import pathlib 29from typing import Any 30 31import sqlalchemy 32 33from tickets_plus.database import const 34 35 36class Secret: 37 """Class for secret.json management 38 39 This class is used to manage the secret.json file. 40 It is used to store sensitive information such as the bot token. 41 This class is used to obfuscate the token when printing the object. 42 Also, this class is used to make it easier to access the token. 43 44 Attributes: 45 token: The bot token. 46 secrets: The raw dictionary of the secret.json file. 47 """ 48 49 token: str 50 secrets: dict[str, Any] 51 52 def __init__(self) -> None: 53 """Loads the secret.json file and stores the data in `self.secrets` 54 55 We load the secret.json file and store the data in self.secrets. 56 We also store the token in `self.token` for easy access. 57 """ 58 self._file = pathlib.Path(const.PROG_DIR, "secret.json") 59 try: 60 with open(self._file, encoding="utf-8") as secret_f: 61 self.secrets = json.load(secret_f) 62 self.token: str = self.secrets["token"] 63 self.ssl_key: str = self.secrets["ssl_key"] 64 except FileNotFoundError: 65 logging.warning("Running in dry-run mode.") 66 self._file = pathlib.Path(const.PROG_DIR, "example_secret.json") 67 with open(self._file, encoding="utf-8") as secret_f: 68 self.secrets = json.load(secret_f) 69 self.token: str = self.secrets["token"] 70 self.ssl_key: str = self.secrets["ssl_key"] 71 72 def __repr__(self) -> str: 73 return "[OBFUSCATED]" 74 75 def __str__(self) -> str: 76 return "[OBFUSCATED]" 77 78 79class MiniConfig: 80 """Class for new config.json management 81 82 This class is used to manage the config.json file. 83 It is used to store non-sensitive information such as the bot prefix. 84 It does not allow for modification of the config.json file. 85 As any functionality that should modify the config.json file should be 86 instead implemented in the `tickets_plus.database.layer.OnlineConfig` class. 87 """ 88 89 def __init__(self) -> None: 90 self._file = pathlib.Path(const.PROG_DIR, "config.json") 91 try: 92 with open(self._file, encoding="utf-8") as config_f: 93 self._config: dict = json.load(config_f) 94 except FileNotFoundError: 95 logging.warning("Running in dry-run mode.") 96 self._file = pathlib.Path(const.PROG_DIR, "example_config.json") 97 with open(self._file, encoding="utf-8") as config_f: 98 self._config: dict = json.load(config_f) 99 100 def __dict__(self) -> dict: 101 return self._config 102 103 def getitem(self, key: str, opt: Any = None) -> Any: 104 """Returns the value of a key in the config.json file 105 106 Args: 107 key: The key to get the value of 108 opt: The default value to return if the key is not found 109 110 Returns: 111 Any: The value of the key in the config.json file 112 or the default value if the key is not found. 113 """ 114 return self._config.get(key, opt) 115 116 def get_url(self) -> sqlalchemy.URL: 117 """Returns the database URL 118 119 Returns: 120 `sqlalchemy.URL`: The database URL as a sqlalchemy URL object 121 """ 122 return sqlalchemy.URL.create( 123 drivername=self._config["dbtype"], 124 host=self._config["dbhost"], 125 port=self._config["dbport"], 126 username=self._config["dbuser"], 127 password=self._config["dbpass"], 128 database=self._config["dbname"], 129 ) 130 131 132class RuntimeConfig: 133 """Advanced low-level configuration parameters 134 135 This class is used to manage the runtimeconfig.json file. 136 It is used to store advanced configuration information. 137 It is not meant to be edited by less experienced users. 138 139 Parameters: 140 spt_clean_usr: Clean user roles seconds per tick 141 spt_notif_usr: Notify user seconds per tick 142 """ 143 144 def __init__(self) -> None: 145 self._file = pathlib.Path(const.PROG_DIR, "runtimeconfig.json") 146 with open(self._file, encoding="utf-8") as config_f: 147 self._config: dict = json.load(config_f) 148 149 def __dict__(self) -> dict: 150 return self._config 151 152 @property 153 def spt_clean_usr(self) -> int: 154 """Returns the clean user roles seconds per tick 155 156 Returns: 157 int: The clean user roles seconds per tick 158 """ 159 return self._config["spt"]["clean_usr"] 160 161 @property 162 def spt_notif_usr(self) -> int: 163 """Returns the notify user seconds per tick 164 165 Returns: 166 int: The notify user seconds per tick 167 """ 168 return self._config["spt"]["notif_usr"]
37class Secret: 38 """Class for secret.json management 39 40 This class is used to manage the secret.json file. 41 It is used to store sensitive information such as the bot token. 42 This class is used to obfuscate the token when printing the object. 43 Also, this class is used to make it easier to access the token. 44 45 Attributes: 46 token: The bot token. 47 secrets: The raw dictionary of the secret.json file. 48 """ 49 50 token: str 51 secrets: dict[str, Any] 52 53 def __init__(self) -> None: 54 """Loads the secret.json file and stores the data in `self.secrets` 55 56 We load the secret.json file and store the data in self.secrets. 57 We also store the token in `self.token` for easy access. 58 """ 59 self._file = pathlib.Path(const.PROG_DIR, "secret.json") 60 try: 61 with open(self._file, encoding="utf-8") as secret_f: 62 self.secrets = json.load(secret_f) 63 self.token: str = self.secrets["token"] 64 self.ssl_key: str = self.secrets["ssl_key"] 65 except FileNotFoundError: 66 logging.warning("Running in dry-run mode.") 67 self._file = pathlib.Path(const.PROG_DIR, "example_secret.json") 68 with open(self._file, encoding="utf-8") as secret_f: 69 self.secrets = json.load(secret_f) 70 self.token: str = self.secrets["token"] 71 self.ssl_key: str = self.secrets["ssl_key"] 72 73 def __repr__(self) -> str: 74 return "[OBFUSCATED]" 75 76 def __str__(self) -> str: 77 return "[OBFUSCATED]"
Class for secret.json management
This class is used to manage the secret.json file. It is used to store sensitive information such as the bot token. This class is used to obfuscate the token when printing the object. Also, this class is used to make it easier to access the token.
Attributes:
- token: The bot token.
- secrets: The raw dictionary of the secret.json file.
53 def __init__(self) -> None: 54 """Loads the secret.json file and stores the data in `self.secrets` 55 56 We load the secret.json file and store the data in self.secrets. 57 We also store the token in `self.token` for easy access. 58 """ 59 self._file = pathlib.Path(const.PROG_DIR, "secret.json") 60 try: 61 with open(self._file, encoding="utf-8") as secret_f: 62 self.secrets = json.load(secret_f) 63 self.token: str = self.secrets["token"] 64 self.ssl_key: str = self.secrets["ssl_key"] 65 except FileNotFoundError: 66 logging.warning("Running in dry-run mode.") 67 self._file = pathlib.Path(const.PROG_DIR, "example_secret.json") 68 with open(self._file, encoding="utf-8") as secret_f: 69 self.secrets = json.load(secret_f) 70 self.token: str = self.secrets["token"] 71 self.ssl_key: str = self.secrets["ssl_key"]
Loads the secret.json file and stores the data in self.secrets
We load the secret.json file and store the data in self.secrets.
We also store the token in self.token for easy access.
80class MiniConfig: 81 """Class for new config.json management 82 83 This class is used to manage the config.json file. 84 It is used to store non-sensitive information such as the bot prefix. 85 It does not allow for modification of the config.json file. 86 As any functionality that should modify the config.json file should be 87 instead implemented in the `tickets_plus.database.layer.OnlineConfig` class. 88 """ 89 90 def __init__(self) -> None: 91 self._file = pathlib.Path(const.PROG_DIR, "config.json") 92 try: 93 with open(self._file, encoding="utf-8") as config_f: 94 self._config: dict = json.load(config_f) 95 except FileNotFoundError: 96 logging.warning("Running in dry-run mode.") 97 self._file = pathlib.Path(const.PROG_DIR, "example_config.json") 98 with open(self._file, encoding="utf-8") as config_f: 99 self._config: dict = json.load(config_f) 100 101 def __dict__(self) -> dict: 102 return self._config 103 104 def getitem(self, key: str, opt: Any = None) -> Any: 105 """Returns the value of a key in the config.json file 106 107 Args: 108 key: The key to get the value of 109 opt: The default value to return if the key is not found 110 111 Returns: 112 Any: The value of the key in the config.json file 113 or the default value if the key is not found. 114 """ 115 return self._config.get(key, opt) 116 117 def get_url(self) -> sqlalchemy.URL: 118 """Returns the database URL 119 120 Returns: 121 `sqlalchemy.URL`: The database URL as a sqlalchemy URL object 122 """ 123 return sqlalchemy.URL.create( 124 drivername=self._config["dbtype"], 125 host=self._config["dbhost"], 126 port=self._config["dbport"], 127 username=self._config["dbuser"], 128 password=self._config["dbpass"], 129 database=self._config["dbname"], 130 )
Class for new config.json management
This class is used to manage the config.json file.
It is used to store non-sensitive information such as the bot prefix.
It does not allow for modification of the config.json file.
As any functionality that should modify the config.json file should be
instead implemented in the tickets_plus.database.layer.OnlineConfig class.
104 def getitem(self, key: str, opt: Any = None) -> Any: 105 """Returns the value of a key in the config.json file 106 107 Args: 108 key: The key to get the value of 109 opt: The default value to return if the key is not found 110 111 Returns: 112 Any: The value of the key in the config.json file 113 or the default value if the key is not found. 114 """ 115 return self._config.get(key, opt)
Returns the value of a key in the config.json file
Arguments:
- key: The key to get the value of
- opt: The default value to return if the key is not found
Returns:
Any: The value of the key in the config.json file or the default value if the key is not found.
117 def get_url(self) -> sqlalchemy.URL: 118 """Returns the database URL 119 120 Returns: 121 `sqlalchemy.URL`: The database URL as a sqlalchemy URL object 122 """ 123 return sqlalchemy.URL.create( 124 drivername=self._config["dbtype"], 125 host=self._config["dbhost"], 126 port=self._config["dbport"], 127 username=self._config["dbuser"], 128 password=self._config["dbpass"], 129 database=self._config["dbname"], 130 )
Returns the database URL
Returns:
sqlalchemy.URL: The database URL as a sqlalchemy URL object
133class RuntimeConfig: 134 """Advanced low-level configuration parameters 135 136 This class is used to manage the runtimeconfig.json file. 137 It is used to store advanced configuration information. 138 It is not meant to be edited by less experienced users. 139 140 Parameters: 141 spt_clean_usr: Clean user roles seconds per tick 142 spt_notif_usr: Notify user seconds per tick 143 """ 144 145 def __init__(self) -> None: 146 self._file = pathlib.Path(const.PROG_DIR, "runtimeconfig.json") 147 with open(self._file, encoding="utf-8") as config_f: 148 self._config: dict = json.load(config_f) 149 150 def __dict__(self) -> dict: 151 return self._config 152 153 @property 154 def spt_clean_usr(self) -> int: 155 """Returns the clean user roles seconds per tick 156 157 Returns: 158 int: The clean user roles seconds per tick 159 """ 160 return self._config["spt"]["clean_usr"] 161 162 @property 163 def spt_notif_usr(self) -> int: 164 """Returns the notify user seconds per tick 165 166 Returns: 167 int: The notify user seconds per tick 168 """ 169 return self._config["spt"]["notif_usr"]
Advanced low-level configuration parameters
This class is used to manage the runtimeconfig.json file. It is used to store advanced configuration information. It is not meant to be edited by less experienced users.
Arguments:
- spt_clean_usr: Clean user roles seconds per tick
- spt_notif_usr: Notify user seconds per tick
153 @property 154 def spt_clean_usr(self) -> int: 155 """Returns the clean user roles seconds per tick 156 157 Returns: 158 int: The clean user roles seconds per tick 159 """ 160 return self._config["spt"]["clean_usr"]
Returns the clean user roles seconds per tick
Returns:
int: The clean user roles seconds per tick
162 @property 163 def spt_notif_usr(self) -> int: 164 """Returns the notify user seconds per tick 165 166 Returns: 167 int: The notify user seconds per tick 168 """ 169 return self._config["spt"]["notif_usr"]
Returns the notify user seconds per tick
Returns:
int: The notify user seconds per tick