system.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from __future__ import unicode_literals
  2. from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter
  3. from prompt_toolkit.contrib.regular_languages.compiler import compile
  4. from .filesystem import PathCompleter, ExecutableCompleter
  5. __all__ = (
  6. 'SystemCompleter',
  7. )
  8. class SystemCompleter(GrammarCompleter):
  9. """
  10. Completer for system commands.
  11. """
  12. def __init__(self):
  13. # Compile grammar.
  14. g = compile(
  15. r"""
  16. # First we have an executable.
  17. (?P<executable>[^\s]+)
  18. # Ignore literals in between.
  19. (
  20. \s+
  21. ("[^"]*" | '[^']*' | [^'"]+ )
  22. )*
  23. \s+
  24. # Filename as parameters.
  25. (
  26. (?P<filename>[^\s]+) |
  27. "(?P<double_quoted_filename>[^\s]+)" |
  28. '(?P<single_quoted_filename>[^\s]+)'
  29. )
  30. """,
  31. escape_funcs={
  32. 'double_quoted_filename': (lambda string: string.replace('"', '\\"')),
  33. 'single_quoted_filename': (lambda string: string.replace("'", "\\'")),
  34. },
  35. unescape_funcs={
  36. 'double_quoted_filename': (lambda string: string.replace('\\"', '"')), # XXX: not enterily correct.
  37. 'single_quoted_filename': (lambda string: string.replace("\\'", "'")),
  38. })
  39. # Create GrammarCompleter
  40. super(SystemCompleter, self).__init__(
  41. g,
  42. {
  43. 'executable': ExecutableCompleter(),
  44. 'filename': PathCompleter(only_directories=False, expanduser=True),
  45. 'double_quoted_filename': PathCompleter(only_directories=False, expanduser=True),
  46. 'single_quoted_filename': PathCompleter(only_directories=False, expanduser=True),
  47. })