mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-25 13:56:08 +00:00
chore: update
This commit is contained in:
+16
-19
@@ -1,6 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import json
|
import json
|
||||||
|
import importlib.resources as resources
|
||||||
|
|
||||||
from .. import model as file_model
|
from .. import model as file_model
|
||||||
|
|
||||||
@@ -11,29 +11,27 @@ class JSONConfigFile(file_model.ConfigFile):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
config_file_name: str,
|
config_file_name: str,
|
||||||
template_file_name: str = None,
|
template_resource_name: str = None,
|
||||||
template_data: dict = None,
|
template_data: dict = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.config_file_name = config_file_name
|
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
|
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:
|
def exists(self) -> bool:
|
||||||
return os.path.exists(self.config_file_name)
|
return os.path.exists(self.config_file_name)
|
||||||
|
|
||||||
async def create(self):
|
async def get_template_file_str(self) -> str:
|
||||||
template_path = self._get_template_path()
|
if self.template_resource_name is None:
|
||||||
|
return None
|
||||||
|
|
||||||
if template_path is not None:
|
with resources.path('langbot.templates', self.template_resource_name) as path:
|
||||||
shutil.copyfile(template_path, self.config_file_name)
|
return path.open('r', encoding='utf-8').read()
|
||||||
|
|
||||||
|
async def create(self):
|
||||||
|
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:
|
elif self.template_data is not None:
|
||||||
with open(self.config_file_name, 'w', encoding='utf-8') as f:
|
with open(self.config_file_name, 'w', encoding='utf-8') as f:
|
||||||
json.dump(self.template_data, f, indent=4, ensure_ascii=False)
|
json.dump(self.template_data, f, indent=4, ensure_ascii=False)
|
||||||
@@ -44,11 +42,10 @@ class JSONConfigFile(file_model.ConfigFile):
|
|||||||
if not self.exists():
|
if not self.exists():
|
||||||
await self.create()
|
await self.create()
|
||||||
|
|
||||||
template_path = self._get_template_path()
|
template_file_str = await self.get_template_file_str()
|
||||||
|
|
||||||
if template_path is not None:
|
if template_file_str is not None:
|
||||||
with open(template_path, 'r', encoding='utf-8') as f:
|
self.template_data = json.loads(template_file_str)
|
||||||
self.template_data = json.load(f)
|
|
||||||
|
|
||||||
with open(self.config_file_name, 'r', encoding='utf-8') as f:
|
with open(self.config_file_name, 'r', encoding='utf-8') as f:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -95,43 +95,43 @@ class LoadConfigStage(stage.BootingStage):
|
|||||||
async def run(self, ap: app.Application):
|
async def run(self, ap: app.Application):
|
||||||
"""Load config file"""
|
"""Load config file"""
|
||||||
|
|
||||||
# ======= deprecated =======
|
# # ======= deprecated =======
|
||||||
if os.path.exists('data/config/command.json'):
|
# if os.path.exists('data/config/command.json'):
|
||||||
ap.command_cfg = await config.load_json_config(
|
# ap.command_cfg = await config.load_json_config(
|
||||||
'data/config/command.json',
|
# 'data/config/command.json',
|
||||||
'templates/legacy/command.json',
|
# 'templates/legacy/command.json',
|
||||||
completion=False,
|
# completion=False,
|
||||||
)
|
# )
|
||||||
|
|
||||||
if os.path.exists('data/config/pipeline.json'):
|
# if os.path.exists('data/config/pipeline.json'):
|
||||||
ap.pipeline_cfg = await config.load_json_config(
|
# ap.pipeline_cfg = await config.load_json_config(
|
||||||
'data/config/pipeline.json',
|
# 'data/config/pipeline.json',
|
||||||
'templates/legacy/pipeline.json',
|
# 'templates/legacy/pipeline.json',
|
||||||
completion=False,
|
# completion=False,
|
||||||
)
|
# )
|
||||||
|
|
||||||
if os.path.exists('data/config/platform.json'):
|
# if os.path.exists('data/config/platform.json'):
|
||||||
ap.platform_cfg = await config.load_json_config(
|
# ap.platform_cfg = await config.load_json_config(
|
||||||
'data/config/platform.json',
|
# 'data/config/platform.json',
|
||||||
'templates/legacy/platform.json',
|
# 'templates/legacy/platform.json',
|
||||||
completion=False,
|
# completion=False,
|
||||||
)
|
# )
|
||||||
|
|
||||||
if os.path.exists('data/config/provider.json'):
|
# if os.path.exists('data/config/provider.json'):
|
||||||
ap.provider_cfg = await config.load_json_config(
|
# ap.provider_cfg = await config.load_json_config(
|
||||||
'data/config/provider.json',
|
# 'data/config/provider.json',
|
||||||
'templates/legacy/provider.json',
|
# 'templates/legacy/provider.json',
|
||||||
completion=False,
|
# completion=False,
|
||||||
)
|
# )
|
||||||
|
|
||||||
if os.path.exists('data/config/system.json'):
|
# if os.path.exists('data/config/system.json'):
|
||||||
ap.system_cfg = await config.load_json_config(
|
# ap.system_cfg = await config.load_json_config(
|
||||||
'data/config/system.json',
|
# 'data/config/system.json',
|
||||||
'templates/legacy/system.json',
|
# 'templates/legacy/system.json',
|
||||||
completion=False,
|
# completion=False,
|
||||||
)
|
# )
|
||||||
|
|
||||||
# ======= deprecated =======
|
# # ======= deprecated =======
|
||||||
|
|
||||||
ap.instance_config = await config.load_yaml_config('data/config.yaml', 'config.yaml', completion=False)
|
ap.instance_config = await config.load_yaml_config('data/config.yaml', 'config.yaml', completion=False)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user