From 965771fb0d85ddb27dc6c5dd7df822d1fb318286 Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Sat, 15 Mar 2025 16:35:39 +0800 Subject: refactor: clean up code formatting and add new PluginManager class --- src/conventionalrp/__main__.py | 2 ++ src/conventionalrp/_core.pyi | 2 +- src/conventionalrp/core/__init__.py | 2 +- src/conventionalrp/core/parser.py | 7 ++++--- src/conventionalrp/core/processor.py | 15 ++++++++++----- src/conventionalrp/extractors/__init__.py | 2 +- src/conventionalrp/extractors/base.py | 2 +- src/conventionalrp/extractors/rule_extractor.py | 5 +++-- src/conventionalrp/plugins/__init__.py | 2 +- src/conventionalrp/plugins/plugin_manager.py | 19 +++++++++++++++++++ src/conventionalrp/renderers/__init__.py | 2 +- src/conventionalrp/renderers/base.py | 2 +- src/conventionalrp/renderers/html_renderer.py | 11 ++++++----- src/conventionalrp/renderers/json_renderer.py | 4 +++- src/conventionalrp/renderers/markdown_renderer.py | 9 +++++---- src/conventionalrp/utils/__init__.py | 2 +- 16 files changed, 60 insertions(+), 28 deletions(-) (limited to 'src/conventionalrp') diff --git a/src/conventionalrp/__main__.py b/src/conventionalrp/__main__.py index 01b510d..fa4f45e 100644 --- a/src/conventionalrp/__main__.py +++ b/src/conventionalrp/__main__.py @@ -1,7 +1,9 @@ from ._core import sum_as_string + def main(): print(sum_as_string(1, 2)) + if __name__ == "__main__": main() diff --git a/src/conventionalrp/_core.pyi b/src/conventionalrp/_core.pyi index 8a0cc40..0805887 100644 --- a/src/conventionalrp/_core.pyi +++ b/src/conventionalrp/_core.pyi @@ -1,3 +1,3 @@ def sum_as_string(a: int, b: int) -> str: ... -class Base: ... \ No newline at end of file +class Base: ... diff --git a/src/conventionalrp/core/__init__.py b/src/conventionalrp/core/__init__.py index d63b2a9..cc59ba4 100644 --- a/src/conventionalrp/core/__init__.py +++ b/src/conventionalrp/core/__init__.py @@ -1,4 +1,4 @@ # FILE: /trpg-log-processor/trpg-log-processor/src/conventionalrp/core/__init__.py """ This file initializes the core module of the conventionalrp SDK. -""" \ No newline at end of file +""" diff --git a/src/conventionalrp/core/parser.py b/src/conventionalrp/core/parser.py index 266506b..d5b91da 100644 --- a/src/conventionalrp/core/parser.py +++ b/src/conventionalrp/core/parser.py @@ -2,18 +2,19 @@ import json import re from pathlib import Path + class Parser: def __init__(self): self.rules = [] - def load_rules(self, rules_path : str): + def load_rules(self, rules_path: str): """Load parsing rules.""" if not Path(rules_path).exists(): raise FileNotFoundError(f"No such file or directory: {rules_path} ") with open(rules_path, "r", encoding="utf-8") as f: file_content = f.read() - + rules = json.loads(file_content) # validation rule format @@ -53,4 +54,4 @@ class Parser: else: parsed_data.append({"content": line.strip(), "type": "unknown"}) - return parsed_data \ No newline at end of file + return parsed_data diff --git a/src/conventionalrp/core/processor.py b/src/conventionalrp/core/processor.py index 40033cf..4e2f573 100644 --- a/src/conventionalrp/core/processor.py +++ b/src/conventionalrp/core/processor.py @@ -17,23 +17,28 @@ class Processor: def generate_output(self, processed_data, format_type): # Implement output generation logic based on format_type - if format_type == 'json': + if format_type == "json": return self.generate_json_output(processed_data) - elif format_type == 'html': + elif format_type == "html": return self.generate_html_output(processed_data) - elif format_type == 'markdown': + elif format_type == "markdown": return self.generate_markdown_output(processed_data) else: raise ValueError("Unsupported format type") def generate_json_output(self, processed_data): import json + return json.dumps(processed_data) def generate_html_output(self, processed_data): # Implement HTML output generation - return "" + "".join(f"
{data}
" for data in processed_data) + "" + return ( + "" + + "".join(f"{data}
" for data in processed_data) + + "" + ) def generate_markdown_output(self, processed_data): # Implement Markdown output generation - return "\n".join(f"- {data}" for data in processed_data) \ No newline at end of file + return "\n".join(f"- {data}" for data in processed_data) diff --git a/src/conventionalrp/extractors/__init__.py b/src/conventionalrp/extractors/__init__.py index c076f07..8606862 100644 --- a/src/conventionalrp/extractors/__init__.py +++ b/src/conventionalrp/extractors/__init__.py @@ -1,4 +1,4 @@ # FILE: /trpg-log-processor/trpg-log-processor/src/conventionalrp/extractors/__init__.py """ This file initializes the extractors module. -""" \ No newline at end of file +""" diff --git a/src/conventionalrp/extractors/base.py b/src/conventionalrp/extractors/base.py index fc42f29..bc3c41f 100644 --- a/src/conventionalrp/extractors/base.py +++ b/src/conventionalrp/extractors/base.py @@ -6,4 +6,4 @@ class BaseExtractor: raise NotImplementedError("Subclasses should implement this method.") def load_rules(self, rules_source): - raise NotImplementedError("Subclasses should implement this method.") \ No newline at end of file + raise NotImplementedError("Subclasses should implement this method.") diff --git a/src/conventionalrp/extractors/rule_extractor.py b/src/conventionalrp/extractors/rule_extractor.py index 78e8505..b0d03d5 100644 --- a/src/conventionalrp/extractors/rule_extractor.py +++ b/src/conventionalrp/extractors/rule_extractor.py @@ -13,7 +13,8 @@ class RuleExtractor(BaseExtractor): def load_rules_from_file(self): import json - with open(self.config_file, 'r') as file: + + with open(self.config_file, "r") as file: return json.load(file) def extract(self): @@ -21,4 +22,4 @@ class RuleExtractor(BaseExtractor): extracted_rules = [] for rule in self.rules: extracted_rules.append(rule) # Placeholder for actual extraction logic - return extracted_rules \ No newline at end of file + return extracted_rules diff --git a/src/conventionalrp/plugins/__init__.py b/src/conventionalrp/plugins/__init__.py index 9f1a11b..a7e5268 100644 --- a/src/conventionalrp/plugins/__init__.py +++ b/src/conventionalrp/plugins/__init__.py @@ -1,4 +1,4 @@ # FILE: /trpg-log-processor/trpg-log-processor/src/conventionalrp/plugins/__init__.py """ This file initializes the plugins module. -""" \ No newline at end of file +""" diff --git a/src/conventionalrp/plugins/plugin_manager.py b/src/conventionalrp/plugins/plugin_manager.py index e69de29..0d49a9c 100644 --- a/src/conventionalrp/plugins/plugin_manager.py +++ b/src/conventionalrp/plugins/plugin_manager.py @@ -0,0 +1,19 @@ +import os +import importlib + + +class PluginManager: + def __init__(self, plugin_dir: str): + self.plugin_dir = plugin_dir + self.plugins = [] + + def load_plugins(self): + for plugin in os.listdir(self.plugin_dir): + if plugin.endswith(".py"): + plugin_name = plugin.split(".")[0] + module = importlib.import_module(f"{self.plugin_dir}.{plugin_name}") + self.plugins.append(module) + + def run_plugins(self): + for plugin in self.plugins: + plugin.run() diff --git a/src/conventionalrp/renderers/__init__.py b/src/conventionalrp/renderers/__init__.py index 7838674..06f0f73 100644 --- a/src/conventionalrp/renderers/__init__.py +++ b/src/conventionalrp/renderers/__init__.py @@ -1,4 +1,4 @@ # FILE: /trpg-log-processor/trpg-log-processor/src/conventionalrp/renderers/__init__.py """ This file initializes the renderers module. -""" \ No newline at end of file +""" diff --git a/src/conventionalrp/renderers/base.py b/src/conventionalrp/renderers/base.py index 498adec..595f9bf 100644 --- a/src/conventionalrp/renderers/base.py +++ b/src/conventionalrp/renderers/base.py @@ -3,4 +3,4 @@ class BaseRenderer: raise NotImplementedError("Render method must be implemented by subclasses.") def set_style(self, style): - self.style = style \ No newline at end of file + self.style = style diff --git a/src/conventionalrp/renderers/html_renderer.py b/src/conventionalrp/renderers/html_renderer.py index 3c30549..75efcd3 100644 --- a/src/conventionalrp/renderers/html_renderer.py +++ b/src/conventionalrp/renderers/html_renderer.py @@ -1,21 +1,22 @@ from .base import BaseRenderer + class HTMLRenderer(BaseRenderer): def __init__(self): super().__init__() self.title = "TRPG Log Output" - + def render(self, data): html_content = f"