Files
LangBot/pkg/utils/paths.py
copilot-swe-agent[bot] cab573f3e2 Add package structure and resource path utilities
- 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>
2025-11-07 13:58:18 +00:00

74 lines
2.2 KiB
Python

"""Utility functions for finding package resources"""
import os
import sys
from pathlib import Path
def get_frontend_path() -> str:
"""
Get the path to the frontend build files.
Returns the path to web/out directory, handling both:
- Development mode: running from source directory
- Package mode: installed via pip/uvx
"""
# First, check if we're running from source directory
# (main.py exists in current directory)
if os.path.exists('main.py') and os.path.exists('web/out'):
with open('main.py', 'r', encoding='utf-8') as f:
content = f.read()
if 'LangBot/main.py' in content:
return 'web/out'
# Second, check if we're in a development/source directory
# (current directory has web/out)
if os.path.exists('web/out'):
return 'web/out'
# Third, try to find it relative to this file's location
# This handles the case when installed as a package
pkg_dir = Path(__file__).parent.parent.parent
frontend_path = pkg_dir / 'web' / 'out'
if frontend_path.exists():
return str(frontend_path)
# Fourth, check if it's in the package installation directory
# Look for the web/out directory in the sys.path
for path in sys.path:
candidate = Path(path) / 'web' / 'out'
if candidate.exists():
return str(candidate)
# Return the default path (will be checked by caller)
return 'web/out'
def get_resource_path(resource: str) -> str:
"""
Get the path to a resource file.
Args:
resource: Relative path to resource (e.g., 'templates/config.yaml')
Returns:
Absolute path to the resource
"""
# First, check if resource exists in current directory
if os.path.exists(resource):
return resource
# Second, try to find it relative to package directory
pkg_dir = Path(__file__).parent.parent.parent
resource_path = pkg_dir / resource
if resource_path.exists():
return str(resource_path)
# Third, check in sys.path
for path in sys.path:
candidate = Path(path) / resource
if candidate.exists():
return str(candidate)
# Return the original path
return resource