Initial upload: cmdparse CLI tool with comprehensive documentation and CI/CD
This commit is contained in:
91
cmdparse/config.py
Normal file
91
cmdparse/config.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
"""Configuration file loading and parser definitions."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import Dict, Any, Optional, List
|
||||||
|
from pathlib import Path
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
|
def find_config_file(config_path: Optional[str] = None) -> Optional[Path]:
|
||||||
|
"""Find config file in specified path or default locations."""
|
||||||
|
if config_path:
|
||||||
|
path = Path(config_path)
|
||||||
|
if path.exists():
|
||||||
|
return path
|
||||||
|
return None
|
||||||
|
|
||||||
|
config_locations = [
|
||||||
|
Path.home() / '.cmdparse.yaml',
|
||||||
|
Path.home() / '.cmdparse' / 'config.yaml',
|
||||||
|
Path.cwd() / '.cmdparse.yaml',
|
||||||
|
Path.cwd() / 'cmdparse.yaml',
|
||||||
|
]
|
||||||
|
|
||||||
|
for location in config_locations:
|
||||||
|
if location.exists():
|
||||||
|
return location
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def load_config(config_path: Optional[str] = None) -> Dict[str, Any]:
|
||||||
|
"""Load configuration from YAML file."""
|
||||||
|
config_file = find_config_file(config_path)
|
||||||
|
|
||||||
|
if config_file is None:
|
||||||
|
return {'parsers': []}
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(config_file, 'r') as f:
|
||||||
|
config = yaml.safe_load(f)
|
||||||
|
return config if config else {'parsers': []}
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Warning: Could not load config file: {e}")
|
||||||
|
return {'parsers': []}
|
||||||
|
|
||||||
|
|
||||||
|
def get_builtin_config_path() -> Path:
|
||||||
|
"""Get the path to the built-in config file."""
|
||||||
|
return Path(__file__).parent.parent / 'config' / 'default_parsers.yaml'
|
||||||
|
|
||||||
|
|
||||||
|
def load_builtin_config() -> Dict[str, Any]:
|
||||||
|
"""Load built-in parser configurations."""
|
||||||
|
config_path = get_builtin_config_path()
|
||||||
|
|
||||||
|
if config_path.exists():
|
||||||
|
try:
|
||||||
|
with open(config_path, 'r') as f:
|
||||||
|
config = yaml.safe_load(f)
|
||||||
|
return config if config else {'parsers': []}
|
||||||
|
except Exception:
|
||||||
|
return {'parsers': []}
|
||||||
|
|
||||||
|
return {'parsers': []}
|
||||||
|
|
||||||
|
|
||||||
|
def get_custom_parser(config: Dict[str, Any], pattern_name: str) -> Optional[Dict[str, Any]]:
|
||||||
|
"""Get a custom parser definition from config."""
|
||||||
|
parsers = config.get('parsers', [])
|
||||||
|
|
||||||
|
for parser in parsers:
|
||||||
|
if parser.get('name') == pattern_name:
|
||||||
|
return parser
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_parsers() -> Dict[str, Any]:
|
||||||
|
"""Get all custom parsers merged with built-in."""
|
||||||
|
builtin = load_builtin_config()
|
||||||
|
custom = load_config()
|
||||||
|
|
||||||
|
all_parsers = builtin.get('parsers', []).copy()
|
||||||
|
custom_parsers = custom.get('parsers', [])
|
||||||
|
|
||||||
|
custom_names = {p.get('name') for p in custom_parsers}
|
||||||
|
filtered_builtin = [p for p in all_parsers if p.get('name') not in custom_names]
|
||||||
|
|
||||||
|
return {
|
||||||
|
'parsers': filtered_builtin + custom_parsers
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user