Make functions smaller
This commit is contained in:
		| @@ -3,22 +3,19 @@ | |||||||
| Functions: | Functions: | ||||||
|     create_settings_file: |     create_settings_file: | ||||||
|         Create the settings file if it doesn't exist. |         Create the settings file if it doesn't exist. | ||||||
|     get_data_dir: |  | ||||||
|         Path to the data directory. This is where the database file and config file are stored. |  | ||||||
|     get_db_file: |     get_db_file: | ||||||
|         Where we store the database file. |         Where we store the database file. | ||||||
|     read_settings_file: |     read_settings_file: | ||||||
|         Read the settings file and return it as a dict. |         Read the settings file and return it as a dict. | ||||||
|  |  | ||||||
| Variables: | Variables: | ||||||
|     data_directory: |     data_dir: | ||||||
|         The application directory, defaults to user_data_dir(). |         Where we store the database and settings file. | ||||||
|     logger: |     logger: | ||||||
|         The logger for this program. |         The logger for this program. | ||||||
| """ | """ | ||||||
| import logging | import logging | ||||||
| import os | import os | ||||||
| from pathlib import Path |  | ||||||
|  |  | ||||||
| from platformdirs import user_data_dir | from platformdirs import user_data_dir | ||||||
| from reader import Reader, make_reader | from reader import Reader, make_reader | ||||||
| @@ -29,22 +26,25 @@ from tomlkit.toml_document import TOMLDocument | |||||||
| logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] [%(funcName)s:%(lineno)d] %(message)s") | logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] [%(funcName)s:%(lineno)d] %(message)s") | ||||||
| logger: logging.Logger = logging.getLogger(__name__) | logger: logging.Logger = logging.getLogger(__name__) | ||||||
|  |  | ||||||
| # For get_data_dir() | data_dir: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True) | ||||||
| data_directory: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True) | os.makedirs(data_dir, exist_ok=True) | ||||||
|  |  | ||||||
|  |  | ||||||
| def create_settings_file(settings_file) -> None: | def create_settings_file(settings_file_location) -> None: | ||||||
|     """Create the settings file if it doesn't exist.""" |     """Create the settings file if it doesn't exist. | ||||||
|     logger.debug(f"Settings file: {settings_file}") |  | ||||||
|  |     Args: | ||||||
|  |         settings_file_location: The location of the settings file. | ||||||
|  |  | ||||||
|  |     Returns: | ||||||
|  |         None | ||||||
|  |     """ | ||||||
|  |     logger.debug(f"{settings_file_location=}") | ||||||
|  |  | ||||||
|     # [webhooks] |  | ||||||
|     # Both options are commented out by default. |  | ||||||
|     webhooks: Table = table() |     webhooks: Table = table() | ||||||
|     webhooks.add(comment('"First webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"')) |     webhooks.add(comment('"First webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"')) | ||||||
|     webhooks.add(comment('"Second webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"')) |     webhooks.add(comment('"Second webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"')) | ||||||
|  |  | ||||||
|     # [database] |  | ||||||
|     # Option is commented out by default. |  | ||||||
|     database: Table = table() |     database: Table = table() | ||||||
|     database.add(comment('"location" = "/path/to/database/file"')) |     database.add(comment('"location" = "/path/to/database/file"')) | ||||||
|  |  | ||||||
| @@ -52,64 +52,45 @@ def create_settings_file(settings_file) -> None: | |||||||
|     doc.add("webhooks", webhooks) |     doc.add("webhooks", webhooks) | ||||||
|     doc.add("database", database) |     doc.add("database", database) | ||||||
|  |  | ||||||
|     logger.debug(f"Settings file: {doc}") |  | ||||||
|     logger.debug(f"Settings file as TOML: {doc.as_string()}") |  | ||||||
|  |  | ||||||
|     # Write the settings file |     # Write the settings file | ||||||
|     with open(settings_file, "w") as f: |     with open(settings_file_location, "w") as f: | ||||||
|         f.write(doc.as_string()) |         f.write(doc.as_string()) | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_db_file(custom_db_name: str = "db.sqlite") -> Path: | def get_db_location(custom_name: str = "db.sqlite") -> str: | ||||||
|     """Where we store the database file |     """Where we store the database file. | ||||||
|  |  | ||||||
|     Args: |     Args: | ||||||
|         custom_db_name: The name of the database file, defaults to db.sqlite. |         custom_name: The name of the database file, defaults to db.sqlite. | ||||||
|  |  | ||||||
|     Returns: |     Returns: | ||||||
|         Path: The database file. |         The database location. | ||||||
|     """ |     """ | ||||||
|     if custom_db_name != "db.sqlite": |     db_name = os.path.join(data_dir, custom_name) | ||||||
|         logger.info(f"Using custom database file: {custom_db_name}") |     logger.debug(f"{db_name=}{f', with custom db name {custom_name!r}' if custom_name != 'db.sqlite' else ''}") | ||||||
|  |  | ||||||
|     # Store the database file in the data directory |     return db_name | ||||||
|     data_dir = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True) |  | ||||||
|     os.makedirs(data_dir, exist_ok=True) |  | ||||||
|  |  | ||||||
|     db_location: Path = Path(os.path.join(data_dir, custom_db_name)) |  | ||||||
|  |  | ||||||
|     logger.debug(f"Database file: {db_location}") |  | ||||||
|  |  | ||||||
|     return Path(db_location) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def read_settings_file(custom_settings_name: str = "settings.toml") -> TOMLDocument: | def read_settings_file(custom_name: str = "settings.toml") -> TOMLDocument: | ||||||
|     """Read the settings file |     """Read the settings file and return the settings as a dict. | ||||||
|  |  | ||||||
|     Args: |     Args: | ||||||
|         custom_settings_name: The name of the settings file, defaults to settings.toml. |         custom_name: The name of the settings file, defaults to settings.toml. | ||||||
|  |  | ||||||
|     Returns: |     Returns: | ||||||
|         dict: The settings file as a dict. |         dict: The settings file as a dict. | ||||||
|     """ |     """ | ||||||
|     if custom_settings_name != "settings.toml": |  | ||||||
|         logger.info(f"Using custom name for settings file: {custom_settings_name}") |  | ||||||
|  |  | ||||||
|     # Store the database file in the data directory. |     settings_file = os.path.join(data_dir, custom_name) | ||||||
|     data_dir = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True) |     logger.debug(f"{settings_file=}{f', with custom db name {custom_name!r}' if custom_name != 'db.sqlite' else ''}") | ||||||
|     os.makedirs(data_dir, exist_ok=True) |  | ||||||
|  |  | ||||||
|     settings_file_location: Path = Path(os.path.join(data_dir, custom_settings_name)) |     with open(settings_file, encoding="utf-8") as f: | ||||||
|  |         contents: TOMLDocument = parse(f.read()) | ||||||
|  |         logger.debug(f"{contents=}") | ||||||
|  |  | ||||||
|     # Create the settings file if it doesn't exist |         return contents | ||||||
|     if not os.path.exists(settings_file_location): |  | ||||||
|         create_settings_file(settings_file_location) |  | ||||||
|  |  | ||||||
|     with open(settings_file_location, encoding="utf-8") as f: |  | ||||||
|         data: TOMLDocument = parse(f.read()) |  | ||||||
|         logger.debug(f"Contents of settings file: {data}") |  | ||||||
|  |  | ||||||
|         return data |  | ||||||
|  |  | ||||||
|  |  | ||||||
| reader: Reader = make_reader(str(get_db_file())) | db_location: str = get_db_location() | ||||||
|  | reader: Reader = make_reader(db_location) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user