From c7125b6a17c33f636759e80c75b7c5d299eef156 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Wed, 4 Feb 2026 02:18:52 +0000 Subject: [PATCH] fix: resolve CI linting and type checking issues --- app/cmdparse/config.py | 88 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 app/cmdparse/config.py diff --git a/app/cmdparse/config.py b/app/cmdparse/config.py new file mode 100644 index 0000000..10013a0 --- /dev/null +++ b/app/cmdparse/config.py @@ -0,0 +1,88 @@ +from typing import Dict, Any, Optional +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 + }