示例#1
0
def get_buffer_mapping(buffers=None, include_default=True, init_contents=None):
    bm = {}
    if include_default:
        init_doc = None
        if init_contents is not None:
            content = init_contents.get(DEFAULT_BUFFER, None)
            if content:
                init_doc = document.Document(content, 0)
        bm.update({
            DEFAULT_BUFFER:
            pt_buffer.Buffer(is_multiline=True, initial_document=init_doc)
        })
    if bm is not None:
        bm.update(
            dict([(b, pt_buffer.Buffer(is_multiline=True, read_only=True))
                  for b in buffers]))
    return buffer_mapping.BufferMapping(bm)
示例#2
0
    def _CreatePromptApplication(self):
        """Creates a shell prompt Application."""

        # Make sure that complete_while_typing is disabled when
        # enable_history_search is enabled. (First convert to SimpleFilter, to
        # avoid doing bitwise operations on bool objects.)
        complete_while_typing = shortcuts.to_simple_filter(True)
        enable_history_search = shortcuts.to_simple_filter(False)
        multiline = shortcuts.to_simple_filter(False)

        complete_while_typing &= ~enable_history_search

        return application.Application(
            layout=layout.CreatePromptLayout(
                message=self.prompt,
                lexer=None,
                is_password=False,
                reserve_space_for_menu=self.MENU_RESERVE_SPACE,
                multiline=filters.Condition(lambda cli: multiline()),
                get_prompt_tokens=None,
                get_continuation_tokens=None,
                get_bottom_toolbar_tokens=self._GetBottomToolbarTokens,
                display_completions_in_columns=False,
                extra_input_processors=None,
                wrap_lines=True,
                show_help=filters.Condition(
                    lambda _: self.key_bindings.help_key.toggle)),
            buffer=pt_buffer.Buffer(
                enable_history_search=enable_history_search,
                complete_while_typing=complete_while_typing,
                is_multiline=multiline,
                history=pt_history.InMemoryHistory(),
                validator=None,
                completer=shell_completer.ShellCliCompleter(),
                auto_suggest=None,
                accept_action=pt_buffer.AcceptAction.RETURN_DOCUMENT,
                initial_document=document.Document(''),
            ),
            style=shell_style.GetDocumentStyle(),
            clipboard=None,
            key_bindings_registry=self.key_bindings.MakeRegistry(),
            get_title=None,
            mouse_support=False,
            erase_when_done=False,
            on_abort=application.AbortAction.RAISE_EXCEPTION,
            on_exit=application.AbortAction.RAISE_EXCEPTION)
示例#3
0
def CreateCli(gcloud_py_dir):
  """Creates the CLI application.

  Args:
    gcloud_py_dir: str, path to completion lookup table

  Returns:
    cli, a cli instance
  """
  completer = ShellCliCompleter(gcloud_py_dir)
  in_memory_history = history.InMemoryHistory()
  auto_suggest_from_history = auto_suggest.AutoSuggestFromHistory()
  key_manager = _KeyBindings()

  layout = shortcuts.create_prompt_layout(
      lexer=shell.BashLexer,
      get_bottom_toolbar_tokens=GetBottomToolbarTokens,
      message=u'Cloud SDK {0}> '.format(config.CLOUD_SDK_VERSION))

  cli_buffer = ptkbuffer.Buffer(
      history=in_memory_history,
      auto_suggest=auto_suggest_from_history,
      complete_while_typing=True,
      completer=completer,
      accept_action=interface.AcceptAction.RETURN_DOCUMENT)

  application = Application(
      style=GetDocumentStyle(),
      buffer=cli_buffer,
      layout=layout,
      key_bindings_registry=key_manager.registry,
      mouse_support=True)

  cli = interface.CommandLineInterface(
      application=application,
      eventloop=shortcuts.create_eventloop())

  return cli
