diff options
| author | 2025-03-15 16:35:39 +0800 | |
|---|---|---|
| committer | 2025-03-15 16:35:39 +0800 | |
| commit | 965771fb0d85ddb27dc6c5dd7df822d1fb318286 (patch) | |
| tree | f33a0d36e5716da13be0e3a4134cf7359ebe5223 | |
| parent | 818d84a7bf00c07b7ab252dda5bfa70a98b8be65 (diff) | |
| download | conventional_role_play-965771fb0d85ddb27dc6c5dd7df822d1fb318286.tar.gz conventional_role_play-965771fb0d85ddb27dc6c5dd7df822d1fb318286.zip | |
refactor: clean up code formatting and add new PluginManager class
| -rw-r--r-- | docs/source/config.py | 24 | ||||
| -rw-r--r-- | examples/basic_usage.py | 8 | ||||
| -rw-r--r-- | examples/custom_plugin.py | 13 | ||||
| -rw-r--r-- | src/conventionalrp/__main__.py | 2 | ||||
| -rw-r--r-- | src/conventionalrp/_core.pyi | 2 | ||||
| -rw-r--r-- | src/conventionalrp/core/__init__.py | 2 | ||||
| -rw-r--r-- | src/conventionalrp/core/parser.py | 7 | ||||
| -rw-r--r-- | src/conventionalrp/core/processor.py | 15 | ||||
| -rw-r--r-- | src/conventionalrp/extractors/__init__.py | 2 | ||||
| -rw-r--r-- | src/conventionalrp/extractors/base.py | 2 | ||||
| -rw-r--r-- | src/conventionalrp/extractors/rule_extractor.py | 5 | ||||
| -rw-r--r-- | src/conventionalrp/plugins/__init__.py | 2 | ||||
| -rw-r--r-- | src/conventionalrp/plugins/plugin_manager.py | 19 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/__init__.py | 2 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/base.py | 2 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/html_renderer.py | 11 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/json_renderer.py | 4 | ||||
| -rw-r--r-- | src/conventionalrp/renderers/markdown_renderer.py | 9 | ||||
| -rw-r--r-- | src/conventionalrp/utils/__init__.py | 2 | ||||
| -rw-r--r-- | tests/test_pyo3.py | 2 |
20 files changed, 87 insertions, 48 deletions
diff --git a/docs/source/config.py b/docs/source/config.py index fa4794e..83c0158 100644 --- a/docs/source/config.py +++ b/docs/source/config.py @@ -10,9 +10,11 @@ if sys.version_info >= (3, 11): else: import tomli as tomllib + def setup(app): - app.add_config_value('releaselevel', '', 'env') - + app.add_config_value("releaselevel", "", "env") + + DATA = None PYPROJECT = os.path.join("..", "..", "Cargo.toml") with open(PYPROJECT, "r", encoding="utf8") as f: @@ -55,17 +57,17 @@ extensions = [ "myst_parser", ] -doctest_global_setup = ''' +doctest_global_setup = """ try: import hydro_roll as hr import hydro_roll_core as hrc except ImportError: hr = None hrc = None -''' +""" todo_include_todos = True todo_emit_warnings = True -intersphinx_mapping = {'python': ('https://docs.python.org/3', None)} +intersphinx_mapping = {"python": ("https://docs.python.org/3", None)} templates_path = ["_templates"] exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] extlinks = { @@ -90,13 +92,13 @@ rst_epilog = """ locale_dirs = ["../locales/"] # path is example but recommended. gettext_compact = False # optional. gettext_uuid = True # optional. -numfig = True # Figures, tables and code blocks are automatically numbered if they have a title -pygments_style = "rrt" # default sphinx, change the style of code block -math_number_all = True # Number all equations, figures, tables and code blocks +numfig = True # Figures, tables and code blocks are automatically numbered if they have a title +pygments_style = "rrt" # default sphinx, change the style of code block +math_number_all = True # Number all equations, figures, tables and code blocks html_additional_pages = { - 'copy': 'copying.html', + "copy": "copying.html", } -html_split_index = True # Split the index page by each alphabet +html_split_index = True # Split the index page by each alphabet # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output @@ -146,4 +148,4 @@ html_theme_options = { # html_sidebars = { # '**': ['globaltoc.html', 'sourcelink.html', 'searchbox.html', 'relations.html'], # 'using/windows': ['windowssidebar.html', 'searchbox.html'], -# }
\ No newline at end of file +# } diff --git a/examples/basic_usage.py b/examples/basic_usage.py index 22bfbe4..7a4e53d 100644 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -3,10 +3,11 @@ from conventionalrp.core.processor import Processor from conventionalrp.extractors.rule_extractor import RuleExtractor from conventionalrp.renderers.html_renderer import HTMLRenderer + def main(): # Initialize the parser and load rules parser = Parser() - parser.load_rules('path/to/rules.json') + parser.load_rules("path/to/rules.json") # Parse the TRPG log log_data = "Your TRPG log data here" @@ -14,7 +15,7 @@ def main(): # Initialize the rule extractor extractor = RuleExtractor() - rules = extractor.extract('path/to/rules.json') + rules = extractor.extract("path/to/rules.json") # Process the parsed tokens processor = Processor() @@ -27,5 +28,6 @@ def main(): # Print or save the output print(output) + if __name__ == "__main__": - main()
\ No newline at end of file + main() diff --git a/examples/custom_plugin.py b/examples/custom_plugin.py index 5ae6581..ecb9e71 100644 --- a/examples/custom_plugin.py +++ b/examples/custom_plugin.py @@ -1,25 +1,28 @@ from conventionalrp.plugins.plugin_manager import PluginManager + class CustomPlugin: def __init__(self): self.name = "Custom Plugin" - + def process(self, data): # Custom processing logic processed_data = data.upper() # Example transformation return processed_data + def main(): plugin_manager = PluginManager() custom_plugin = CustomPlugin() - + plugin_manager.register_plugin(custom_plugin) - + # Example data to process data = "This is a sample TRPG log." result = custom_plugin.process(data) - + print(f"Processed Data: {result}") + if __name__ == "__main__": - main()
\ No newline at end of file + main() 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 "<html><body>" + "".join(f"<p>{data}</p>" for data in processed_data) + "</body></html>" + return ( + "<html><body>" + + "".join(f"<p>{data}</p>" for data in processed_data) + + "</body></html>" + ) 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"<html><head><title>{self.title}</title></head><body>" html_content += "<h1>TRPG Log Output</h1>" html_content += "<ul>" - + for entry in data: html_content += f"<li>{entry}</li>" - + html_content += "</ul></body></html>" return html_content - + def set_style(self, style): # Implement style setting if needed - pass
\ No newline at end of file + pass diff --git a/src/conventionalrp/renderers/json_renderer.py b/src/conventionalrp/renderers/json_renderer.py index 4313001..8dcdd6d 100644 --- a/src/conventionalrp/renderers/json_renderer.py +++ b/src/conventionalrp/renderers/json_renderer.py @@ -1,9 +1,11 @@ from .base import BaseRenderer + class JSONRenderer(BaseRenderer): def render(self, data): import json + return json.dumps(data, ensure_ascii=False, indent=4) def set_style(self, style): - self.style = style # Placeholder for potential styling options in the future
\ No newline at end of file + self.style = style # Placeholder for potential styling options in the future diff --git a/src/conventionalrp/renderers/markdown_renderer.py b/src/conventionalrp/renderers/markdown_renderer.py index 396d6a4..fab429f 100644 --- a/src/conventionalrp/renderers/markdown_renderer.py +++ b/src/conventionalrp/renderers/markdown_renderer.py @@ -1,13 +1,14 @@ from .base import BaseRenderer + class MarkdownRenderer(BaseRenderer): def render(self, data): """ Renders the given data in Markdown format. - + Args: data (dict): The data to render. - + Returns: str: The rendered Markdown string. """ @@ -19,8 +20,8 @@ class MarkdownRenderer(BaseRenderer): def set_style(self, style): """ Sets the style for the Markdown renderer. - + Args: style (dict): A dictionary of style options. """ - self.style = style # Currently, Markdown does not support styling, but this can be extended.
\ No newline at end of file + self.style = style # Currently, Markdown does not support styling, but this can be extended. diff --git a/src/conventionalrp/utils/__init__.py b/src/conventionalrp/utils/__init__.py index 44b1840..33b66e4 100644 --- a/src/conventionalrp/utils/__init__.py +++ b/src/conventionalrp/utils/__init__.py @@ -1,2 +1,2 @@ # FILE: /trpg-log-processor/trpg-log-processor/src/conventionalrp/utils/__init__.py -"""This file initializes the utils module."""
\ No newline at end of file +"""This file initializes the utils module.""" diff --git a/tests/test_pyo3.py b/tests/test_pyo3.py index 2948da8..9668519 100644 --- a/tests/test_pyo3.py +++ b/tests/test_pyo3.py @@ -1,4 +1,4 @@ from conventionalrp._core import sum_as_string if __name__ == "__main__": - print(sum_as_string(1, 2))
\ No newline at end of file + print(sum_as_string(1, 2)) |
