mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-26 03:44:58 +08:00
- Created langbot/ package with __init__.py and __main__.py entry point - Added paths utility to find frontend and resource files from package installation - Updated config loading to use resource paths - Updated frontend serving to use resource paths - Added MANIFEST.in for package data inclusion - Updated pyproject.toml with build system and entry points Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com>
73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
import os
|
|
import shutil
|
|
import yaml
|
|
|
|
from .. import model as file_model
|
|
|
|
|
|
class YAMLConfigFile(file_model.ConfigFile):
|
|
"""YAML config file"""
|
|
|
|
def __init__(
|
|
self,
|
|
config_file_name: str,
|
|
template_file_name: str = None,
|
|
template_data: dict = None,
|
|
) -> None:
|
|
self.config_file_name = config_file_name
|
|
self.template_file_name = template_file_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 create(self):
|
|
template_path = self._get_template_path()
|
|
|
|
if template_path is not None:
|
|
shutil.copyfile(template_path, self.config_file_name)
|
|
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)
|
|
else:
|
|
raise ValueError('template_file_name or template_data must be provided')
|
|
|
|
async def load(self, completion: bool = True) -> dict:
|
|
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)
|
|
|
|
with open(self.config_file_name, 'r', encoding='utf-8') as f:
|
|
try:
|
|
cfg = yaml.load(f, Loader=yaml.FullLoader)
|
|
except yaml.YAMLError as e:
|
|
raise Exception(f'Syntax error in config file {self.config_file_name}: {e}')
|
|
|
|
if completion:
|
|
for key in self.template_data:
|
|
if key not in cfg:
|
|
cfg[key] = self.template_data[key]
|
|
|
|
return cfg
|
|
|
|
async def save(self, cfg: dict):
|
|
with open(self.config_file_name, 'w', encoding='utf-8') as f:
|
|
yaml.dump(cfg, f, indent=4, allow_unicode=True)
|
|
|
|
def save_sync(self, cfg: dict):
|
|
with open(self.config_file_name, 'w', encoding='utf-8') as f:
|
|
yaml.dump(cfg, f, indent=4, allow_unicode=True)
|