示例#4
0
    def __init__(self, cosh=None, args=None, config=None):
        self.args = args
        self.coshell = cosh
        self.config = config
        self.key_bindings = bindings.KeyBindings(
            edit_mode=self.coshell.edit_mode == 'emacs')

        # Load the default CLI trees. On startup we ignore out of date trees. The
        # alternative is to regenerate them before the first prompt. This could be
        # a noticeable delay for users that accrue a lot of trees. Although ignored
        # at startup, the regen will happen on demand as the individual commands
        # are typed.
        self.root = generate_cli_trees.LoadAll(ignore_out_of_date=True,
                                               warn_on_exceptions=True)

        # Add the exit command completer node to the CLI tree.
        self.root[parser.LOOKUP_COMMANDS]['exit'] = cli_tree.Node(
            command='exit',
            description='Exit the interactive shell.',
            positionals=[
                {
                    'default': '0',
                    'description': 'The exit status.',
                    'name': 'status',
                    'nargs': '?',
                    'required': False,
                    'value': 'STATUS',
                },
            ],
        )

        # Create the parser and completer.
        interactive_parser = parser.Parser(self.root,
                                           context=config.context,
                                           hidden=config.hidden)
        interactive_completer = completer.InteractiveCliCompleter(
            interactive_parser=interactive_parser,
            args=args,
            cosh=self.coshell,
            hidden=config.hidden,
            manpage_generator=config.manpage_generator)

        # Make sure that complete_while_typing is disabled when
        # enable_history_search is enabled. (First convert to SimpleFilter, to
        # avoid doing bitwise operations on bool objects.)
        complete_while_typing = shortcuts.to_simple_filter(True)
        enable_history_search = shortcuts.to_simple_filter(False)
        complete_while_typing &= ~enable_history_search
        history_file = os.path.join(core_config.Paths().global_config_dir,
                                    'shell_history')
        multiline = shortcuts.to_simple_filter(False)

        # Create the default buffer.

        self.default_buffer = pt_buffer.Buffer(
            enable_history_search=enable_history_search,
            complete_while_typing=complete_while_typing,
            is_multiline=multiline,
            history=pt_history.FileHistory(history_file),
            validator=None,
            completer=interactive_completer,
            auto_suggest=(auto_suggest.AutoSuggestFromHistory()
                          if config.suggest else None),
            accept_action=pt_buffer.AcceptAction.RETURN_DOCUMENT,
        )

        # Create the CLI.
        self.cli = CLI(
            config=config,
            cosh=cosh,
            root=self.root,
            interactive_parser=interactive_parser,
            application=self._CreatePromptApplication(config=config,
                                                      multiline=multiline),
            eventloop=shortcuts.create_eventloop(),
            output=shortcuts.create_output(),
        )
        self.key_bindings.Initialize(self.cli)
示例#5
0
  def __init__(self, coshell=None, args=None, config=None, debug=None):
    self.args = args
    self.coshell = coshell
    self.config = config
    self.debug = debug
    self.key_bindings = bindings.KeyBindings()
    self.key_bindings_registry = self.key_bindings.MakeRegistry()

    # Load the default CLI trees. On startup we ignore out of date trees. The
    # alternative is to regenerate them before the first prompt. This could be
    # a noticeable delay for users that accrue a lot of trees. Although ignored
    # at startup, the regen will happen on demand as the individual commands
    # are typed.
    self.root = generate_cli_trees.LoadAll(
        ignore_out_of_date=True, warn_on_exceptions=True)

    # Add the interactive default CLI tree nodes.

    _AddCliTreeKeywordsAndBuiltins(self.root)

    # Make sure that complete_while_typing is disabled when
    # enable_history_search is enabled. (First convert to SimpleFilter, to
    # avoid doing bitwise operations on bool objects.)
    complete_while_typing = shortcuts.to_simple_filter(True)
    enable_history_search = shortcuts.to_simple_filter(False)
    complete_while_typing &= ~enable_history_search
    history_file = os.path.join(core_config.Paths().global_config_dir,
                                'shell_history')
    multiline = shortcuts.to_simple_filter(False)

    # Create the parser.
    interactive_parser = parser.Parser(
        self.root,
        context=config.context,
        hidden=config.hidden)

    # Create the completer.
    interactive_completer = completer.InteractiveCliCompleter(
        coshell=coshell,
        debug=debug,
        interactive_parser=interactive_parser,
        args=args,
        hidden=config.hidden,
        manpage_generator=config.manpage_generator)

    # Create the default buffer.
    self.default_buffer = pt_buffer.Buffer(
        enable_history_search=enable_history_search,
        complete_while_typing=complete_while_typing,
        is_multiline=multiline,
        history=pt_history.FileHistory(history_file),
        validator=None,
        completer=interactive_completer,
        auto_suggest=(auto_suggest.AutoSuggestFromHistory()
                      if config.suggest else None),
        accept_action=pt_buffer.AcceptAction.RETURN_DOCUMENT,
    )

    # Create the CLI.
    self.cli = CLI(
        config=config,
        coshell=coshell,
        debug=debug,
        root=self.root,
        interactive_parser=interactive_parser,
        interactive_completer=interactive_completer,
        application=self._CreatePromptApplication(config=config,
                                                  multiline=multiline),
        eventloop=shortcuts.create_eventloop(),
        output=shortcuts.create_output(),
    )

    # The interactive completer is friends with the CLI.
    interactive_completer.cli = self.cli

    # Initialize the bindings.
    self.key_bindings.Initialize(self.cli)
    bindings_vi.LoadViBindings(self.key_bindings_registry)
