diff --git a/MANIFEST.in b/MANIFEST.in index 043e60be..4b8d62b8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -8,7 +8,7 @@ recursive-include pkg *.py recursive-include libs *.py # Include templates and resources -recursive-include templates * +recursive-include langbot/templates * recursive-include res * # Include compiled frontend files (will be added during build) diff --git a/templates/__init__.py b/langbot/templates/__init__.py similarity index 100% rename from templates/__init__.py rename to langbot/templates/__init__.py diff --git a/templates/config.yaml b/langbot/templates/config.yaml similarity index 100% rename from templates/config.yaml rename to langbot/templates/config.yaml diff --git a/templates/default-pipeline-config.json b/langbot/templates/default-pipeline-config.json similarity index 100% rename from templates/default-pipeline-config.json rename to langbot/templates/default-pipeline-config.json diff --git a/templates/legacy/command.json b/langbot/templates/legacy/command.json similarity index 100% rename from templates/legacy/command.json rename to langbot/templates/legacy/command.json diff --git a/templates/legacy/pipeline.json b/langbot/templates/legacy/pipeline.json similarity index 100% rename from templates/legacy/pipeline.json rename to langbot/templates/legacy/pipeline.json diff --git a/templates/legacy/platform.json b/langbot/templates/legacy/platform.json similarity index 100% rename from templates/legacy/platform.json rename to langbot/templates/legacy/platform.json diff --git a/templates/legacy/provider.json b/langbot/templates/legacy/provider.json similarity index 100% rename from templates/legacy/provider.json rename to langbot/templates/legacy/provider.json diff --git a/templates/legacy/system.json b/langbot/templates/legacy/system.json similarity index 100% rename from templates/legacy/system.json rename to langbot/templates/legacy/system.json diff --git a/templates/metadata/pipeline/ai.yaml b/langbot/templates/metadata/pipeline/ai.yaml similarity index 100% rename from templates/metadata/pipeline/ai.yaml rename to langbot/templates/metadata/pipeline/ai.yaml diff --git a/templates/metadata/pipeline/output.yaml b/langbot/templates/metadata/pipeline/output.yaml similarity index 100% rename from templates/metadata/pipeline/output.yaml rename to langbot/templates/metadata/pipeline/output.yaml diff --git a/templates/metadata/pipeline/safety.yaml b/langbot/templates/metadata/pipeline/safety.yaml similarity index 100% rename from templates/metadata/pipeline/safety.yaml rename to langbot/templates/metadata/pipeline/safety.yaml diff --git a/templates/metadata/pipeline/trigger.yaml b/langbot/templates/metadata/pipeline/trigger.yaml similarity index 100% rename from templates/metadata/pipeline/trigger.yaml rename to langbot/templates/metadata/pipeline/trigger.yaml diff --git a/templates/metadata/sensitive-words.json b/langbot/templates/metadata/sensitive-words.json similarity index 100% rename from templates/metadata/sensitive-words.json rename to langbot/templates/metadata/sensitive-words.json diff --git a/pkg/config/impls/yaml.py b/pkg/config/impls/yaml.py index 1ba47353..bf013629 100644 --- a/pkg/config/impls/yaml.py +++ b/pkg/config/impls/yaml.py @@ -1,6 +1,6 @@ import os -import shutil import yaml +import importlib.resources as resources from .. import model as file_model @@ -11,29 +11,27 @@ class YAMLConfigFile(file_model.ConfigFile): def __init__( self, config_file_name: str, - template_file_name: str = None, + template_resource_name: str = None, template_data: dict = None, ) -> None: self.config_file_name = config_file_name - self.template_file_name = template_file_name + self.template_resource_name = template_resource_name self.template_data = template_data - def _get_template_path(self) -> str: - """Get the actual path to the template file, handling package installation""" - if self.template_file_name is None: - return None - - from ...utils import paths as path_utils - return path_utils.get_resource_path(self.template_file_name) - def exists(self) -> bool: return os.path.exists(self.config_file_name) + async def get_template_file_str(self) -> str: + if self.template_resource_name is None: + return None + + with resources.path('langbot.templates', self.template_resource_name) as path: + return path.open('r', encoding='utf-8').read() + async def create(self): - template_path = self._get_template_path() - - if template_path is not None: - shutil.copyfile(template_path, self.config_file_name) + if await self.get_template_file_str() is not None: + with open(self.config_file_name, 'w', encoding='utf-8') as f: + f.write(await self.get_template_file_str()) elif self.template_data is not None: with open(self.config_file_name, 'w', encoding='utf-8') as f: yaml.dump(self.template_data, f, indent=4, allow_unicode=True) @@ -44,11 +42,10 @@ class YAMLConfigFile(file_model.ConfigFile): if not self.exists(): await self.create() - template_path = self._get_template_path() - - if template_path is not None: - with open(template_path, 'r', encoding='utf-8') as f: - self.template_data = yaml.load(f, Loader=yaml.FullLoader) + template_file_str = await self.get_template_file_str() + + if template_file_str is not None: + self.template_data = yaml.load(template_file_str, Loader=yaml.FullLoader) with open(self.config_file_name, 'r', encoding='utf-8') as f: try: diff --git a/pkg/config/manager.py b/pkg/config/manager.py index d552b038..405c89d5 100644 --- a/pkg/config/manager.py +++ b/pkg/config/manager.py @@ -63,6 +63,7 @@ async def load_python_module_config(config_name: str, template_name: str, comple async def load_json_config( config_name: str, template_name: str = None, + template_resource_name: str = None, template_data: dict = None, completion: bool = True, ) -> ConfigManager: @@ -85,6 +86,7 @@ async def load_json_config( async def load_yaml_config( config_name: str, template_name: str = None, + template_resource_name: str = None, template_data: dict = None, completion: bool = True, ) -> ConfigManager: