Initial upload: Shell History Alias Generator with full test suite
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
46
shell_alias_gen/parsers/history_factory.py
Normal file
46
shell_alias_gen/parsers/history_factory.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""History parser factory for creating shell-specific parsers."""
|
||||
|
||||
from typing import Dict, Optional, Type
|
||||
from .base import HistoryParser
|
||||
from .bash import BashHistoryParser
|
||||
from .zsh import ZshHistoryParser
|
||||
from .fish import FishHistoryParser
|
||||
|
||||
|
||||
class HistoryParserFactory:
|
||||
"""Factory for creating shell-specific history parsers."""
|
||||
|
||||
_parsers: Dict[str, Type[HistoryParser]] = {
|
||||
'bash': BashHistoryParser,
|
||||
'zsh': ZshHistoryParser,
|
||||
'fish': FishHistoryParser,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_parser(cls, shell_type: str) -> Optional[HistoryParser]:
|
||||
"""Get a parser instance for the specified shell type."""
|
||||
parser_class = cls._parsers.get(shell_type.lower())
|
||||
if parser_class:
|
||||
return parser_class()
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def register_parser(cls, name: str, parser_class: Type[HistoryParser]) -> None:
|
||||
"""Register a new parser class."""
|
||||
cls._parsers[name.lower()] = parser_class
|
||||
|
||||
@classmethod
|
||||
def get_supported_shells(cls) -> list:
|
||||
"""Return list of supported shell types."""
|
||||
return list(cls._parsers.keys())
|
||||
|
||||
@classmethod
|
||||
def detect_shell_from_file(cls, filepath: str) -> Optional[str]:
|
||||
"""Try to detect shell type from file extension or content."""
|
||||
if filepath.endswith('.json'):
|
||||
return 'fish'
|
||||
elif filepath.endswith('.zsh_history'):
|
||||
return 'zsh'
|
||||
elif filepath.endswith('.bash_history'):
|
||||
return 'bash'
|
||||
return None
|
||||
Reference in New Issue
Block a user