示例#6
0
def CreatePromptApplication(
        message='',
        multiline=False,
        wrap_lines=True,
        is_password=False,
        complete_while_typing=True,
        enable_history_search=False,
        lexer=None,
        enable_system_bindings=False,
        enable_open_in_editor=False,
        validator=None,
        completer=None,
        reserve_space_for_menu=5,
        auto_suggest=None,
        style=None,
        history=None,
        clipboard=None,
        get_prompt_tokens=None,
        get_continuation_tokens=None,
        get_bottom_toolbar_tokens=None,
        display_completions_in_columns=False,
        get_title=None,
        mouse_support=False,
        extra_input_processors=None,
        key_bindings_registry=None,
        on_abort=application.AbortAction.RAISE_EXCEPTION,
        on_exit=application.AbortAction.RAISE_EXCEPTION,
        accept_action=pt_buffer.AcceptAction.RETURN_DOCUMENT,
        erase_when_done=False,
        default='',
        get_help_tokens=None):
    """Create the shell prompt Application."""
    if key_bindings_registry is None:
        key_bindings_registry = manager.KeyBindingManager.for_prompt(
            enable_system_bindings=enable_system_bindings,
            enable_open_in_editor=enable_open_in_editor).registry

    # Make sure that complete_while_typing is disabled when enable_history_search
    # is enabled. (First convert to SimpleFilter, to avoid doing bitwise
    # operations on bool objects.)
    complete_while_typing = shortcuts.to_simple_filter(complete_while_typing)
    enable_history_search = shortcuts.to_simple_filter(enable_history_search)
    multiline = shortcuts.to_simple_filter(multiline)

    complete_while_typing &= ~enable_history_search

    # Create application
    return application.Application(
        layout=layout.CreatePromptLayout(
            message=message,
            lexer=lexer,
            is_password=is_password,
            reserve_space_for_menu=(reserve_space_for_menu
                                    if completer is not None else 0),
            multiline=filters.Condition(lambda cli: multiline()),
            get_prompt_tokens=get_prompt_tokens,
            get_continuation_tokens=get_continuation_tokens,
            get_bottom_toolbar_tokens=get_bottom_toolbar_tokens,
            display_completions_in_columns=display_completions_in_columns,
            extra_input_processors=extra_input_processors,
            wrap_lines=wrap_lines,
            get_help_tokens=get_help_tokens,
            show_help=filters.Condition(lambda _: SHOW_HELP_WINDOW)),
        buffer=pt_buffer.Buffer(
            enable_history_search=enable_history_search,
            complete_while_typing=complete_while_typing,
            is_multiline=multiline,
            history=(history or pt_history.InMemoryHistory()),
            validator=validator,
            completer=completer,
            auto_suggest=auto_suggest,
            accept_action=accept_action,
            initial_document=document.Document(default),
        ),
        style=style,
        clipboard=clipboard,
        key_bindings_registry=key_bindings_registry,
        get_title=get_title,
        mouse_support=mouse_support,
        erase_when_done=erase_when_done,
        on_abort=on_abort,
        on_exit=on_exit)
示例#7
0
  def __init__(self, cli=None, cosh=None, args=None, config=None):
    self.args = args
    self.coshell = cosh
    self.config = config
    self.key_bindings = bindings.KeyBindings(
        edit_mode=self.coshell.edit_mode == 'emacs')

    # Load the default CLI trees.
    self.root = cli_tree.LoadAll(cli=cli)

    # Add the exit command completer node to the CLI tree.
    self.root[parser.LOOKUP_COMMANDS]['exit'] = cli_tree.Node(
        command='exit',
        description='Exit the interactive shell.',
        positionals=[
            {
                'default': '0',
                'description': 'The exit status.',
                'name': 'status',
                'nargs': '?',
                'required': False,
                'value': 'STATUS',
            },
        ],
    )

    # Create the parser and completer.
    shell_parser = parser.Parser(
        self.root,
        context=config.context,
        hidden=config.hidden)
    shell_completer = completer.ShellCliCompleter(shell_parser=shell_parser,
                                                  args=args,
                                                  cosh=self.coshell,
                                                  hidden=config.hidden)

    # Make sure that complete_while_typing is disabled when
    # enable_history_search is enabled. (First convert to SimpleFilter, to
    # avoid doing bitwise operations on bool objects.)
    complete_while_typing = shortcuts.to_simple_filter(True)
    enable_history_search = shortcuts.to_simple_filter(False)
    complete_while_typing &= ~enable_history_search
    history_file = os.path.join(core_config.Paths().global_config_dir,
                                'shell_history')
    multiline = shortcuts.to_simple_filter(False)

    # Create the default buffer.

    self.default_buffer = pt_buffer.Buffer(
        enable_history_search=enable_history_search,
        complete_while_typing=complete_while_typing,
        is_multiline=multiline,
        history=pt_history.FileHistory(history_file),
        validator=None,
        completer=shell_completer,
        auto_suggest=(auto_suggest.AutoSuggestFromHistory()
                      if config.suggest else None),
        accept_action=pt_buffer.AcceptAction.RETURN_DOCUMENT,
    )

    # Create the CLI.
    self.cli = CLI(
        config=config,
        cosh=cosh,
        root=self.root,
        shell_parser=shell_parser,
        application=self._CreatePromptApplication(config=config,
                                                  multiline=multiline),
        eventloop=shortcuts.create_eventloop(),
        output=shortcuts.create_output(),
    )
    self.key_bindings.Initialize(self.cli)