diff --git a/langbot/__main__.py b/langbot/__main__.py index 69833d1a..bba1bfc4 100644 --- a/langbot/__main__.py +++ b/langbot/__main__.py @@ -86,10 +86,7 @@ def main(): # Set up the working directory # When installed as a package, we need to handle the working directory differently # We'll create data directory in current working directory if not exists - if not os.path.exists('data'): - print('Creating data directory in current working directory...') - print('在当前工作目录创建 data 目录...') - os.makedirs('data', exist_ok=True) + os.makedirs('data', exist_ok=True) loop = asyncio.new_event_loop() diff --git a/pkg/api/http/service/pipeline.py b/pkg/api/http/service/pipeline.py index c5b960c7..30cbd8ba 100644 --- a/pkg/api/http/service/pipeline.py +++ b/pkg/api/http/service/pipeline.py @@ -82,7 +82,8 @@ class PipelineService: pipeline_data['is_default'] = default template_path = path_utils.get_resource_path('templates/default-pipeline-config.json') - pipeline_data['config'] = json.load(open(template_path, 'r', encoding='utf-8')) + with open(template_path, 'r', encoding='utf-8') as f: + pipeline_data['config'] = json.load(f) await self.ap.persistence_mgr.execute_async( sqlalchemy.insert(persistence_pipeline.LegacyPipeline).values(**pipeline_data) diff --git a/pkg/utils/paths.py b/pkg/utils/paths.py index 379ac63c..20880c57 100644 --- a/pkg/utils/paths.py +++ b/pkg/utils/paths.py @@ -26,7 +26,8 @@ def _check_if_source_install() -> bool: if 'LangBot/main.py' in content: _is_source_install = True return True - except Exception: + except (IOError, OSError, UnicodeDecodeError): + # If we can't read the file, assume not a source install pass _is_source_install = False @@ -51,7 +52,7 @@ def get_frontend_path() -> str: # Third, find it relative to the package installation # Get the directory where this file is located - # paths.py is in pkg/utils/, so go up 3 levels to get to the package root + # paths.py is in pkg/utils/, so parent.parent goes up to pkg/, then parent again goes up to the package root pkg_dir = Path(__file__).parent.parent.parent frontend_path = pkg_dir / 'web' / 'out' if frontend_path.exists(): @@ -81,7 +82,7 @@ def get_resource_path(resource: str) -> str: # Third, find it relative to package directory # Get the directory where this file is located - # paths.py is in pkg/utils/, so go up 3 levels to get to the package root + # paths.py is in pkg/utils/, so parent.parent goes up to pkg/, then parent again goes up to the package root pkg_dir = Path(__file__).parent.parent.parent resource_path = pkg_dir / resource if resource_path.exists():