ford_qc/utils/path_resolver.py
michael 53e06fff63 Add environment configuration support for multi-deployment (prod/dev)
- Add utils/config.py module using python-dotenv for centralized config
- Externalize Box folder IDs, paths, and timeout settings to .env files
- Create .env.example template with all configuration variables
- Add separate systemd service files for prod and dev environments
- Update ford_qc_box_hotfolder_process.py to use config module
- Update qc_engine.py and path_resolver.py with optional config support
- Maintain backwards compatibility with hardcoded defaults
- Update documentation in CLAUDE.md

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-18 06:10:43 -06:00

63 lines
2.1 KiB
Python

import os
import json
import logging
# Try to load configuration for fallback path
try:
from utils.config import config
_fallback_working_dir = config.FALLBACK_WORKING_DIR
except ImportError:
# Fallback when config module not available (backwards compatibility)
_fallback_working_dir = '/home/box-cli/FORD_SCRIPTS/FORD_ASSET_PACK_QC_NEW/working'
def get_working_dir():
"""
Returns the working directory path.
Tries the local path first, then falls back to configured fallback path if necessary.
"""
script_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
working_dir = os.path.join(script_dir, 'working')
# Create the directory if it doesn't exist
if not os.path.exists(working_dir):
try:
os.makedirs(working_dir, exist_ok=True)
except Exception as e:
logging.warning(f"Failed to create local working directory: {e}")
# Fall back to configured path
if os.path.exists(_fallback_working_dir):
working_dir = _fallback_working_dir
return working_dir
def update_profile_paths(profile_path):
"""
Updates the profile file by replacing __WORKING_DIR__ placeholders with actual paths.
Returns a temporary profile path with the updated content.
"""
working_dir = get_working_dir()
try:
# Read the profile file
with open(profile_path, 'r', encoding='utf-8') as f:
profile_content = f.read()
# Replace the placeholder with the actual working directory
updated_content = profile_content.replace('"__WORKING_DIR__"', f'"{working_dir}"')
# Create a temporary file with the updated content
timestamp = os.path.basename(profile_path).split('.')[0]
temp_dir = os.path.dirname(profile_path)
temp_profile_path = os.path.join(temp_dir, f"{timestamp}_temp.json")
with open(temp_profile_path, 'w', encoding='utf-8') as f:
f.write(updated_content)
return temp_profile_path
except Exception as e:
logging.error(f"Failed to update profile paths: {e}")
return profile_path # Return original path in case of error