From 03a65a22448ee475e750279fac109801f35e71fe Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Sat, 12 Apr 2025 18:16:27 +0100 Subject: [PATCH] implement toml config --- .gitignore | 2 ++ config/__init__.py | 0 config/configuration.py | 71 +++++++++++++++++++++++++++++++++++++++++ config/create_config.py | 28 ++++++++++++++++ examples/example.py | 12 ++++--- pyproject.toml | 3 +- 6 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 config/__init__.py create mode 100644 config/configuration.py create mode 100644 config/create_config.py diff --git a/.gitignore b/.gitignore index 8bade5a..7c1a13b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ .idea/ keys/ poetry.lock +config.toml +**/*.ipynb diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/config/configuration.py b/config/configuration.py new file mode 100644 index 0000000..c9fcaf4 --- /dev/null +++ b/config/configuration.py @@ -0,0 +1,71 @@ +import toml +from pathlib import Path + + +class Config: + """Configuration class that loads settings from a TOML file""" + + def __init__(self, config_file="config.toml"): + """Initialize configuration from a TOML file + + Args: + config_path (str): Path to the TOML configuration file + """ + self.project_root = Path(__file__).resolve().parent + self.config_path = self.project_root / config_file + self._load_config() + + def _load_config(self): + """Load configuration from TOML file""" + if not self.config_path.exists(): + raise FileNotFoundError(f"Configuration file not found: {self.config_path}") + + # Load configuration + config_data = toml.load(self.config_path) + + # Extract and set attributes + self.username = config_data["user"]["username"] + self.api_key_file = self.project_root.joinpath( + "keys", config_data["api"]["key_file"]) + self.host = config_data["api"]["host"] + + # Load API key + self._load_api_key() + + def _load_api_key(self): + """Load API key from the specified file""" + if not self.api_key_file.exists(): + raise FileNotFoundError(f"API key file not found: {self.api_key_file}") + + with open(self.api_key_file, 'r') as key_file: + self.api_key = key_file.read().strip() + + def get_headers(self): + """Return headers for API requests""" + return { + "api_key": self.api_key, + "api_username": self.username + } + + def get_api_url(self, endpoint=""): + """Return the full API URL for the given endpoint + + Args: + endpoint (str): The API endpoint (e.g., "posts.json") + + Returns: + str: The full API URL + """ + # Handle cases where endpoint may already have leading slash + endpoint = endpoint.lstrip('/') + + # Construct and return the full URL + return f"https://{self.host}/{endpoint}" + + def __str__(self): + """String representation of the configuration""" + return (f"Configuration:\n" + f"- Username: {self.username}\n" + f"- Host: {self.host}\n" + f"- API Key File: {self.api_key_file}\n" + f"- API Key: {'*' * 10}") # Don't show the actual key diff --git a/config/create_config.py b/config/create_config.py new file mode 100644 index 0000000..0161edf --- /dev/null +++ b/config/create_config.py @@ -0,0 +1,28 @@ +import os + +import toml + + +def create_sample_config(config_path="config.toml"): + """Create a sample configuration file if it doesn't exist""" + if os.path.exists(config_path): + print(f"Configuration file already exists: {config_path}") + return + + config_data = { + "user": { + "username": "joe_doakes" + }, + "api": { + "key_file": "../keys/cli_key.txt", + "host": "discourse.forum.com" + } + } + + with open(config_path, "w") as config_file: + toml.dump(config_data, config_file) + + print(f"Created sample configuration file: {config_path}") + +# Create a sample configuration file +create_sample_config() \ No newline at end of file diff --git a/examples/example.py b/examples/example.py index 662197c..08f50b2 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,19 +1,23 @@ +from config.configuration import Config from lunduke.config import DiscourseConfig from lunduke.auth import DiscourseAuth from lunduke.client import DiscourseClient def main(): + config_data = Config() + print(config_data) + # Set up configuration config = DiscourseConfig( - host='forum.lunduke.com', - username='gmgauthier' + host=config_data.host, + username=config_data.username ) # Set up authentication auth = DiscourseAuth( - api_key_file='../keys/cli_key.txt', - username=config.username + api_key_file=config_data.api_key_file, + username=config_data.username ) # Create client diff --git a/pyproject.toml b/pyproject.toml index 03082de..283e670 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,8 @@ authors = [ readme = "README.md" requires-python = ">=3.12" dependencies = [ - "requests (>=2.32.3,<3.0.0)" + "requests (>=2.32.3,<3.0.0)", + "toml (>=0.10.2, <1.0.0)" ]