Example #1
0
class HistoryManager(object):
    def __init__(self, *args, **kwargs):
        super(HistoryManager, self).__init__(*args, **kwargs)

        self._history = None
        self.last_cmd = None
        self.db = None

        if HAVE_SQLITE:
            dbpath = os.path.join(
                save_cache_path('graphcli'),
                'history.db'
            )
            self.db = sqlite3.connect(dbpath)
            self.cursor = self.db.cursor()

            self.cursor.execute('''
                CREATE TABLE IF NOT EXISTS graphcli_history (
                    ts INT,
                    cmd TEXT
                )
            ''')

    @property
    def history(self):
        if self._history is None:
            self._history = InMemoryHistory()

            for cmd in self.read_history():
                self._history.append('{0};'.format(cmd))
                self.last_cmd = cmd

        return self._history

    def read_history(self):
        if self.db is not None:
            commands = self.cursor.execute('''
                SELECT cmd FROM graphcli_history
                ORDER BY ts DESC
                LIMIT ?
            ''', (self.history_size,))

            for row in reversed(list(commands)):
                yield row[0]

    def add_to_history(self, cmd):
        if cmd != self.last_cmd:
            if self.db is not None:
                ts = int(time())

                self.cursor.execute('''
                    INSERT INTO graphcli_history VALUES (:ts, :cmd)
                ''', {'ts': ts, 'cmd': cmd})

            self.last_cmd = cmd

    def close(self):
        if self.db is not None:
            self.db.commit()
            self.db.close()
    def init_prompt_toolkit_cli(self):
        if self.simple_prompt:
            # Fall back to plain non-interactive output for tests.
            # This is very limited, and only accepts a single line.
            def prompt():
                isp = self.input_splitter
                prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens())
                prompt_continuation = "".join(x[1] for x in self.prompts.continuation_prompt_tokens())
                while isp.push_accepts_more():
                    line = cast_unicode_py2(input(prompt_text))
                    isp.push(line)
                    prompt_text = prompt_continuation
                return isp.source_reset()
            self.prompt_for_code = prompt
            return

        # Set up keyboard shortcuts
        kbmanager = KeyBindingManager.for_prompt(
            enable_open_in_editor=self.extra_open_editor_shortcuts,
        )
        register_ipython_shortcuts(kbmanager.registry, self)

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)
                last_cell = cell

        self._style = self._make_style_from_name_or_cls(self.highlighting_style)
        self.style = DynamicStyle(lambda: self._style)

        editing_mode = getattr(EditingMode, self.editing_mode.upper())

        def patch_stdout(**kwargs):
            return self.pt_cli.patch_stdout_context(**kwargs)

        self._pt_app = create_prompt_application(
                            editing_mode=editing_mode,
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=IPythonPTCompleter(shell=self,
                                                    patch_stdout=patch_stdout),
                            enable_history_search=True,
                            style=self.style,
                            mouse_support=self.mouse_support,
                            **self._layout_options()
        )
        self._eventloop = create_eventloop(self.inputhook)
        self.pt_cli = CommandLineInterface(
            self._pt_app, eventloop=self._eventloop,
            output=create_output(true_color=self.true_color))
Example #3
0
    def __init__(self, completer, model_completer, docs,
                 input=None, output=None):
        self.completer = completer
        self.model_completer = model_completer
        self.history = InMemoryHistory()
        self.file_history = FileHistory(build_config_file_path('history'))
        self._cli = None
        self._docs = docs
        self.current_docs = u''
        self.refresh_cli = False
        self.key_manager = None
        self._dot_cmd = DotCommandHandler()
        self._env = os.environ.copy()
        self._profile = None
        self._input = input
        self._output = output

        # These attrs come from the config file.
        self.config_obj = None
        self.config_section = None
        self.enable_vi_bindings = None
        self.show_completion_columns = None
        self.show_help = None
        self.theme = None

        self.load_config()
Example #4
0
    def init_prompt_toolkit_cli(self):
        if self.simple_prompt:
            # Fall back to plain non-interactive output for tests.
            # This is very limited, and only accepts a single line.
            def prompt():
                isp = self.input_splitter
                prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens())
                prompt_continuation = "".join(x[1] for x in self.prompts.continuation_prompt_tokens())
                while isp.push_accepts_more():
                    line = input(prompt_text)
                    isp.push(line)
                    prompt_text = prompt_continuation
                return isp.source_reset()
            self.prompt_for_code = prompt
            return

        # Set up keyboard shortcuts
        key_bindings = create_ipython_shortcuts(self)

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append_string(cell)
                last_cell = cell

        self._style = self._make_style_from_name_or_cls(self.highlighting_style)
        self.style = DynamicStyle(lambda: self._style)

        editing_mode = getattr(EditingMode, self.editing_mode.upper())

        self.pt_app = PromptSession(
                            editing_mode=editing_mode,
                            key_bindings=key_bindings,
                            history=history,
                            completer=IPythonPTCompleter(shell=self),
                            enable_history_search = self.enable_history_search,
                            style=self.style,
                            include_default_pygments_style=False,
                            mouse_support=self.mouse_support,
                            enable_open_in_editor=self.extra_open_editor_shortcuts,
                            color_depth=(ColorDepth.TRUE_COLOR if self.true_color else None),
                            **self._extra_prompt_options())
def main():
    # Create some history first. (Easy for testing.)
    history = InMemoryHistory()
    history.append_string('import os')
    history.append_string('print("hello")')
    history.append_string('print("world")')
    history.append_string('import path')

    # Print help.
    print('This CLI has up-arrow partial string matching enabled.')
    print('Type for instance "pri" followed by up-arrow and you')
    print('get the last items starting with "pri".')
    print('Press Control-C to retry. Control-D to exit.')
    print()

    session = PromptSession(history=history, enable_history_search=True)

    while True:
        try:
            text = session.prompt('Say something: ')
        except KeyboardInterrupt:
            pass  # Ctrl-C pressed. Try again.
        else:
            break

    print('You said: %s' % text)
def main():
    # Create some history first. (Easy for testing.)
    history = InMemoryHistory()
    history.append_string('import os')
    history.append_string('print("hello")')
    history.append_string('print("world")')
    history.append_string('import path')

    # Print help.
    print('This CLI has fish-style auto-suggestion enable.')
    print('Type for instance "pri", then you\'ll see a suggestion.')
    print('Press the right arrow to insert the suggestion.')
    print('Press Control-C to retry. Control-D to exit.')
    print()

    session = PromptSession(
        history=history,
        auto_suggest=AutoSuggestFromHistory(),
        enable_history_search=True)

    while True:
        try:
            text = session.prompt('Say something: ')
        except KeyboardInterrupt:
            pass  # Ctrl-C pressed. Try again.
        else:
            break

    print('You said: %s' % text)
Example #7
0
    def history(self):
        if self._history is None:
            self._history = InMemoryHistory()

            for cmd in self.read_history():
                self._history.append('{0};'.format(cmd))
                self.last_cmd = cmd

        return self._history
Example #8
0
    def init_prompt_toolkit_cli(self):
        if self.simple_prompt:
            # Fall back to plain non-interactive output for tests.
            # This is very limited, and only accepts a single line.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            return

        # Set up keyboard shortcuts
        kbmanager = KeyBindingManager.for_prompt()
        register_ipython_shortcuts(kbmanager.registry, self)

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)
                last_cell = cell

        self._style = self._make_style_from_name_or_cls(self.highlighting_style)
        style = DynamicStyle(lambda: self._style)

        editing_mode = getattr(EditingMode, self.editing_mode.upper())

        self._pt_app = create_prompt_application(
                            editing_mode=editing_mode,
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=IPythonPTCompleter(shell=self),
                            enable_history_search=True,
                            style=style,
                            mouse_support=self.mouse_support,
                            **self._layout_options()
        )
        self._eventloop = create_eventloop(self.inputhook)
        self.pt_cli = CommandLineInterface(
            self._pt_app, eventloop=self._eventloop,
            output=create_output(true_color=self.true_color))
Example #9
0
 def __init__(self, completer, model_completer, docs):
     self.completer = completer
     self.model_completer = model_completer
     self.history = InMemoryHistory()
     self.file_history = FileHistory(build_config_file_path('history'))
     self._cli = None
     self._docs = docs
     self.current_docs = u''
     self.refresh_cli = False
     self._dot_cmd = DotCommandHandler()
     self.load_config()
def main():
    # Create some history first. (Easy for testing.)
    history = InMemoryHistory()
    history.append("import os")
    history.append('print("hello")')
    history.append('print("world")')
    history.append("import path")

    # Print help.
    print("This CLI has up-arrow partial string matching enabled.")
    print('Type for instance "pri" followed by up-arrow and you')
    print('get the last items starting with "pri".')
    print("Press Control-C to retry. Control-D to exit.")
    print()

    text = prompt("Say something: ", history=history, enable_history_search=Always(), on_abort=AbortAction.RETRY)
    print("You said: %s" % text)
def main():
    # Create some history first. (Easy for testing.)
    history = InMemoryHistory()
    history.append('import os')
    history.append('print("hello")')
    history.append('print("world")')
    history.append('import path')

    # Print help.
    print('This CLI has fish-style auto-suggestion enable.')
    print('Type for instance "pri", then you\'ll see a suggestion.')
    print('Press the right arrow to insert the suggestion.')
    print('Press Control-C to retry. Control-D to exit.')
    print()

    text = prompt('Say something: ', history=history,
                  auto_suggest=AutoSuggestFromHistory(),
                  enable_history_search=True,
                  on_abort=AbortAction.RETRY)
    print('You said: {0!s}'.format(text))
Example #12
0
class AWSShell(object):
    """Encapsulate the ui, completer, command history, docs, and config.

    Runs the input event loop and delegates the command execution to either
    the `awscli` or the underlying shell.

    :type refresh_cli: bool
    :param refresh_cli: Flag to refresh the cli.

    :type config_obj: :class:`configobj.ConfigObj`
    :param config_obj: Contains the config information for reading and writing.

    :type config_section: :class:`configobj.Section`
    :param config_section: Convenience attribute to access the main section
        of the config.

    :type model_completer: :class:`AWSCLIModelCompleter`
    :param model_completer: Matches input with completions.  `AWSShell` sets
        and gets the attribute `AWSCLIModelCompleter.match_fuzzy`.

    :type enable_vi_bindings: bool
    :param enable_vi_bindings: If True, enables Vi key bindings. Else, Emacs
        key bindings are enabled.

    :type show_completion_columns: bool
    param show_completion_columns: If True, completions are shown in multiple
        columns.  Else, completions are shown in a single scrollable column.

    :type show_help: bool
    :param show_help: If True, shows the help pane.  Else, hides the help pane.

    :type theme: str
    :param theme: The pygments theme.

    """
    def __init__(self,
                 completer,
                 model_completer,
                 docs,
                 input=None,
                 output=None):
        self.completer = completer
        self.model_completer = model_completer
        self.history = InMemoryHistory()
        self.file_history = FileHistory(build_config_file_path('history'))
        self._cli = None
        self._docs = docs
        self.current_docs = u''
        self.refresh_cli = False
        self.key_manager = None
        self._dot_cmd = DotCommandHandler()
        self._env = os.environ.copy()
        self._profile = None
        self._input = input
        self._output = output
        self.prompt_tokens = u'aws > '

        # These attrs come from the config file.
        self.config_obj = None
        self.config_section = None
        self.enable_vi_bindings = None
        self.show_completion_columns = None
        self.show_help = None
        self.theme = None

        self.load_config()

    def load_config(self):
        """Load the config from the config file or template."""
        config = Config()
        self.config_obj = config.load('awsshellrc')
        self.config_section = self.config_obj['aws-shell']
        self.model_completer.match_fuzzy = self.config_section.as_bool(
            'match_fuzzy')
        self.enable_vi_bindings = self.config_section.as_bool(
            'enable_vi_bindings')
        self.show_completion_columns = self.config_section.as_bool(
            'show_completion_columns')
        self.show_help = self.config_section.as_bool('show_help')
        self.theme = self.config_section['theme']

    def save_config(self):
        """Save the config to the config file."""
        self.config_section['match_fuzzy'] = self.model_completer.match_fuzzy
        self.config_section['enable_vi_bindings'] = self.enable_vi_bindings
        self.config_section['show_completion_columns'] = \
            self.show_completion_columns
        self.config_section['show_help'] = self.show_help
        self.config_section['theme'] = self.theme
        self.config_obj.write()

    def add_context(self, text):
        self.model_completer.context.append(text.split()[0].strip('@'))
        self.model_completer.reset()
        self.prompt_tokens = u'aws ' + ' '.join(
            self.model_completer.context) + u' > '
        self.refresh_cli = True
        self.cli.request_redraw()

    def remove_context(self):
        self.model_completer.context.pop()
        self.prompt_tokens = u'aws ' + ' '.join(
            self.model_completer.context) + u' > '
        self.refresh_cli = True
        self.cli.request_redraw()

    @property
    def cli(self):
        if self._cli is None or self.refresh_cli:
            self._cli = self.create_cli_interface(self.show_completion_columns)
            self.refresh_cli = False
        return self._cli

    def run(self):
        while True:
            try:
                document = self.cli.run(reset_current_buffer=True)
                if self.model_completer.context and isinstance(
                        self.model_completer.context, list):
                    text = " ".join(self.model_completer.context) + \
                        " " + document.text
                    original_text = document.text
                else:
                    text = document.text
                    original_text = text
            except InputInterrupt:
                pass
            except (KeyboardInterrupt, EOFError):
                self.save_config()
                break
            else:
                if original_text.startswith('.'):
                    # These are special commands (dot commands) that are
                    # interpreted by the aws-shell directly and typically used
                    # to modify some type of behavior in the aws-shell.
                    result = self._dot_cmd.handle_cmd(text, application=self)
                    if result is EXIT_REQUESTED:
                        break
                else:
                    if original_text.startswith('!'):
                        # Then run the rest as a normal shell command.
                        full_cmd = text[1:]
                    elif original_text.startswith('@'):
                        # Add word as context to completions
                        self.add_context(text)
                        continue
                    elif original_text == 'exit' and\
                            self.model_completer.context:
                        # Remove most recently added context
                        self.remove_context()
                        continue
                    else:
                        full_cmd = 'aws ' + text
                        self.history.append(full_cmd)
                    self.current_docs = u''
                    self.cli.buffers['clidocs'].reset(
                        initial_document=Document(self.current_docs,
                                                  cursor_position=0))
                    self.cli.request_redraw()
                    p = subprocess.Popen(full_cmd, shell=True, env=self._env)
                    p.communicate()

    def stop_input_and_refresh_cli(self):
        """Stop input by raising an `InputInterrupt`, forces a cli refresh.

        The cli refresh is necessary because changing options such as key
        bindings, single vs multi column menu completions, and the help pane
        all require a rebuild.

        :raises: :class:`InputInterrupt <exceptions.InputInterrupt>`.

        """
        self.refresh_cli = True
        self.cli.request_redraw()
        raise InputInterrupt

    def create_layout(self, display_completions_in_columns, toolbar):
        from awsshell.lexer import ShellLexer
        lexer = ShellLexer
        if self.config_section['theme'] == 'none':
            lexer = None
        return create_default_layout(
            self,
            self.prompt_tokens,
            lexer=lexer,
            reserve_space_for_menu=True,
            display_completions_in_columns=display_completions_in_columns,
            get_bottom_toolbar_tokens=toolbar.handler)

    def create_buffer(self, completer, history):
        return Buffer(history=history,
                      auto_suggest=AutoSuggestFromHistory(),
                      enable_history_search=True,
                      completer=completer,
                      complete_while_typing=Always(),
                      accept_action=AcceptAction.RETURN_DOCUMENT)

    def create_key_manager(self):
        """Create the :class:`KeyManager`.

        The inputs to KeyManager are expected to be callable, so we can't
        use the standard @property and @attrib.setter for these attributes.
        Lambdas cannot contain assignments so we're forced to define setters.

        :rtype: :class:`KeyManager`
        :return: A KeyManager with callables to set the toolbar options.  Also
            includes the method stop_input_and_refresh_cli to ensure certain
            options take effect within the current session.

        """
        def set_match_fuzzy(match_fuzzy):
            """Setter for fuzzy matching mode.

            :type match_fuzzy: bool
            :param match_fuzzy: The match fuzzy flag.

            """
            self.model_completer.match_fuzzy = match_fuzzy

        def set_enable_vi_bindings(enable_vi_bindings):
            """Setter for vi mode keybindings.

            If vi mode is off, emacs mode is enabled by default by
            `prompt_toolkit`.

            :type enable_vi_bindings: bool
            :param enable_vi_bindings: The enable Vi bindings flag.

            """
            self.enable_vi_bindings = enable_vi_bindings

        def set_show_completion_columns(show_completion_columns):
            """Setter for showing the completions in columns flag.

            :type show_completion_columns: bool
            :param show_completion_columns: The show completions in
                multiple columns flag.

            """
            self.show_completion_columns = show_completion_columns

        def set_show_help(show_help):
            """Setter for showing the help container flag.

            :type show_help: bool
            :param show_help: The show help flag.

            """
            self.show_help = show_help

        return KeyManager(lambda: self.model_completer.match_fuzzy,
                          set_match_fuzzy, lambda: self.enable_vi_bindings,
                          set_enable_vi_bindings,
                          lambda: self.show_completion_columns,
                          set_show_completion_columns, lambda: self.show_help,
                          set_show_help, self.stop_input_and_refresh_cli)

    def create_application(self, completer, history,
                           display_completions_in_columns):
        self.key_manager = self.create_key_manager()
        toolbar = Toolbar(lambda: self.model_completer.match_fuzzy,
                          lambda: self.enable_vi_bindings,
                          lambda: self.show_completion_columns,
                          lambda: self.show_help)
        style_factory = StyleFactory(self.theme)
        buffers = {'clidocs': Buffer(read_only=True)}

        return Application(
            layout=self.create_layout(display_completions_in_columns, toolbar),
            mouse_support=False,
            style=style_factory.style,
            buffers=buffers,
            buffer=self.create_buffer(completer, history),
            on_abort=AbortAction.RETRY,
            on_exit=AbortAction.RAISE_EXCEPTION,
            on_input_timeout=self.on_input_timeout,
            key_bindings_registry=self.key_manager.manager.registry,
        )

    def on_input_timeout(self, cli):
        if not self.show_help:
            return
        document = cli.current_buffer.document
        text = document.text
        LOG.debug("document.text = %s", text)
        LOG.debug("current_command = %s", self.completer.current_command)
        if text.strip():
            command = self.completer.current_command
            key_name = '.'.join(command.split()).encode('utf-8')
            last_option = self.completer.last_option
            if last_option:
                self.current_docs = self._docs.extract_param(
                    key_name, last_option)
            else:
                self.current_docs = self._docs.extract_description(key_name)
        else:
            self.current_docs = u''
        cli.buffers['clidocs'].reset(
            initial_document=Document(self.current_docs, cursor_position=0))
        cli.request_redraw()

    def create_cli_interface(self, display_completions_in_columns):
        # A CommandLineInterface from prompt_toolkit
        # accepts two things: an application and an
        # event loop.
        loop = create_eventloop()
        app = self.create_application(self.completer, self.file_history,
                                      display_completions_in_columns)
        cli = CommandLineInterface(application=app,
                                   eventloop=loop,
                                   input=self._input,
                                   output=self._output)
        return cli

    @property
    def profile(self):
        return self._profile

    @profile.setter
    def profile(self, new_profile_name):
        # There's only two things that need to know about new profile
        # changes.
        #
        # First, the actual command runner.  If we want
        # to use a different profile, it should ensure that the CLI
        # commands that get run use the new profile (via the
        # AWS_DEFAULT_PROFILE env var).
        #
        # Second, we also need to let the server side autocompleter know.
        #
        # Given this is easy to manage by hand, I don't think
        # it's worth adding an event system or observers just yet.
        # If this gets hard to manage, the complexity of those systems
        # would be worth it.
        self._env['AWS_DEFAULT_PROFILE'] = new_profile_name
        self.completer.change_profile(new_profile_name)
        self._profile = new_profile_name
Example #13
0
class AWSShell(object):
    """Encapsulate the ui, completer, command history, docs, and config.

    Runs the input event loop and delegates the command execution to either
    the `awscli` or the underlying shell.

    :type refresh_cli: bool
    :param refresh_cli: Flag to refresh the cli.

    :type config_obj: :class:`configobj.ConfigObj`
    :param config_obj: Contains the config information for reading and writing.

    :type config_section: :class:`configobj.Section`
    :param config_section: Convenience attribute to access the main section
        of the config.

    :type model_completer: :class:`AWSCLIModelCompleter`
    :param model_completer: Matches input with completions.  `AWSShell` sets
        and gets the attribute `AWSCLIModelCompleter.match_fuzzy`.

    :type enable_vi_bindings: bool
    :param enable_vi_bindings: If True, enables Vi key bindings. Else, Emacs
        key bindings are enabled.

    :type show_completion_columns: bool
    param show_completion_columns: If True, completions are shown in multiple
        columns.  Else, completions are shown in a single scrollable column.

    :type show_help: bool
    :param show_help: If True, shows the help pane.  Else, hides the help pane.

    :type theme: str
    :param theme: The pygments theme.

    """

    def __init__(self, completer, model_completer, docs,
                 input=None, output=None):
        self.completer = completer
        self.model_completer = model_completer
        self.history = InMemoryHistory()
        self.file_history = FileHistory(build_config_file_path('history'))
        self._cli = None
        self._docs = docs
        self.current_docs = u''
        self.refresh_cli = False
        self.key_manager = None
        self._dot_cmd = DotCommandHandler()
        self._env = os.environ.copy()
        self._profile = None
        self._input = input
        self._output = output

        # These attrs come from the config file.
        self.config_obj = None
        self.config_section = None
        self.enable_vi_bindings = None
        self.show_completion_columns = None
        self.show_help = None
        self.theme = None

        self.load_config()

    def load_config(self):
        """Load the config from the config file or template."""
        config = Config()
        self.config_obj = config.load('awsshellrc')
        self.config_section = self.config_obj['aws-shell']
        self.model_completer.match_fuzzy = self.config_section.as_bool(
            'match_fuzzy')
        self.enable_vi_bindings = self.config_section.as_bool(
            'enable_vi_bindings')
        self.show_completion_columns = self.config_section.as_bool(
            'show_completion_columns')
        self.show_help = self.config_section.as_bool('show_help')
        self.theme = self.config_section['theme']

    def save_config(self):
        """Save the config to the config file."""
        self.config_section['match_fuzzy'] = self.model_completer.match_fuzzy
        self.config_section['enable_vi_bindings'] = self.enable_vi_bindings
        self.config_section['show_completion_columns'] = \
            self.show_completion_columns
        self.config_section['show_help'] = self.show_help
        self.config_section['theme'] = self.theme
        self.config_obj.write()

    @property
    def cli(self):
        if self._cli is None or self.refresh_cli:
            self._cli = self.create_cli_interface(self.show_completion_columns)
            self.refresh_cli = False
        return self._cli

    def run(self):
        while True:
            try:
                document = self.cli.run(reset_current_buffer=True)
                text = document.text
            except InputInterrupt:
                pass
            except (KeyboardInterrupt, EOFError):
                self.save_config()
                break
            else:
                if text.startswith('.'):
                    # These are special commands (dot commands) that are
                    # interpreted by the aws-shell directly and typically used
                    # to modify some type of behavior in the aws-shell.
                    result = self._dot_cmd.handle_cmd(text, application=self)
                    if result is EXIT_REQUESTED:
                        break
                else:
                    if text.startswith('!'):
                        # Then run the rest as a normally shell command.
                        full_cmd = text[1:]
                    else:
                        full_cmd = 'aws ' + text
                        self.history.append(full_cmd)
                    self.current_docs = u''
                    self.cli.buffers['clidocs'].reset(
                        initial_document=Document(self.current_docs,
                                                  cursor_position=0))
                    self.cli.request_redraw()
                    p = subprocess.Popen(full_cmd, shell=True, env=self._env)
                    p.communicate()

    def stop_input_and_refresh_cli(self):
        """Stop input by raising an `InputInterrupt`, forces a cli refresh.

        The cli refresh is necessary because changing options such as key
        bindings, single vs multi column menu completions, and the help pane
        all require a rebuild.

        :raises: :class:`InputInterrupt <exceptions.InputInterrupt>`.

        """
        self.refresh_cli = True
        self.cli.request_redraw()
        raise InputInterrupt

    def create_layout(self, display_completions_in_columns, toolbar):
        from awsshell.lexer import ShellLexer
        lexer = ShellLexer
        if self.config_section['theme'] == 'none':
            lexer = None
        return create_default_layout(
            self, u'aws> ', lexer=lexer, reserve_space_for_menu=True,
            display_completions_in_columns=display_completions_in_columns,
            get_bottom_toolbar_tokens=toolbar.handler)

    def create_buffer(self, completer, history):
        return Buffer(
            history=history,
            auto_suggest=AutoSuggestFromHistory(),
            enable_history_search=True,
            completer=completer,
            complete_while_typing=Always(),
            accept_action=AcceptAction.RETURN_DOCUMENT)

    def create_key_manager(self):
        """Create the :class:`KeyManager`.

        The inputs to KeyManager are expected to be callable, so we can't
        use the standard @property and @attrib.setter for these attributes.
        Lambdas cannot contain assignments so we're forced to define setters.

        :rtype: :class:`KeyManager`
        :return: A KeyManager with callables to set the toolbar options.  Also
            includes the method stop_input_and_refresh_cli to ensure certain
            options take effect within the current session.

        """
        def set_match_fuzzy(match_fuzzy):
            """Setter for fuzzy matching mode.

            :type match_fuzzy: bool
            :param match_fuzzy: The match fuzzy flag.

            """
            self.model_completer.match_fuzzy = match_fuzzy

        def set_enable_vi_bindings(enable_vi_bindings):
            """Setter for vi mode keybindings.

            If vi mode is off, emacs mode is enabled by default by
            `prompt_toolkit`.

            :type enable_vi_bindings: bool
            :param enable_vi_bindings: The enable Vi bindings flag.

            """
            self.enable_vi_bindings = enable_vi_bindings

        def set_show_completion_columns(show_completion_columns):
            """Setter for showing the completions in columns flag.

            :type show_completion_columns: bool
            :param show_completion_columns: The show completions in
                multiple columns flag.

            """
            self.show_completion_columns = show_completion_columns

        def set_show_help(show_help):
            """Setter for showing the help container flag.

            :type show_help: bool
            :param show_help: The show help flag.

            """
            self.show_help = show_help

        return KeyManager(
            lambda: self.model_completer.match_fuzzy, set_match_fuzzy,
            lambda: self.enable_vi_bindings, set_enable_vi_bindings,
            lambda: self.show_completion_columns, set_show_completion_columns,
            lambda: self.show_help, set_show_help,
            self.stop_input_and_refresh_cli)

    def create_application(self, completer, history,
                           display_completions_in_columns):
        self.key_manager = self.create_key_manager()
        toolbar = Toolbar(
            lambda: self.model_completer.match_fuzzy,
            lambda: self.enable_vi_bindings,
            lambda: self.show_completion_columns,
            lambda: self.show_help)
        style_factory = StyleFactory(self.theme)
        buffers = {
            'clidocs': Buffer(read_only=True)
        }

        return Application(
            layout=self.create_layout(display_completions_in_columns, toolbar),
            mouse_support=False,
            style=style_factory.style,
            buffers=buffers,
            buffer=self.create_buffer(completer, history),
            on_abort=AbortAction.RETRY,
            on_exit=AbortAction.RAISE_EXCEPTION,
            on_input_timeout=self.on_input_timeout,
            key_bindings_registry=self.key_manager.manager.registry,
        )

    def on_input_timeout(self, cli):
        if not self.show_help:
            return
        document = cli.current_buffer.document
        text = document.text
        LOG.debug("document.text = %s", text)
        LOG.debug("current_command = %s", self.completer.current_command)
        if text.strip():
            command = self.completer.current_command
            key_name = '.'.join(command.split()).encode('utf-8')
            last_option = self.completer.last_option
            if last_option:
                self.current_docs = self._docs.extract_param(
                    key_name, last_option)
            else:
                self.current_docs = self._docs.extract_description(key_name)
        else:
            self.current_docs = u''
        cli.buffers['clidocs'].reset(
            initial_document=Document(self.current_docs, cursor_position=0))
        cli.request_redraw()

    def create_cli_interface(self, display_completions_in_columns):
        # A CommandLineInterface from prompt_toolkit
        # accepts two things: an application and an
        # event loop.
        loop = create_eventloop()
        app = self.create_application(self.completer,
                                      self.file_history,
                                      display_completions_in_columns)
        cli = CommandLineInterface(application=app, eventloop=loop,
                                   input=self._input, output=self._output)
        return cli

    @property
    def profile(self):
        return self._profile

    @profile.setter
    def profile(self, new_profile_name):
        # There's only two things that need to know about new profile
        # changes.
        #
        # First, the actual command runner.  If we want
        # to use a different profile, it should ensure that the CLI
        # commands that get run use the new profile (via the
        # AWS_DEFAULT_PROFILE env var).
        #
        # Second, we also need to let the server side autocompleter know.
        #
        # Given this is easy to manage by hand, I don't think
        # it's worth adding an event system or observers just yet.
        # If this gets hard to manage, the complexity of those systems
        # would be worth it.
        self._env['AWS_DEFAULT_PROFILE'] = new_profile_name
        self.completer.change_profile(new_profile_name)
        self._profile = new_profile_name
Example #14
0
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit import prompt

from query_handler import predict

print("Welcome to PreCrimeBot!")
print("You can ask me to predict crime anywhere in London. Let's get started!")
print()
print("Each prediction will require a date and address as input.")
print("Don't worry, I understand most date and address formats.")
print("I will try to tell you when I can't understand you.")
print()
print("PS: type 'exit' and hit ENTER at any time to exit, or use CTLR+D")

address_history = InMemoryHistory()
date_history = InMemoryHistory()

while True:
    print('-' * 50)

    address = prompt("Address: ", history=address_history)
    if 'exit' == address:
        break

    date = prompt("Date: ", history=date_history)
    if 'exit' == date:
        break

    try:
        result = predict(date, address)
    except ValueError as e:
Example #15
0
    def __init__(self,
                 cli_ctx,
                 style=None,
                 completer=None,
                 styles=None,
                 lexer=None,
                 history=InMemoryHistory(),
                 app=None,
                 input_custom=sys.stdin,
                 output_custom=None,
                 user_feedback=False,
                 intermediate_sleep=.25,
                 final_sleep=4):

        from azclishell.color_styles import style_factory

        self.cli_ctx = cli_ctx
        self.config = Configuration(cli_ctx.config)
        self.config.set_style(style)
        self.styles = style or style_factory(self.config.get_style())
        self.lexer = lexer or get_az_lexer(
            self.config) if self.styles else None
        try:
            from azclishell.gather_commands import GatherCommands
            from azclishell.az_completer import AzCompleter
            self.completer = completer or AzCompleter(
                self, GatherCommands(self.config))
        except IOError:  # if there is no cache
            self.completer = None
        self.history = history or FileHistory(
            os.path.join(self.config.config_dir, self.config.get_history()))
        os.environ[ENV_ADDITIONAL_USER_AGENT] = 'AZURECLISHELL/' + __version__
        self.telemetry = Telemetry(self.cli_ctx)

        # OH WHAT FUN TO FIGURE OUT WHAT THESE ARE!
        self._cli = None
        self.refresh_cli = False
        self.layout = None
        self.description_docs = u''
        self.param_docs = u''
        self.example_docs = u''
        self._env = os.environ
        self.last = None
        self.last_exit = 0
        self.user_feedback = user_feedback
        self.input = input_custom
        self.output = output_custom
        self.config_default = ""
        self.default_command = ""
        self.threads = []
        self.curr_thread = None
        self.spin_val = -1
        self.intermediate_sleep = intermediate_sleep
        self.final_sleep = final_sleep

        # try to consolidate state information here...
        # These came from key bindings...
        self._section = 1
        self.is_prompting = False
        self.is_example_repl = False
        self.is_showing_default = False
        self.is_symbols = True
Example #16
0
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
import getpass
import logging

import requests
from prompt_toolkit import prompt
from prompt_toolkit.history import InMemoryHistory

logger = logging.getLogger(__name__)
history = InMemoryHistory()


# class PyborgCommandSuggest(AutoSuggest):
#    "NB: for pt v2"
#     def __init__(self):
#         super(PyborgCommandSuggest, self).__init__()

#     def get_suggestion(self, buffer, document):
#         #use document.text
#         if document.text.startswith("!"):
#             return Suggestion("!quit")
#         return None


class ModLineIn():
Example #17
0
    def __init__(self,
                 message='',
                 default='',
                 multiline=False,
                 wrap_lines=True,
                 is_password=False,
                 vi_mode=False,
                 editing_mode=EditingMode.EMACS,
                 complete_while_typing=True,
                 validate_while_typing=True,
                 enable_history_search=False,
                 search_ignore_case=False,
                 lexer=None,
                 enable_system_prompt=False,
                 enable_suspend=False,
                 enable_open_in_editor=False,
                 validator=None,
                 completer=None,
                 complete_in_thread=False,
                 reserve_space_for_menu=8,
                 complete_style=None,
                 auto_suggest=None,
                 style=None,
                 color_depth=None,
                 include_default_pygments_style=True,
                 history=None,
                 clipboard=None,
                 prompt_continuation=None,
                 rprompt=None,
                 bottom_toolbar=None,
                 mouse_support=False,
                 input_processors=None,
                 key_bindings=None,
                 erase_when_done=False,
                 tempfile_suffix='.txt',
                 inputhook=None,
                 refresh_interval=0,
                 input=None,
                 output=None):
        assert style is None or isinstance(style, BaseStyle)
        assert input_processors is None or isinstance(input_processors, list)
        assert key_bindings is None or isinstance(key_bindings,
                                                  KeyBindingsBase)

        # Defaults.
        output = output or get_default_output()
        input = input or get_default_input()

        history = history or InMemoryHistory()
        clipboard = clipboard or InMemoryClipboard()

        # Ensure backwards-compatibility, when `vi_mode` is passed.
        if vi_mode:
            editing_mode = EditingMode.VI

        # Store all settings in this class.
        self.input = input
        self.output = output

        # Store all settings in this class.
        for name in self._fields:
            if name not in ('editing_mode', ):
                value = locals()[name]
                setattr(self, name, value)

        # Create buffers, layout and Application.
        self.history = history
        self.default_buffer = self._create_default_buffer()
        self.search_buffer = self._create_search_buffer()
        self.layout = self._create_layout()
        self.app = self._create_application(editing_mode, erase_when_done)
Example #18
0
    def create_prompt_application(self,
                                  message='',
                                  multiline=False,
                                  wrap_lines=True,
                                  is_password=False,
                                  vi_mode=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=8,
                                  auto_suggest=None,
                                  style=None,
                                  history=None,
                                  clipboard=None,
                                  get_prompt_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=AbortAction.RAISE_EXCEPTION,
                                  on_exit=AbortAction.RAISE_EXCEPTION,
                                  accept_action=AcceptAction.RETURN_DOCUMENT,
                                  default=''):
        """Create an :class:`~Application` instance for a prompt.

        (It is meant to cover 90% of the prompt use cases, where no extreme
        customization is required. For more complex input, it is required to create
        a custom :class:`~Application` instance.)

        message : Text to be shown before the prompt.
        mulitiline : Allow multiline input. Pressing enter will insert a
                           newline. (This requires Meta+Enter to accept the input.)
        wrap_lines : `bool` or :class:`~prompt_toolkit.filters.CLIFilter`.
            When True (the default), automatically wrap long lines instead of
            scrolling horizontally.
        is_password : Show asterisks instead of the actual typed characters.
        vi_mode : `bool` or :class:`~prompt_toolkit.filters.CLIFilter`. If
            True, use Vi key bindings.
        complete_while_typing : `bool` or
            :class:`~prompt_toolkit.filters.CLIFilter`. Enable autocompletion while
            typing.
        enable_history_search : `bool` or
            :class:`~prompt_toolkit.filters.CLIFilter`. Enable up-arrow parting
            string matching.
        lexer : :class:`~prompt_toolkit.layout.lexers.Lexer` to be used for
            the syntax highlighting.
        validator : :class:`~prompt_toolkit.validation.Validator` instance
            for input validation.
        completer : :class:`~prompt_toolkit.completion.Completer` instance
            for input completion.
        reserve_space_for_menu : Space to be reserved for displaying the menu.
            (0 means that no space needs to be reserved.)
        auto_suggest : :class:`~prompt_toolkit.auto_suggest.AutoSuggest`
            instance for input suggestions.
        style : Pygments style class for the color scheme.
        enable_system_bindings : `bool` or
            :class:`~prompt_toolkit.filters.CLIFilter`. Pressing Meta+'!' will show
            a system prompt.
        enable_open_in_editor : `bool` or
            :class:`~prompt_toolkit.filters.CLIFilter`. Pressing 'v' in Vi mode or
            C-X C-E in emacs mode will open an external editor.
        history : :class:`~prompt_toolkit.history.History` instance.
        clipboard : :class:`~prompt_toolkit.clipboard.base.Clipboard` instance.
            (e.g. :class:`~prompt_toolkit.clipboard.in_memory.InMemoryClipboard`)
        get_bottom_toolbar_tokens : Optional callable which takes a
            :class:`~prompt_toolkit.interface.CommandLineInterface` and returns a
            list of tokens for the bottom toolbar.
        display_completions_in_columns : `bool` or
            :class:`~prompt_toolkit.filters.CLIFilter`. Display the completions in
            multiple columns.
        get_title : Callable that returns the title to be displayed in the
            terminal.
        mouse_support : `bool` or :class:`~prompt_toolkit.filters.CLIFilter`
            to enable mouse support.
        default : The default text to be shown in the input buffer. (This can
            be edited by the user.)

        Notes
        -----
        This method was forked from the mainline prompt-toolkit repo.
        Copyright (c) 2014, Jonathan Slenders, All rights reserved.

        WARNING; This method is due for removal once prompt-toolkit >v0.54 
        is released.
        """
        if key_bindings_registry is None:
            key_bindings_registry = KeyBindingManager.for_prompt(
                enable_vi_mode=vi_mode,
                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 = to_simple_filter(complete_while_typing)
        enable_history_search = to_simple_filter(enable_history_search)
        multiline = to_simple_filter(multiline)

        complete_while_typing = complete_while_typing & ~enable_history_search

        # Accept Pygments styles as well for backwards compatibility.
        try:
            if issubclass(style, pygments.style.Style):
                style = PygmentsStyle(style)
        except TypeError:  # Happens when style is `None` or an instance of something else.
            pass

        # Create application
        return Application(layout=self.create_prompt_layout(
            message=message,
            lexer=lexer,
            is_password=is_password,
            reserve_space_for_menu=(reserve_space_for_menu
                                    if completer is not None else 0),
            multiline=Condition(lambda cli: multiline()),
            get_prompt_tokens=get_prompt_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),
                           buffer=Buffer(
                               enable_history_search=enable_history_search,
                               complete_while_typing=complete_while_typing,
                               is_multiline=multiline,
                               history=(history or InMemoryHistory()),
                               validator=validator,
                               completer=completer,
                               auto_suggest=auto_suggest,
                               accept_action=accept_action,
                               initial_document=Document(default),
                           ),
                           style=style or DEFAULT_STYLE,
                           clipboard=clipboard,
                           key_bindings_registry=key_bindings_registry,
                           get_title=get_title,
                           mouse_support=mouse_support,
                           on_abort=on_abort,
                           on_exit=on_exit)
Example #19
0
    def init_prompt_toolkit_cli(self):
        if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty():
            # Fall back to plain non-interactive output for tests.
            # This is very limited, and only accepts a single line.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            return

        kbmanager = KeyBindingManager.for_prompt(enable_vi_mode=self.vi_mode)
        insert_mode = ViStateFilter(kbmanager.get_vi_state, InputMode.INSERT)
        # Ctrl+J == Enter, seemingly
        @kbmanager.registry.add_binding(Keys.ControlJ,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                   ))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >= d.line_count
                                           - d.empty_line_count_at_the_end()):
                b.newline()
                return

            status, indent = self.input_splitter.check_complete(d.text)

            if (status != 'incomplete') and b.accept_action.is_returnable:
                b.accept_action.validate_and_handle(event.cli, b)
            else:
                b.insert_text('\n' + (' ' * (indent or 0)))

        @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER))
        def _(event):
            event.current_buffer.reset()

        supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP'))

        @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend)
        def _(event):
            event.cli.suspend_to_background()

        @Condition
        def cursor_in_leading_ws(cli):
            before = cli.application.buffer.document.current_line_before_cursor
            return (not before) or before.isspace()

        # Ctrl+I == Tab
        @kbmanager.registry.add_binding(Keys.ControlI,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                    & cursor_in_leading_ws
                                   ))
        def _(event):
            event.current_buffer.insert_text(' ' * 4)

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for _, _, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)

        style_overrides = {
            Token.Prompt: '#009900',
            Token.PromptNum: '#00ff00 bold',
        }
        if self.highlighting_style:
            style_cls = get_style_by_name(self.highlighting_style)
        else:
            style_cls = get_style_by_name('default')
            # The default theme needs to be visible on both a dark background
            # and a light background, because we can't tell what the terminal
            # looks like. These tweaks to the default theme help with that.
            style_overrides.update({
                Token.Number: '#007700',
                Token.Operator: 'noinherit',
                Token.String: '#BB6622',
                Token.Name.Function: '#2080D0',
                Token.Name.Class: 'bold #2080D0',
                Token.Name.Namespace: 'bold #2080D0',
            })
        style_overrides.update(self.highlighting_style_overrides)
        style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
                                            style_dict=style_overrides)

        app = create_prompt_application(multiline=True,
                            lexer=IPythonPTLexer(),
                            get_prompt_tokens=self.get_prompt_tokens,
                            get_continuation_tokens=self.get_continuation_tokens,
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=IPythonPTCompleter(self.Completer),
                            enable_history_search=True,
                            style=style,
                            mouse_support=self.mouse_support,
                            reserve_space_for_menu=6,
        )

        self.pt_cli = CommandLineInterface(app,
                           eventloop=create_eventloop(self.inputhook))
Example #20
0
class PromptInterface(object):

    go_on = True

    completer = WordCompleter([
        'block', 'tx', 'header', 'mem', 'help', 'state', 'node', 'exit',
        'quit', 'config', 'db', 'log'
    ])

    commands = [
        'quit',
        'help',
        'block {index/hash}',
        'header {index/hash}',
        'tx {hash}',
        'mem',
        'nodes',
        'state',
        'config {node, db} int int'
        'config log on/off'
        'pause',
    ]

    token_style = style_from_dict({
        Token.Command: '#ff0066',
        Token.Neo: '#0000ee',
        Token.Default: '#00ee00',
        Token.Number: "#ffffff",
    })

    history = InMemoryHistory()

    start_height = Blockchain.Default().Height
    start_dt = datetime.datetime.utcnow()

    node_leader = None

    paused = False

    def paused_loop(self):

        while self.paused:
            #            self.__log.debug("paused...")
            time.sleep(1)

    def get_bottom_toolbar(self, cli=None):
        try:
            return [(Token.Command, 'Progress: '),
                    (Token.Number, str(Blockchain.Default().Height)),
                    (Token.Neo, '/'),
                    (Token.Number, str(Blockchain.Default().HeaderHeight))]
        except Exception as e:
            print("couldnt get toolbar: %s " % e)
            return []

    def quit(self):
        print('Shutting down.  This may take a bit...')
        self.go_on = False
        self.paused = False
        Blockchain.Default().Dispose()
        reactor.stop()
        self.node_leader.Shutdown()

    def help(self):
        tokens = []
        for c in self.commands:
            tokens.append((Token.Command, "%s\n" % c))
        print_tokens(tokens, self.token_style)

    def toggle_pause(self):
        if self.paused:
            self.paused = not self.paused
            #            reactor.run()
            print("resusiming execution")

        else:
            self.paused = not self.paused
            print('pausing execution!')
            reactor.callLater(1, self.paused_loop)

    def show_state(self):
        height = Blockchain.Default().Height
        headers = Blockchain.Default().HeaderHeight

        diff = height - self.start_height
        now = datetime.datetime.utcnow()
        difftime = now - self.start_dt

        mins = difftime / datetime.timedelta(minutes=1)

        bpm = 0
        if diff > 0 and mins > 0:
            bpm = diff / mins

        out = 'Progress: %s / %s\n' % (height, headers)
        out += 'Block Cache length %s\n' % Blockchain.Default().BlockCacheCount
        out += 'Blocks since program start %s\n' % diff
        out += 'Time elapsed %s mins\n' % mins
        out += 'blocks per min %s \n' % bpm
        #        out += "Node Req Part, Max: %s %s\n" % (self.node_leader.BREQPART, self.node_leader.BREQMAX)
        #        out += "DB Cache Lim, Miss Lim %s %s\n" % (Blockchain.Default().CACHELIM, Blockchain.Default().CMISSLIM)
        tokens = [(Token.Number, out)]
        print_tokens(tokens, self.token_style)

    def show_nodes(self):
        if self.node_leader and len(self.node_leader.Peers):
            out = ''
            for peer in self.node_leader.Peers:
                out += 'Peer %s - IO: %s\n' % (peer.Name(), peer.IOStats())
            print_tokens([(Token.Number, out)], self.token_style)
        else:
            print('Not connected yet\n')

    def show_block(self, args):
        item = self.get_arg(args)
        txarg = self.get_arg(args, 1)
        if item is not None:
            block = Blockchain.Default().GetBlock(item)

            if block is not None:
                bjson = json.dumps(block.ToJson(), indent=4)
                tokens = [(Token.Number, bjson)]
                print_tokens(tokens, self.token_style)
                print('\n')
                if txarg and 'tx' in txarg:

                    for tx in block.Transactions:
                        self.show_tx([tx])

            else:
                print("could not locate block %s" % item)
        else:
            print("please specify a block")

    def show_header(self, args):
        item = self.get_arg(args)
        if item is not None:
            header = Blockchain.Default().GetHeaderBy(item)
            if header is not None:
                print(json.dumps(header.ToJson(), indent=4))
            else:
                print("could not locate Header %s \n" % item)
        else:
            print("please specify a header")

    def show_tx(self, args):
        item = self.get_arg(args)
        if item is not None:
            try:
                tx, height = Blockchain.Default().GetTransaction(item)
                if height > -1:

                    bjson = json.dumps(tx.ToJson(), indent=4)
                    tokens = [(Token.Command, bjson)]
                    print_tokens(tokens, self.token_style)
                    print('\n')
            except Exception as e:
                print("Could not find transaction with id %s " % item)
                print(
                    "Please specify a tx hash like 'db55b4d97cf99db6826967ef4318c2993852dff3e79ec446103f141c716227f6'"
                )
        else:
            print("please specify a tx hash")

    def show_account_state(self, args):
        item = self.get_arg(args)
        print("account to show %s " % item)

        if item is not None:
            account = Blockchain.Default().GetAccountState(
                item, print_all_accounts=True)

            if account is not None:
                bjson = json.dumps(account.ToJson(), indent=4)
                tokens = [(Token.Number, bjson)]
                print_tokens(tokens, self.token_style)
                print('\n')
            else:
                print("account %s not found" % item)
        else:
            print("please specify an account address")

    def show_asset_state(self, args):
        item = self.get_arg(args)
        print("asset to show %s " % item)

        if item is not None:
            asset = Blockchain.Default().GetAssetState(item)

            if asset is not None:
                bjson = json.dumps(asset.ToJson(), indent=4)
                tokens = [(Token.Number, bjson)]
                print_tokens(tokens, self.token_style)
                print('\n')
            else:
                print("asset %s not found" % item)
        else:
            print("please specify an asset hash")

    def show_contract_state(self, args):
        item = self.get_arg(args)

        if item is not None:

            if item.lower() == 'all':
                contracts = Blockchain.Default().ShowAllContracts()
                print("contracts: %s " % contracts)
            else:
                contract = Blockchain.Default().GetContract(item)
                if contract is not None:
                    bjson = json.dumps(contract.ToJson(), indent=4)
                    tokens = [(Token.Number, bjson)]
                    print_tokens(tokens, self.token_style)
                    print('\n')
        else:
            print("please specify a contract")

    def show_spent_coins(self, args):
        item = self.get_arg(args)

        if item is not None:
            if item.lower() == 'all':
                coins = Blockchain.Default().GetAllSpentCoins()
                print("coins %s " % coins)
            else:

                coin = Blockchain.Default().GetSpentCoins(item)
                if coin is not None:
                    bjson = json.dumps(coin.ToJson(), indent=4)
                    tokens = [(Token.Number, bjson)]
                    print_tokens(tokens, self.token_style)
                    print("\n")
        else:
            print("please specify a tx hash")

    def show_mem(self):
        total = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
        totalmb = total / 1000000
        out = "Total: %s MB\n" % totalmb
        out += "total buffers %s\n" % StreamManager.TotalBuffers()
        print_tokens([(Token.Number, out)], self.token_style)

    def configure(self, args):
        what = self.get_arg(args)

        if what == 'node':
            c1 = self.get_arg(args, 1, convert_to_int=True)
            c2 = self.get_arg(args, 2, convert_to_int=True)

            if not c1:
                print("please provide settings. arguments must be integers")

            if c1 is not None and c1 > 1:
                if c1 > 200:
                    c1 = 200
                    print("Node request part must be less than 201")
                self.node_leader.BREQPART = c1
                print("Set Node Request Part to %s " % c1)
            if c2 is not None and c2 >= self.node_leader.BREQPART:
                self.node_leader.BREQMAX = c2
                print("Set Node Request Max to %s " % c2)
            self.show_state()
        elif what == 'db':
            c1 = self.get_arg(args, 1, convert_to_int=True)
            c2 = self.get_arg(args, 2, convert_to_int=True)
            if c1 is not None and c1 > 1:
                Blockchain.Default().CACHELIM = c1
                print("Set DB Cache Limit to %s " % c1)
            if c2 is not None and c2 >= self.node_leader.BREQPART:
                Blockchain.Default().CMISSLIM = c2
                print("Set DB Cache Miss Limit %s " % c2)
            self.show_state()
        elif what == 'log' or what == 'logs':
            c1 = self.get_arg(args, 1).lower()
            if c1 is not None:
                if c1 == 'on' or c1 == '1':
                    print("turning on logging")
                    logger = logging.getLogger()
                    logger.setLevel(logging.DEBUG)
                if c1 == 'off' or c1 == '0':
                    print("turning off logging")
                    logger = logging.getLogger()
                    logger.setLevel(logging.ERROR)

            else:
                print("cannot configure log.  Please specify on or off")
        else:
            print("cannot configure %s " % what)
            print(
                "Try 'config node 100 1000' or config db 1000 4' or config log on/off"
            )

    def get_arg(self, arguments, index=0, convert_to_int=False):
        try:
            arg = arguments[index]
            if convert_to_int:
                return int(arg)
            return arg
        except Exception as e:
            pass
        return None

    def parse_result(self, result):
        if len(result):
            commandParts = [s for s in result.split()]
            return commandParts[0], commandParts[1:]
        return None, None

    def run(self):

        dbloop = task.LoopingCall(Blockchain.Default().PersistBlocks)
        dbloop.start(.1)

        Blockchain.Default().PersistBlocks()

        self.node_leader = NodeLeader.Instance()

        tokens = [(Token.Neo, 'NEO'), (Token.Default, ' cli. Type '),
                  (Token.Command, "'help' "),
                  (Token.Default, 'to get started')]
        print_tokens(tokens, self.token_style)
        print("\n")

        while self.go_on:

            result = prompt("neo> ",
                            completer=self.completer,
                            history=self.history,
                            get_bottom_toolbar_tokens=self.get_bottom_toolbar,
                            style=self.token_style)

            command, arguments = self.parse_result(result)

            if command is not None and len(command) > 0:
                command = command.lower()

                if command == 'quit' or command == 'exit':
                    self.quit()
                elif command == 'help':
                    self.help()
                elif command == 'block':
                    self.show_block(arguments)
                elif command == 'tx':
                    self.show_tx(arguments)
                elif command == 'header':
                    self.show_header(arguments)
                elif command == 'account':
                    self.show_account_state(arguments)
                elif command == 'asset':
                    self.show_asset_state(arguments)
                elif command == 'contract':
                    self.show_contract_state(arguments)
                elif command == 'sc':
                    self.show_spent_coins(arguments)
                elif command == 'mem':
                    self.show_mem()
                elif command == 'nodes' or command == 'node':
                    self.show_nodes()
                elif command == 'state':
                    self.show_state()
                elif command == 'config':
                    self.configure(arguments)
                elif command == 'pause' or command == 'unpause' or command == 'resume':
                    self.toggle_pause()
                elif command == None:
                    print('please specify a command')
                else:
                    print("command %s not found" % command)

            else:
                pass
Example #21
0
    def init_prompt_toolkit_cli(self):
        if 'JUPYTER_CONSOLE_TEST' in os.environ:
            # Simple restricted interface for tests so we can find prompts with
            # pexpect. Multi-line input not supported.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            self.print_out_prompt = \
                lambda: print('Out[%d]: ' % self.execution_count, end='')
            return

        kbmanager = KeyBindingManager.for_prompt()
        insert_mode = ViInsertMode() | EmacsInsertMode()
        # Ctrl+J == Enter, seemingly
        @kbmanager.registry.add_binding(Keys.ControlJ,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                   ))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >= d.line_count
                                           - d.empty_line_count_at_the_end()):
                b.newline()
                return

            more, indent = self.check_complete(d.text)

            if (not more) and b.accept_action.is_returnable:
                b.accept_action.validate_and_handle(event.cli, b)
            else:
                b.insert_text('\n' + indent)

        @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER))
        def _(event):
            event.current_buffer.reset()

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for _, _, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)

        style_overrides = {
            Token.Prompt: '#009900',
            Token.PromptNum: '#00ff00 bold',
            Token.OutPrompt: '#ff2200',
            Token.OutPromptNum: '#ff0000 bold',
        }
        if self.highlighting_style:
            style_cls = get_style_by_name(self.highlighting_style)
        else:
            style_cls = get_style_by_name('default')
            # The default theme needs to be visible on both a dark background
            # and a light background, because we can't tell what the terminal
            # looks like. These tweaks to the default theme help with that.
            style_overrides.update({
                Token.Number: '#007700',
                Token.Operator: 'noinherit',
                Token.String: '#BB6622',
                Token.Name.Function: '#2080D0',
                Token.Name.Class: 'bold #2080D0',
                Token.Name.Namespace: 'bold #2080D0',
            })
        style_overrides.update(self.highlighting_style_overrides)
        style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
                                            style_dict=style_overrides)

        editing_mode = getattr(EditingMode, self.editing_mode.upper())
        langinfo = self.kernel_info.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', 'text'))
        app = create_prompt_application(multiline=True,
                            editing_mode=editing_mode,
                            lexer=PygmentsLexer(get_pygments_lexer(lexer)),
                            get_prompt_tokens=self.get_prompt_tokens,
                            get_continuation_tokens=self.get_continuation_tokens,
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=JupyterPTCompleter(self.Completer),
                            enable_history_search=True,
                            style=style,
        )

        self._eventloop = create_eventloop()
        self.pt_cli = CommandLineInterface(app, eventloop=self._eventloop)
Example #22
0
    def init_prompt_toolkit_cli(self):
        if self.simple_prompt or ('JUPYTER_CONSOLE_TEST' in os.environ):
            # Simple restricted interface for tests so we can find prompts with
            # pexpect. Multi-line input not supported.
            async def prompt():
                prompt = 'In [%d]: ' % self.execution_count
                raw = await async_input(prompt)
                return raw

            self.prompt_for_code = prompt
            self.print_out_prompt = \
                lambda: print('Out[%d]: ' % self.execution_count, end='')
            return

        kb = KeyBindings()
        insert_mode = vi_insert_mode | emacs_insert_mode

        @kb.add("enter",
                filter=(has_focus(DEFAULT_BUFFER)
                        & ~has_selection
                        & insert_mode))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >=
                    d.line_count - d.empty_line_count_at_the_end()):
                b.newline()
                return

            # Pressing enter flushes any pending display. This also ensures
            # the displayed execution_count is correct.
            self.handle_iopub()

            more, indent = self.check_complete(d.text)

            if (not more) and b.accept_handler:
                b.validate_and_handle()
            else:
                b.insert_text('\n' + indent)

        @kb.add("c-c", filter=has_focus(DEFAULT_BUFFER))
        def _(event):
            event.current_buffer.reset()

        @kb.add("c-\\", filter=has_focus(DEFAULT_BUFFER))
        def _(event):
            raise EOFError

        @kb.add("c-z",
                filter=Condition(lambda: suspend_to_background_supported()))
        def _(event):
            event.cli.suspend_to_background()

        @kb.add("c-o", filter=(has_focus(DEFAULT_BUFFER) & emacs_insert_mode))
        def _(event):
            event.current_buffer.insert_text("\n")

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for _, _, cell in self.history_manager.get_tail(
                self.history_load_length, include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append_string(cell)

        style_overrides = {
            Token.Prompt: '#009900',
            Token.PromptNum: '#00ff00 bold',
            Token.OutPrompt: '#ff2200',
            Token.OutPromptNum: '#ff0000 bold',
            Token.RemotePrompt: '#999900',
        }
        if self.highlighting_style:
            style_cls = get_style_by_name(self.highlighting_style)
        else:
            style_cls = get_style_by_name('default')
            # The default theme needs to be visible on both a dark background
            # and a light background, because we can't tell what the terminal
            # looks like. These tweaks to the default theme help with that.
            style_overrides.update({
                Token.Number: '#007700',
                Token.Operator: 'noinherit',
                Token.String: '#BB6622',
                Token.Name.Function: '#2080D0',
                Token.Name.Class: 'bold #2080D0',
                Token.Name.Namespace: 'bold #2080D0',
            })
        style_overrides.update(self.highlighting_style_overrides)
        style = merge_styles([
            style_from_pygments_cls(style_cls),
            style_from_pygments_dict(style_overrides),
        ])

        editing_mode = getattr(EditingMode, self.editing_mode.upper())
        langinfo = self.kernel_info.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', 'text'))

        # If enabled in the settings, highlight matching brackets
        # when the DEFAULT_BUFFER has the focus
        input_processors = [
            ConditionalProcessor(
                processor=HighlightMatchingBracketProcessor(chars='[](){}'),
                filter=has_focus(DEFAULT_BUFFER) & ~is_done
                & Condition(lambda: self.highlight_matching_brackets))
        ]

        # Tell prompt_toolkit to use the asyncio event loop.
        # Obsolete in prompt_toolkit.v3
        if not PTK3:
            use_asyncio_event_loop()

        self.pt_cli = PromptSession(
            message=(lambda: PygmentsTokens(self.get_prompt_tokens())),
            multiline=True,
            complete_style=self.pt_complete_style,
            editing_mode=editing_mode,
            lexer=PygmentsLexer(get_pygments_lexer(lexer)),
            prompt_continuation=(
                lambda width, lineno, is_soft_wrap: PygmentsTokens(
                    self.get_continuation_tokens(width))),
            key_bindings=kb,
            history=history,
            completer=JupyterPTCompleter(self.Completer),
            enable_history_search=True,
            style=style,
            input_processors=input_processors,
            color_depth=(ColorDepth.TRUE_COLOR if self.true_color else None),
        )
Example #23
0
    def __init__(
        self,
        get_globals: Optional[_GetNamespace] = None,
        get_locals: Optional[_GetNamespace] = None,
        history_filename: Optional[str] = None,
        vi_mode: bool = False,
        color_depth: Optional[ColorDepth] = None,
        # Input/output.
        input: Optional[Input] = None,
        output: Optional[Output] = None,
        # For internal use.
        extra_key_bindings: Optional[KeyBindings] = None,
        _completer: Optional[Completer] = None,
        _validator: Optional[Validator] = None,
        _lexer: Optional[Lexer] = None,
        _extra_buffer_processors=None,
        _extra_layout_body=None,
        _extra_toolbars=None,
        _input_buffer_height=None,
    ) -> None:

        self.get_globals: _GetNamespace = get_globals or (lambda: {})
        self.get_locals: _GetNamespace = get_locals or self.get_globals

        self._completer = _completer or FuzzyCompleter(
            PythonCompleter(
                self.get_globals,
                self.get_locals,
                lambda: self.enable_dictionary_completion,
            ),
            enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion),
        )
        self._validator = _validator or PythonValidator(
            self.get_compiler_flags)
        self._lexer = _lexer or PygmentsLexer(PythonLexer)

        self.history: History
        if history_filename:
            self.history = ThreadedHistory(FileHistory(history_filename))
        else:
            self.history = InMemoryHistory()

        self._input_buffer_height = _input_buffer_height
        self._extra_layout_body = _extra_layout_body or []
        self._extra_toolbars = _extra_toolbars or []
        self._extra_buffer_processors = _extra_buffer_processors or []

        self.extra_key_bindings = extra_key_bindings or KeyBindings()

        # Settings.
        self.title: AnyFormattedText = ""
        self.show_signature: bool = False
        self.show_docstring: bool = False
        self.show_meta_enter_message: bool = True
        self.completion_visualisation: CompletionVisualisation = CompletionVisualisation.MULTI_COLUMN
        self.completion_menu_scroll_offset: int = 1

        self.show_line_numbers: bool = False
        self.show_status_bar: bool = True
        self.wrap_lines: bool = True
        self.complete_while_typing: bool = True
        self.paste_mode: bool = False  # When True, don't insert whitespace after newline.
        self.confirm_exit: bool = True  # Ask for confirmation when Control-D is pressed.
        self.accept_input_on_enter: int = 2  # Accept when pressing Enter 'n' times.
        # 'None' means that meta-enter is always required.
        self.enable_open_in_editor: bool = True
        self.enable_system_bindings: bool = True
        self.enable_input_validation: bool = True
        self.enable_auto_suggest: bool = False
        self.enable_mouse_support: bool = False
        self.enable_history_search: bool = False  # When True, like readline, going
        # back in history will filter the
        # history on the records starting
        # with the current input.

        self.enable_syntax_highlighting: bool = True
        self.enable_fuzzy_completion: bool = False
        self.enable_dictionary_completion: bool = False
        self.swap_light_and_dark: bool = False
        self.highlight_matching_parenthesis: bool = False
        self.show_sidebar: bool = False  # Currently show the sidebar.

        # When the sidebar is visible, also show the help text.
        self.show_sidebar_help: bool = True

        # Currently show 'Do you really want to exit?'
        self.show_exit_confirmation: bool = False

        # The title to be displayed in the terminal. (None or string.)
        self.terminal_title: Optional[str] = None

        self.exit_message: str = "Do you really want to exit?"
        self.insert_blank_line_after_output: bool = True  # (For the REPL.)

        # The buffers.
        self.default_buffer = self._create_buffer()
        self.search_buffer: Buffer = Buffer()
        self.docstring_buffer: Buffer = Buffer(read_only=True)

        # Tokens to be shown at the prompt.
        self.prompt_style: str = "classic"  # The currently active style.

        # Styles selectable from the menu.
        self.all_prompt_styles: Dict[str, PromptStyle] = {
            "ipython": IPythonPrompt(self),
            "classic": ClassicPrompt(),
        }

        self.get_input_prompt = lambda: self.all_prompt_styles[
            self.prompt_style].in_prompt()

        self.get_output_prompt = lambda: self.all_prompt_styles[
            self.prompt_style].out_prompt()

        #: Load styles.
        self.code_styles: Dict[str, BaseStyle] = get_all_code_styles()
        self.ui_styles = get_all_ui_styles()
        self._current_code_style_name: str = "default"
        self._current_ui_style_name: str = "default"

        if is_windows():
            self._current_code_style_name = "win32"

        self._current_style = self._generate_style()
        self.color_depth: ColorDepth = color_depth or ColorDepth.default()

        self.max_brightness: float = 1.0
        self.min_brightness: float = 0.0

        # Options to be configurable from the sidebar.
        self.options = self._create_options()
        self.selected_option_index: int = 0

        #: Incremeting integer counting the current statement.
        self.current_statement_index: int = 1

        # Code signatures. (This is set asynchronously after a timeout.)
        self.signatures: List[Any] = []

        # Boolean indicating whether we have a signatures thread running.
        # (Never run more than one at the same time.)
        self._get_signatures_thread_running: bool = False

        # Get into Vi navigation mode at startup
        self.vi_start_in_navigation_mode: bool = False

        # Preserve last used Vi input mode between main loop iterations
        self.vi_keep_last_used_mode: bool = False

        self.style_transformation = merge_style_transformations([
            ConditionalStyleTransformation(
                SwapLightAndDarkStyleTransformation(),
                filter=Condition(lambda: self.swap_light_and_dark),
            ),
            AdjustBrightnessStyleTransformation(lambda: self.min_brightness,
                                                lambda: self.max_brightness),
        ])
        self.ptpython_layout = PtPythonLayout(
            self,
            lexer=DynamicLexer(lambda: self._lexer if self.
                               enable_syntax_highlighting else SimpleLexer()),
            input_buffer_height=self._input_buffer_height,
            extra_buffer_processors=self._extra_buffer_processors,
            extra_body=self._extra_layout_body,
            extra_toolbars=self._extra_toolbars,
        )

        self.app = self._create_application(input, output)

        if vi_mode:
            self.app.editing_mode = EditingMode.VI
Example #24
0
# Definitions of commands
CMD_HELP = "help"
CMD_LOAD = "load"
CMD_REGISTERS = "registers"
CMD_FIND = "find"
CMD_PAYLOAD = "payload"
CMD_CONTEXT = "context"
CMD_CONFIG = "config"
CMD_EXPLOIT = "exploit"
CMD_EXIT = "exit"

command_list = [
    CMD_HELP, CMD_LOAD, CMD_REGISTERS, CMD_FIND, CMD_CONFIG, CMD_EXIT
]
command_completer = WordCompleter(command_list)
command_history = InMemoryHistory()


def main(time_mesure=False):

    if (time_mesure):
        pr = cProfile.Profile()
        pr.enable()

    #try:
    # Launching ROPGenerator
    write_colored(ASCII_art)
    Config.load_config()
    Logs.init()
    quit = False
    while (not quit):
Example #25
0
    def pt_init(self, pt_session_options=None):
        """Initialize the prompt session and the prompt loop
        and store them in self.pt_app and self.pt_loop.

        Additional keyword arguments for the PromptSession class
        can be specified in pt_session_options.
        """
        if pt_session_options is None:
            pt_session_options = {}

        def get_prompt_tokens():
            return [(Token.Prompt, self.prompt)]

        if self._ptcomp is None:
            compl = IPCompleter(shell=self.shell,
                                namespace={},
                                global_namespace={},
                                parent=self.shell)
            # add a completer for all the do_ methods
            methods_names = [m[3:] for m in dir(self) if m.startswith("do_")]

            def gen_comp(self, text):
                return [m for m in methods_names if m.startswith(text)]

            import types
            newcomp = types.MethodType(gen_comp, compl)
            compl.custom_matchers.insert(0, newcomp)
            # end add completer.

            self._ptcomp = IPythonPTCompleter(compl)

        # setup history only when we start pdb
        if self.shell.debugger_history is None:
            if self.shell.debugger_history_file is not None:

                p = Path(self.shell.debugger_history_file).expanduser()
                if not p.exists():
                    p.touch()
                self.debugger_history = FileHistory(os.path.expanduser(str(p)))
            else:
                self.debugger_history = InMemoryHistory()
        else:
            self.debugger_history = self.shell.debugger_history

        options = dict(
            message=(lambda: PygmentsTokens(get_prompt_tokens())),
            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
            key_bindings=create_ipython_shortcuts(self.shell),
            history=self.debugger_history,
            completer=self._ptcomp,
            enable_history_search=True,
            mouse_support=self.shell.mouse_support,
            complete_style=self.shell.pt_complete_style,
            style=getattr(self.shell, "style", None),
            color_depth=self.shell.color_depth,
        )

        if not PTK3:
            options['inputhook'] = self.shell.inputhook
        options.update(pt_session_options)
        self.pt_loop = asyncio.new_event_loop()
        self.pt_app = PromptSession(**options)
Example #26
0
    def init_prompt_toolkit_cli(self):
        self._app = None
        if self.simple_prompt:
            # Fall back to plain non-interactive output for tests.
            # This is very limited, and only accepts a single line.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            return

        kbmanager = KeyBindingManager.for_prompt()
        insert_mode = ViInsertMode() | EmacsInsertMode()
        # Ctrl+J == Enter, seemingly
        @kbmanager.registry.add_binding(Keys.ControlJ,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                   ))
        def _(event):
            b = event.current_buffer
            d = b.document

            if b.complete_state:
                cc = b.complete_state.current_completion
                if cc:
                    b.apply_completion(cc)
                else:
                    b.cancel_completion()
                return

            if not (d.on_last_line or d.cursor_position_row >= d.line_count
                                           - d.empty_line_count_at_the_end()):
                b.newline()
                return

            status, indent = self.input_splitter.check_complete(d.text)

            if (status != 'incomplete') and b.accept_action.is_returnable:
                b.accept_action.validate_and_handle(event.cli, b)
            else:
                b.insert_text('\n' + (' ' * (indent or 0)))

        @kbmanager.registry.add_binding(Keys.ControlP, filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER)))
        def _previous_history_or_previous_completion(event):
            """
            Control-P in vi edit mode on readline is history next, unlike default prompt toolkit.

            If completer is open this still select previous completion.
            """
            event.current_buffer.auto_up()

        @kbmanager.registry.add_binding(Keys.ControlN, filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER)))
        def _next_history_or_next_completion(event):
            """
            Control-N in vi edit mode on readline is history previous, unlike default prompt toolkit.

            If completer is open this still select next completion.
            """
            event.current_buffer.auto_down()

        @kbmanager.registry.add_binding(Keys.ControlG, filter=(
            HasFocus(DEFAULT_BUFFER) & HasCompletions()
            ))
        def _dismiss_completion(event):
            b = event.current_buffer
            if b.complete_state:
                b.cancel_completion()

        @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER))
        def _reset_buffer(event):
            b = event.current_buffer
            if b.complete_state:
                b.cancel_completion()
            else:
                b.reset()

        @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER))
        def _reset_search_buffer(event):
            if event.current_buffer.document.text:
                event.current_buffer.reset()
            else:
                event.cli.push_focus(DEFAULT_BUFFER)

        supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP'))

        @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend)
        def _suspend_to_bg(event):
            event.cli.suspend_to_background()

        @Condition
        def cursor_in_leading_ws(cli):
            before = cli.application.buffer.document.current_line_before_cursor
            return (not before) or before.isspace()

        # Ctrl+I == Tab
        @kbmanager.registry.add_binding(Keys.ControlI,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                    & cursor_in_leading_ws
                                   ))
        def _indent_buffer(event):
            event.current_buffer.insert_text(' ' * 4)

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)

        self._style = self._make_style_from_name(self.highlighting_style)
        style = DynamicStyle(lambda: self._style)

        editing_mode = getattr(EditingMode, self.editing_mode.upper())

        self._app = create_prompt_application(
                            editing_mode=editing_mode,
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=IPythonPTCompleter(self.Completer),
                            enable_history_search=True,
                            style=style,
                            mouse_support=self.mouse_support,
                            **self._layout_options()
        )
        self._eventloop = create_eventloop(self.inputhook)
        self.pt_cli = CommandLineInterface(self._app, eventloop=self._eventloop)
Example #27
0
class PromptInterface(object):

    go_on = True

    completer = WordCompleter([
        'block',
        'tx',
        'header',
        'mem',
        'help',
        'state',
        'node',
        'exit',
        'quit',
        'config',
        'import',
        'export',
        'open',
        'wallet',
        'contract',
        'asset',
    ])

    _gathering_password = False
    _gathered_passwords = []
    _gather_password_action = None
    _gather_address_str = None
    _num_passwords_req = 0
    _wallet_create_path = None
    _wallet_send_tx = None

    _invoke_test_tx = None
    _invoke_test_tx_fee = None

    Wallet = None

    commands = [
        'quit',
        'help',
        'block {index/hash}',
        'header {index/hash}',
        'tx {hash}',
        'asset {assetId}',
        'asset search {query}',
        'contract {contract hash}',
        'contract search {query}',
        'mem',
        'nodes',
        'state',
        'config log {on/off}',
        'import wif {wif}',
        'import contract {path} {params} {returntype}',
        'export wif {address}'
        'open wallet {path}',
        'create wallet {path}',
        'wallet {verbose}',
        'wallet rebuild {start block}',
        'send {assetId or name} {address} {amount}',
        'testinvoke {contract hash} {params}',
        'invoke',
        'cancel',
    ]

    token_style = style_from_dict({
        Token.Command: '#ff0066',
        Token.Neo: '#0000ee',
        Token.Default: '#00ee00',
        Token.Number: "#ffffff",
    })

    history = InMemoryHistory()

    start_height = Blockchain.Default().Height
    start_dt = datetime.datetime.utcnow()

    def get_bottom_toolbar(self, cli=None):
        out = []
        try:
            out = [(Token.Command, 'Progress: '),
                   (Token.Number, str(Blockchain.Default().Height)),
                   (Token.Neo, '/'),
                   (Token.Number, str(Blockchain.Default().HeaderHeight))]
        except Exception as e:
            pass

        return out

    def quit(self):
        print('Shutting down.  This may take a bit...')
        self.go_on = False
        Blockchain.Default().Dispose()
        reactor.stop()
        NodeLeader.Instance().Shutdown()

    def help(self):
        tokens = []
        for c in self.commands:
            tokens.append((Token.Command, "%s\n" % c))
        print_tokens(tokens, self.token_style)

    def do_open(self, arguments):
        item = get_arg(arguments)

        if item and item == 'wallet':

            path = get_arg(arguments, 1)

            if path:

                if not os.path.exists(path):
                    print("wallet file not found")
                    return

                self._num_passwords_req = 1
                self._wallet_create_path = path
                self._gathered_passwords = []
                self._gathering_password = True
                self._gather_password_action = self.do_open_wallet
            else:
                print("Please specify a path")
        else:
            print("item is? %s " % item)

    def do_create(self, arguments):
        item = get_arg(arguments)

        if item and item == 'wallet':

            path = get_arg(arguments, 1)

            if path:

                if os.path.exists(path):
                    print("File already exists")
                    return

                self._num_passwords_req = 2
                self._wallet_create_path = path
                self._gathered_passwords = []
                self._gathering_password = True
                self._gather_password_action = self.do_create_wallet

    #           print("create wallet! Please specify a password")
            else:
                print("Please specify a path")

    def do_create_wallet(self):
        #        print("do create wallet with passwords %s "% self._gathered_passwords)
        psswds = self._gathered_passwords
        path = self._wallet_create_path
        self._wallet_create_path = None
        self._gathered_passwords = None
        self._gather_password_action = None

        if len(psswds) != 2 or psswds[0] != psswds[1] or len(psswds[0]) < 10:
            print(
                "please provide matching passwords that are at least 10 characters long"
            )
            return

        passwd = psswds[1]

        try:
            self.Wallet = UserWallet.Create(path=path, password=passwd)
        except Exception as e:
            print("Exception creating wallet: %s " % e)

        contract = self.Wallet.GetDefaultContract()
        key = self.Wallet.GetKey(contract.PublicKeyHash)

        print("Wallet %s " % json.dumps(self.Wallet.ToJson(), indent=4))
        print("pubkey %s " % key.PublicKey.encode_point(True))

        dbloop = task.LoopingCall(self.Wallet.ProcessBlocks)
        dbloop.start(1)

    def do_open_wallet(self):
        passwd = self._gathered_passwords[0]
        path = self._wallet_create_path
        self._wallet_create_path = None
        self._gathered_passwords = None
        self._gather_password_action = None

        try:
            self.Wallet = UserWallet.Open(path, passwd)

            dbloop = task.LoopingCall(self.Wallet.ProcessBlocks)
            dbloop.start(1)
            print("Opened wallet at %s" % path)
        except Exception as e:
            print("could not open wallet: %s " % e)


#            traceback.print_stack()
#            traceback.print_exc()

    def do_import(self, arguments):
        item = get_arg(arguments)

        if item:

            if item == 'wif':

                if not self.Wallet:
                    print("Please open a wallet before importing WIF")
                    return

                wif = get_arg(arguments, 1)

                if wif:
                    prikey = KeyPair.PrivateKeyFromWIF(wif)
                    if prikey:

                        key = self.Wallet.CreateKey(prikey)
                        print("imported key %s " % wif)
                        print("Pubkey: %s \n" %
                              key.PublicKey.encode_point(True).hex())
                        print("Wallet: %s " %
                              json.dumps(self.Wallet.ToJson(), indent=4))
                    else:
                        print("invalid wif")
                    return

            elif item == 'contract':
                return self.load_smart_contract(arguments)

            elif item == 'contract_addr':
                return ImportContractAddr(self.Wallet, arguments[1:])

        print("please specify something to import")
        return

    def do_export(self, arguments):
        item = get_arg(arguments)

        if item == 'wif':

            if not self.Wallet:
                print("please open a wallet")
                return
            addr = get_arg(arguments, 1)

            if not addr:
                print('please specify an address')
                return

            if not self.Wallet.ContainsAddressStr(addr):
                print("address %s not found in wallet" % addr)
                return

            self._num_passwords_req = 1
            self._gather_address_str = addr
            self._gathered_passwords = []
            self._gathering_password = True
            self._gather_password_action = self.do_export_wif
            return

        print("Command export %s not found" % item)

    def do_export_wif(self):
        passwd = self._gathered_passwords[0]
        address = self._gather_address_str
        self._gather_address_str = None
        self._gathered_passwords = None
        self._gather_password_action = None

        if not self.Wallet.ValidatePassword(passwd):
            print("incorrect password")
            return

        keys = self.Wallet.GetKeys()
        for key in keys:
            export = key.Export()
            print("key export : %s " % export)

    def show_wallet(self, arguments):

        if not self.Wallet:
            print("please open a wallet")
            return

        item = get_arg(arguments)

        if not item:
            print("Wallet %s " % json.dumps(self.Wallet.ToJson(), indent=4))
            return

        if item in ['v', '--v', 'verbose']:
            print("Wallet %s " %
                  json.dumps(self.Wallet.ToJson(verbose=True), indent=4))
            return

        if item == 'migrate' and self.Wallet is not None:
            print("migrating wallet...")
            self.Wallet.Migrate()
            print("migrated wallet")

        if item == 'delete_addr':
            addr_to_delete = get_arg(arguments, 1)
            print("address to delete %s" % addr_to_delete)
            DeleteAddress(self, self.Wallet, addr_to_delete)

        if item == 'close':
            print('closed wallet')
            self.Wallet = None

        if item == 'rebuild':
            self.Wallet.Rebuild()
            try:
                item2 = int(get_arg(arguments, 1))
                if item2 and item2 > 0:
                    print('restarting at %s ' % item2)
                    self.Wallet._current_height = item2
            except Exception as e:
                pass
        if item == 'unspent':
            self.Wallet.FindUnspentCoins()

    def do_send(self, arguments):
        construct_and_send(self, self.Wallet, arguments)

    def show_state(self):
        height = Blockchain.Default().Height
        headers = Blockchain.Default().HeaderHeight

        diff = height - self.start_height
        now = datetime.datetime.utcnow()
        difftime = now - self.start_dt

        mins = difftime / datetime.timedelta(minutes=1)

        bpm = 0
        if diff > 0 and mins > 0:
            bpm = diff / mins

        out = 'Progress: %s / %s\n' % (height, headers)
        out += 'Block Cache length %s\n' % Blockchain.Default().BlockCacheCount
        out += 'Blocks since program start %s\n' % diff
        out += 'Time elapsed %s mins\n' % mins
        out += 'blocks per min %s \n' % bpm
        tokens = [(Token.Number, out)]
        print_tokens(tokens, self.token_style)

    def show_nodes(self):
        if len(NodeLeader.Instance().Peers) > 0:
            out = ''
            for peer in NodeLeader.Instance().Peers:
                out += 'Peer %s - IO: %s\n' % (peer.Name(), peer.IOStats())
            print_tokens([(Token.Number, out)], self.token_style)
        else:
            print('Not connected yet\n')

    def show_block(self, args):
        item = get_arg(args)
        txarg = get_arg(args, 1)
        if item is not None:
            block = Blockchain.Default().GetBlock(item)

            if block is not None:

                bjson = json.dumps(block.ToJson(), indent=4)
                tokens = [(Token.Number, bjson)]
                print_tokens(tokens, self.token_style)
                print('\n')
                if txarg and 'tx' in txarg:

                    #                    with open("b_1445025.txt",'w') as myfile:
                    #                        for tx in block.FullTransactions:
                    #                            myfile.write(json.dumps(tx.ToJson(), indent=4))
                    for tx in block.FullTransactions:
                        print(json.dumps(tx.ToJson(), indent=4))

            else:
                print("could not locate block %s" % item)
        else:
            print("please specify a block")

    def show_header(self, args):
        item = get_arg(args)
        if item is not None:
            header = Blockchain.Default().GetHeaderBy(item)
            if header is not None:
                print(json.dumps(header.ToJson(), indent=4))
            else:
                print("could not locate Header %s \n" % item)
        else:
            print("please specify a header")

    def show_tx(self, args):
        item = get_arg(args)
        if item is not None:
            try:
                tx, height = Blockchain.Default().GetTransaction(item)
                if height > -1:

                    bjson = json.dumps(tx.ToJson(), indent=4)
                    tokens = [(Token.Command, bjson)]
                    print_tokens(tokens, self.token_style)
                    print('\n')
            except Exception as e:
                print("Could not find transaction with id %s " % item)
                print(
                    "Please specify a tx hash like 'db55b4d97cf99db6826967ef4318c2993852dff3e79ec446103f141c716227f6'"
                )
        else:
            print("please specify a tx hash")

    def show_account_state(self, args):
        item = get_arg(args)

        if item is not None:
            account = Blockchain.Default().GetAccountState(
                item, print_all_accounts=True)

            if account is not None:
                bjson = json.dumps(account.ToJson(), indent=4)
                tokens = [(Token.Number, bjson)]
                print_tokens(tokens, self.token_style)
                print('\n')
            else:
                print("account %s not found" % item)
        else:
            print("please specify an account address")

    def show_asset_state(self, args):
        item = get_arg(args)

        if item is not None:

            if item == 'search':
                query = get_arg(args, 1)
                results = Blockchain.Default().SearchAssetState(query)
                print("Found %s results for %s " % (len(results), query))
                for asset in results:
                    bjson = json.dumps(asset.ToJson(), indent=4)
                    tokens = [(Token.Number, bjson)]
                    print_tokens(tokens, self.token_style)
                    print('\n')

                return

            asset = Blockchain.Default().GetAssetState(item)

            if asset is not None:
                bjson = json.dumps(asset.ToJson(), indent=4)
                tokens = [(Token.Number, bjson)]
                print_tokens(tokens, self.token_style)
                print('\n')
            else:
                print("asset %s not found" % item)
        else:
            print("please specify an asset hash")

    def show_contract_state(self, args):
        item = get_arg(args)

        if item is not None:

            if item.lower() == 'all':
                contracts = Blockchain.Default().ShowAllContracts()
                print("contracts: %s " % contracts)
            elif item.lower() == 'search':
                query = get_arg(args, 1)
                if query:

                    contracts = Blockchain.Default().SearchContracts(
                        query=query)
                    print("Found %s results for %s " % (len(contracts), query))
                    for contract in contracts:
                        bjson = json.dumps(contract.ToJson(), indent=4)
                        tokens = [(Token.Number, bjson)]
                        print_tokens(tokens, self.token_style)
                        print('\n')
                else:
                    print("Please specify a search query")
            else:
                contract = Blockchain.Default().GetContract(item)

                if contract is not None:
                    bjson = json.dumps(contract.ToJson(), indent=4)
                    tokens = [(Token.Number, bjson)]
                    print_tokens(tokens, self.token_style)
                    print('\n')
        else:
            print("please specify a contract")

    def load_smart_contract(self, args):

        if not self.Wallet:
            print("please open wallet")
            return

        function_code = LoadContract(args[1:])

        if function_code is not None:

            contract_script = GatherContractDetails(function_code, self)

            if contract_script is not None:

                tx, fee, results, num_ops = test_invoke(
                    contract_script, self.Wallet, [])

                if tx is not None and results is not None:
                    self._invoke_test_tx = tx
                    self._invoke_test_tx_fee = fee
                    print(
                        "\n-------------------------------------------------------------------------------------------------------------------------------------"
                    )
                    print("Test deploy invoke successful")
                    print("Total operations executed: %s " % num_ops)
                    print("Results %s " % [str(item) for item in results])
                    print("Deploy Invoke TX gas cost: %s " %
                          (tx.Gas.value / Fixed8.D))
                    print("Deploy Invoke TX Fee: %s " % (fee.value / Fixed8.D))
                    print(
                        "-------------------------------------------------------------------------------------------------------------------------------------\n"
                    )
                    print(
                        "You may now deploy this contract on the blockchain by using the 'invoke' command with no arguments or type 'cancel' to cancel deploy\n"
                    )
                    return
                else:
                    print("test ivoke failed")
                    print("tx is, results are %s %s " % (tx, results))
                    return

    def test_invoke_contract(self, args):
        self._invoke_test_tx = None
        self._invoke_test_tx_fee = None

        if not self.Wallet:
            print("please open a wallet")
            return

        if args and len(args) > 0:
            tx, fee, results, num_ops = TestInvokeContract(self.Wallet, args)

            if tx is not None and results is not None:
                self._invoke_test_tx = tx
                self._invoke_test_tx_fee = fee
                print(
                    "\n-------------------------------------------------------------------------------------------------------------------------------------"
                )
                print("Test invoke successful")
                print("Total operations: %s " % num_ops)
                print("Results %s " % [str(item) for item in results])
                print("Invoke TX gas cost: %s " % (tx.Gas.value / Fixed8.D))
                print("Invoke TX Fee: %s " % (fee.value / Fixed8.D))
                print(
                    "-------------------------------------------------------------------------------------------------------------------------------------\n"
                )
                print(
                    "You may now invoke this on the blockchain by using the 'invoke' command with no arguments or type 'cancel' to cancel invoke\n"
                )
                return
            else:
                print("Error testing contract invoke")
                return

        print("please specify a contract to invoke")

    def invoke_contract(self, args):

        if not self._invoke_test_tx:
            print(
                "Please test your invoke before deploying it with the 'testinvoke {contracthash} *args' command"
            )
            return

        result = InvokeContract(self.Wallet, self._invoke_test_tx,
                                self._invoke_test_tx_fee)

        self._invoke_test_tx = None
        self._invoke_test_tx_fee = None
        return

    def cancel_operations(self):
        self._invoke_test_tx = None
        print("Operation cancelled")
        return

    def show_mem(self):
        total = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
        totalmb = total / 1000000
        out = "Total: %s MB\n" % totalmb
        out += "total buffers %s\n" % StreamManager.TotalBuffers()
        print_tokens([(Token.Number, out)], self.token_style)

    def configure(self, args):
        what = get_arg(args)

        if what == 'log' or what == 'logs':
            c1 = get_arg(args, 1).lower()
            if c1 is not None:
                if c1 == 'on' or c1 == '1':
                    print("turning on logging")
                    logger = logging.getLogger()
                    logger.setLevel(logging.DEBUG)
                if c1 == 'off' or c1 == '0':
                    print("turning off logging")
                    logger = logging.getLogger()
                    logger.setLevel(logging.ERROR)

            else:
                print("cannot configure log.  Please specify on or off")
        else:
            print("cannot configure %s " % what)
            print("Try 'config log on/off'")

    def parse_result(self, result):
        if len(result):
            commandParts = [s for s in result.split()]
            return commandParts[0], commandParts[1:]
        return None, None

    def run(self):

        dbloop = task.LoopingCall(Blockchain.Default().PersistBlocks)
        dbloop.start(.1)

        Blockchain.Default().PersistBlocks()

        tokens = [(Token.Neo, 'NEO'), (Token.Default, ' cli. Type '),
                  (Token.Command, "'help' "),
                  (Token.Default, 'to get started')]
        print_tokens(tokens, self.token_style)
        print("\n")

        while self.go_on:

            if self._gathered_passwords and len(
                    self._gathered_passwords) == self._num_passwords_req:
                self._gathering_password = False
                self._gather_password_action()

            if self._gathering_password:
                result = prompt("password> ", is_password=True)

            else:
                result = prompt(
                    "neo> ",
                    completer=self.completer,
                    history=self.history,
                    get_bottom_toolbar_tokens=self.get_bottom_toolbar,
                    style=self.token_style)

            if self._gathering_password:
                self._gathered_passwords.append(result)

            else:

                try:
                    command, arguments = self.parse_result(result)

                    if command is not None and len(command) > 0:
                        command = command.lower()

                        if command == 'quit' or command == 'exit':
                            self.quit()
                        elif command == 'help':
                            self.help()
                        elif command == 'create':
                            self.do_create(arguments)
                        elif command == 'open':
                            self.do_open(arguments)
                        elif command == 'import':
                            self.do_import(arguments)
                        elif command == 'export':
                            self.do_export(arguments)
                        elif command == 'wallet':
                            self.show_wallet(arguments)
                        elif command == 'send':
                            self.do_send(arguments)
                        elif command == 'block':
                            self.show_block(arguments)
                        elif command == 'tx':
                            self.show_tx(arguments)
                        elif command == 'header':
                            self.show_header(arguments)
                        elif command == 'account':
                            self.show_account_state(arguments)
                        elif command == 'asset':
                            self.show_asset_state(arguments)
                        elif command == 'contract':
                            self.show_contract_state(arguments)
                        elif command == 'invoke':
                            self.invoke_contract(arguments)
                        elif command == 'testinvoke':
                            self.test_invoke_contract(arguments)
                        elif command == 'cancel':
                            self.cancel_operations()
                        elif command == 'mem':
                            self.show_mem()
                        elif command == 'nodes' or command == 'node':
                            self.show_nodes()
                        elif command == 'state':
                            self.show_state()
                        elif command == 'config':
                            self.configure(arguments)
                        elif command == None:
                            print('please specify a command')
                        else:
                            print("command %s not found" % command)

                except Exception as e:

                    print("could not execute command: %s " % e)
                    traceback.print_stack()
                    traceback.print_exc()
Example #28
0
def _history():
    h = InMemoryHistory()
    h.append_string("line1 first input")
    h.append_string("line2 second input")
    h.append_string("line3 third input")
    return h
Example #29
0
    def __init__(
            self,
            get_globals=None,
            get_locals=None,
            history_filename=None,
            vi_mode=False,
            input=None,
            output=None,
            color_depth=None,

            # For internal use.
            extra_key_bindings=None,
            _completer=None,
            _validator=None,
            _lexer=None,
            _extra_buffer_processors=None,
            _extra_layout_body=None,
            _extra_toolbars=None,
            _input_buffer_height=None):

        self.get_globals = get_globals or (lambda: {})
        self.get_locals = get_locals or self.get_globals

        self._completer = _completer or FuzzyCompleter(
            PythonCompleter(self.get_globals, self.get_locals,
                            lambda: self.enable_dictionary_completion),
            enable_fuzzy=Condition(lambda: self.enable_fuzzy_completion))
        self._validator = _validator or PythonValidator(
            self.get_compiler_flags)
        self._lexer = _lexer or PygmentsLexer(PythonLexer)

        if history_filename:
            self.history = ThreadedHistory(FileHistory(history_filename))
        else:
            self.history = InMemoryHistory()

        self._input_buffer_height = _input_buffer_height
        self._extra_layout_body = _extra_layout_body or []
        self._extra_toolbars = _extra_toolbars or []
        self._extra_buffer_processors = _extra_buffer_processors or []

        self.extra_key_bindings = extra_key_bindings or KeyBindings()

        # Settings.
        self.show_signature = False
        self.show_docstring = False
        self.show_meta_enter_message = True
        self.completion_visualisation = CompletionVisualisation.MULTI_COLUMN
        self.completion_menu_scroll_offset = 1

        self.show_line_numbers = False
        self.show_status_bar = True
        self.wrap_lines = True
        self.complete_while_typing = True
        self.paste_mode = False  # When True, don't insert whitespace after newline.
        self.confirm_exit = True  # Ask for confirmation when Control-D is pressed.
        self.accept_input_on_enter = 2  # Accept when pressing Enter 'n' times.
        # 'None' means that meta-enter is always required.
        self.enable_open_in_editor = True
        self.enable_system_bindings = True
        self.enable_input_validation = True
        self.enable_auto_suggest = False
        self.enable_mouse_support = False
        self.enable_history_search = False  # When True, like readline, going
        # back in history will filter the
        # history on the records starting
        # with the current input.

        self.enable_syntax_highlighting = True
        self.enable_fuzzy_completion = False
        self.enable_dictionary_completion = False
        self.swap_light_and_dark = False
        self.highlight_matching_parenthesis = False
        self.show_sidebar = False  # Currently show the sidebar.
        self.show_sidebar_help = True  # When the sidebar is visible, also show the help text.
        self.show_exit_confirmation = False  # Currently show 'Do you really want to exit?'
        self.terminal_title = None  # The title to be displayed in the terminal. (None or string.)
        self.exit_message = 'Do you really want to exit?'
        self.insert_blank_line_after_output = True  # (For the REPL.)

        # The buffers.
        self.default_buffer = self._create_buffer()
        self.search_buffer = Buffer()
        self.docstring_buffer = Buffer(read_only=True)

        # Tokens to be shown at the prompt.
        self.prompt_style = 'classic'  # The currently active style.

        self.all_prompt_styles = {  # Styles selectable from the menu.
            'ipython': IPythonPrompt(self),
            'classic': ClassicPrompt(),
        }

        self.get_input_prompt = lambda: \
            self.all_prompt_styles[self.prompt_style].in_prompt()

        self.get_output_prompt = lambda: \
            self.all_prompt_styles[self.prompt_style].out_prompt()

        #: Load styles.
        self.code_styles = get_all_code_styles()
        self.ui_styles = get_all_ui_styles()
        self._current_code_style_name = 'default'
        self._current_ui_style_name = 'default'

        if is_windows():
            self._current_code_style_name = 'win32'

        self._current_style = self._generate_style()
        self.color_depth = color_depth or ColorDepth.default()

        self.max_brightness = 1.0
        self.min_brightness = 0.0

        # Options to be configurable from the sidebar.
        self.options = self._create_options()
        self.selected_option_index = 0

        #: Incremeting integer counting the current statement.
        self.current_statement_index = 1

        # Code signatures. (This is set asynchronously after a timeout.)
        self.signatures = []

        # Boolean indicating whether we have a signatures thread running.
        # (Never run more than one at the same time.)
        self._get_signatures_thread_running = False

        self.output = output or create_output()
        self.input = input or create_input(sys.stdin)

        self.style_transformation = merge_style_transformations([
            ConditionalStyleTransformation(
                SwapLightAndDarkStyleTransformation(),
                filter=Condition(lambda: self.swap_light_and_dark)),
            AdjustBrightnessStyleTransformation(lambda: self.min_brightness,
                                                lambda: self.max_brightness),
        ])
        self.ptpython_layout = PtPythonLayout(
            self,
            lexer=DynamicLexer(lambda: self._lexer if self.
                               enable_syntax_highlighting else SimpleLexer()),
            input_buffer_height=self._input_buffer_height,
            extra_buffer_processors=self._extra_buffer_processors,
            extra_body=self._extra_layout_body,
            extra_toolbars=self._extra_toolbars)

        self.app = self._create_application()

        if vi_mode:
            self.app.editing_mode = EditingMode.VI
Example #30
0
class AzureShell(object):
    def __init__(self, config, completer):
        self._cli = None
        self._env = os.environ.copy()
        self.history = InMemoryHistory()
        self.file_history = FileHistory("{}/history".format(
            AzureShellCache.Instance().get('base_dir')))
        self._config = config
        self.completer = completer

    def run(self):
        while True:
            try:
                document = self.get_cli().run(reset_current_buffer=True)
                text = document.text
            except InputInterrupt:
                pass
            except (KeyboardInterrupt, EOFError):
                logger.debug(
                    "breaking loop due to KeyboardInterrupt or EOFError")
                break
            else:
                if text.startswith('exit') or text.startswith('quit'):
                    sys.exit(0)

                if text.startswith('az'):
                    full_cmd = text
                    self.history.append(full_cmd)
                else:
                    full_cmd = text[0:]

                logger.debug("Execute subprocess command:{}".format(full_cmd))
                self.get_cli().request_redraw()
                p = subprocess.Popen(full_cmd, shell=True, env=self._env)
                p.communicate()

    def on_input_timeout(self, cli):
        document = cli.current_buffer.document
        text = document.text
        logger.debug("on_input_timeout document:{}".format(document))
        # Add 'az' to current buffer if no text typed in
        #if not document.text:
        #    cli.current_buffer.document = Document(u'az', 2)
        cli.request_redraw()

    def get_cli(self):
        if self._cli is None:
            self._cli = self.create_cli()
        return self._cli

    def create_cli(self):
        ## KeyBindings configuration
        key_binding = KeyBindingManager(enable_search=True,
                                        enable_abort_and_exit_bindings=True,
                                        enable_system_bindings=True,
                                        enable_auto_suggest_bindings=True,
                                        enable_open_in_editor=False)

        ## Buffer configuration
        default_buffer = Buffer(history=self.file_history,
                                auto_suggest=AutoSuggestFromHistory(),
                                enable_history_search=True,
                                completer=self.completer,
                                complete_while_typing=Always(),
                                accept_action=AcceptAction.RETURN_DOCUMENT)

        ## Style configuration
        try:
            style = get_style_by_name(self._config.highlighter_style)
        except ClassNotFound:
            style = get_style_by_name('native')

        styles = {}
        styles.update(style.styles)
        styles.update(default_style_extensions)
        styles.update({
            Token.Menu.Completions.Completion: 'bg:#003fff #ffffff',
            Token.Menu.Completions.Completion.Current: 'bg:#5ab300 #000000',
            Token.Menu.Completions.Meta.Current: 'bg:#5ab300 #ffffff',
            Token.Menu.Completions.Meta: 'bg:#ffffff #000000',
            Token.Scrollbar: 'bg:#003fff',
            Token.Scrollbar.Button: 'bg:#003333',
        })
        prompt_style = style_from_dict(styles)

        ## Application
        application = Application(
            layout=self.create_cli_layout(),
            mouse_support=False,
            style=prompt_style,
            buffer=default_buffer,
            on_abort=AbortAction.RETRY,
            on_exit=AbortAction.RAISE_EXCEPTION,
            on_input_timeout=self.on_input_timeout,
            key_bindings_registry=key_binding.registry,
        )

        cli = CommandLineInterface(application=application,
                                   eventloop=create_eventloop())
        return cli

    def create_cli_layout(self):
        from .lexer import AzureShellLexer
        lexer = AzureShellLexer
        return create_default_layout(
            message=u'azure> ',
            reserve_space_for_menu=8,
            lexer=lexer,
            extra_input_processors=[
                ConditionalProcessor(
                    processor=HighlightMatchingBracketProcessor(
                        chars='[](){}'),
                    filter=HasFocus(DEFAULT_BUFFER) & ~IsDone())
            ])
Example #31
0
    def do_loop(self):  # use console facilities
        history = InMemoryHistory()
        gh_completer_client = WordCompleter([
            'show', 'ident', 'peek', 'clear', 'reset', 'set', 'recv', 'csend',
            'dsend'
        ],
                                            ignore_case=True,
                                            match_middle=True)

        while True:
            result = None
            try:
                result = prompt(
                    completer=gh_completer_client,
                    style=AgentStyle,
                    vi_mode=True,
                    enable_history_search=True,
                    reserve_space_for_menu=4,
                    complete_while_typing=True,
                    display_completions_in_columns=True,
                    get_rprompt_tokens=self._get_rprompt_tokens,
                    wrap_lines=True,
                    get_prompt_tokens=self._get_prompt_tokens,
                    get_bottom_toolbar_tokens=self._get_bottom_toolbar_tokens,
                    enable_system_bindings=True,
                    get_title=self._get_title,
                    history=history,
                    auto_suggest=AutoSuggestFromHistory(),
                    patch_stdout=True)
            except KeyboardInterrupt:
                self.fx.flogger.warning("^D to exit")
            except EOFError:
                return

            if not result:
                pass
            else:
                cmdargs = ""
                tokens = result.split(' ')

                if len(tokens) > 0:
                    cmd = tokens[0]  # get command
                    if cmd == 'clear':
                        clear()
                    elif cmd == 'help':
                        print("""
                              System: Alt-!
                              Exit: Ctlr-D
                              Skip: Ctrl-C
                              Search: Vi mode standard
                              """)

                    elif cmd == 'ident':
                        self.do_ident()

                    elif cmd == 'show':
                        self.do_show()

                    elif cmd == 'peek':
                        self.do_peek()

                    elif cmd == 'reset':
                        self.do_reset()

                    elif cmd == 'recv':
                        if len(tokens) == 2:
                            comm = tokens[1]
                            self.do_recv(comm)
                        else:
                            self.do_recv()

                    elif cmd == 'csend':
                        if len(tokens) > 1:
                            self.do_csend(tokens[1:])
                        else:
                            print("Need commands")

                    elif cmd == 'dsend':
                        if len(tokens) > 1:
                            self.do_dsend(tokens[1])
                        else:
                            print("Need path to data file")

                    elif cmd == 'set':
                        self.do_set(result)
                    else:
                        print("Unsupported Command")
                else:
                    print("Invalid Command")
Example #32
0
import click
import numpy as np  # noqa
import pandas as pd
from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.validation import ValidationError, Validator

from kartothek.core.cube.conditions import Conjunction
from kartothek.io.dask.bag_cube import query_cube_bag
from kartothek.io_components.metapartition import SINGLE_TABLE
from kartothek.utils.ktk_adapters import get_dataset_columns

__all__ = ("query", )

_history_conditions = InMemoryHistory()
_history_payload = InMemoryHistory()


@click.pass_context
def query(ctx):
    """
    Interactive cube queries into IPython.
    """
    cube = ctx.obj["cube"]
    datasets = ctx.obj["datasets"]
    store = ctx.obj["store"]

    store_instance = store()

    datasets = {
Example #33
0
    def __init__(
            self,
            get_globals=None,
            get_locals=None,
            history_filename=None,
            vi_mode=False,

            # For internal use.
            _completer=None,
            _validator=None,
            _lexer=None,
            _extra_buffers=None,
            _extra_buffer_processors=None,
            _on_start=None,
            _extra_layout_body=None,
            _extra_toolbars=None,
            _input_buffer_height=None,
            _accept_action=AcceptAction.RETURN_DOCUMENT,
            _on_exit=AbortAction.RAISE_EXCEPTION):

        self.get_globals = get_globals or (lambda: {})
        self.get_locals = get_locals or self.get_globals

        self._completer = _completer or PythonCompleter(
            self.get_globals, self.get_locals)
        self._validator = _validator or PythonValidator(
            self.get_compiler_flags)
        self.history = FileHistory(
            history_filename) if history_filename else InMemoryHistory()
        self._lexer = _lexer or PygmentsLexer(PythonLexer)
        self._extra_buffers = _extra_buffers
        self._accept_action = _accept_action
        self._on_exit = _on_exit
        self._on_start = _on_start

        self._input_buffer_height = _input_buffer_height
        self._extra_layout_body = _extra_layout_body or []
        self._extra_toolbars = _extra_toolbars or []
        self._extra_buffer_processors = _extra_buffer_processors or []

        # Settings.
        self.show_signature = False
        self.show_docstring = False
        self.show_meta_enter_message = True
        self.completion_visualisation = CompletionVisualisation.MULTI_COLUMN
        self.completion_menu_scroll_offset = 1

        self.show_line_numbers = False
        self.show_status_bar = True
        self.wrap_lines = True
        self.complete_while_typing = True
        self.vi_mode = vi_mode
        self.paste_mode = False  # When True, don't insert whitespace after newline.
        self.confirm_exit = True  # Ask for confirmation when Control-D is pressed.
        self.accept_input_on_enter = 2  # Accept when pressing Enter 'n' times.
        # 'None' means that meta-enter is always required.
        self.enable_open_in_editor = True
        self.enable_system_bindings = True
        self.enable_input_validation = True
        self.enable_auto_suggest = False
        self.enable_mouse_support = False
        self.enable_history_search = False  # When True, like readline, going
        # back in history will filter the
        # history on the records starting
        # with the current input.

        self.highlight_matching_parenthesis = False
        self.show_sidebar = False  # Currently show the sidebar.
        self.show_sidebar_help = True  # When the sidebar is visible, also show the help text.
        self.show_exit_confirmation = False  # Currently show 'Do you really want to exit?'
        self.terminal_title = None  # The title to be displayed in the terminal. (None or string.)
        self.exit_message = 'Do you really want to exit?'
        self.insert_blank_line_after_output = True  # (For the REPL.)

        # Tokens to be shown at the prompt.
        self.prompt_style = 'classic'  # The currently active style.

        self.all_prompt_styles = {  # Styles selectable from the menu.
            'ipython': IPythonPrompt(self),
            'classic': ClassicPrompt(),
        }

        self.get_input_prompt_tokens = lambda cli: \
            self.all_prompt_styles[self.prompt_style].in_tokens(cli)

        self.get_output_prompt_tokens = lambda cli: \
            self.all_prompt_styles[self.prompt_style].out_tokens(cli)

        #: Load styles.
        self.code_styles = get_all_code_styles()
        self.ui_styles = get_all_ui_styles()
        self._current_code_style_name = 'default'
        self._current_ui_style_name = 'default'

        if is_windows():
            self._current_code_style_name = 'win32'

        self._current_style = self._generate_style()
        self.true_color = False

        # Options to be configurable from the sidebar.
        self.options = self._create_options()
        self.selected_option_index = 0

        #: Incremeting integer counting the current statement.
        self.current_statement_index = 1

        # Code signatures. (This is set asynchronously after a timeout.)
        self.signatures = []

        # Create a Registry for the key bindings.
        self.key_bindings_registry = MergedRegistry([
            ConditionalRegistry(
                registry=load_key_bindings_for_prompt(
                    enable_abort_and_exit_bindings=True,
                    enable_search=True,
                    enable_open_in_editor=Condition(
                        lambda cli: self.enable_open_in_editor),
                    enable_system_bindings=Condition(
                        lambda cli: self.enable_system_bindings),
                    enable_auto_suggest_bindings=Condition(
                        lambda cli: self.enable_auto_suggest)),

                # Disable all default key bindings when the sidebar or the exit confirmation
                # are shown.
                filter=Condition(lambda cli: not (self.show_sidebar or self.
                                                  show_exit_confirmation))),
            load_mouse_bindings(),
            load_python_bindings(self),
            load_sidebar_bindings(self),
            load_confirm_exit_bindings(self),
        ])

        # Boolean indicating whether we have a signatures thread running.
        # (Never run more than one at the same time.)
        self._get_signatures_thread_running = False
Example #34
0
 def console(self):
     hist = InMemoryHistory()
     suggest = AutoSuggestFromHistory()
     while True:
         gdb_cmd = prompt(u'> ', history=hist, auto_suggest=suggest)
         self.handle_cmd(gdb_cmd.encode('ascii'))
def _history():
    " Prefilled history. "
    history = InMemoryHistory()
    history.append_string('alpha beta gamma delta')
    history.append_string('one two three four')
    return history
Example #36
0
def _history():
    h = InMemoryHistory()
    h.append('line1 first input')
    h.append('line2 second input')
    h.append('line3 third input')
    return h
    def __configure_layout(self):
        # Configure completers used by the application.
        commands_completer = NestedCommandsFuzzyWordCompleter(
            self.commands, completers=self.completers)

        # Configure PythonRuntimeCompleter
        python_runtime_completer = PythonRuntimeCompleter(self)

        # --------- This is the CLI input container ---------
        # Comfigure the input container and handler.
        self.input_container = TextArea(prompt='{}> '.format(self.app_name),
                                        style='class:input-field',
                                        multiline=False,
                                        wrap_lines=True,
                                        completer=commands_completer,
                                        history=InMemoryHistory())
        self.input_container.accept_handler = lambda command: self.__root_command_handler(
            self.input_container.text)

        # Configure the python buffer to write interactive pytho code.
        self.python_code_buffer = Buffer(completer=python_runtime_completer)

        # --------- This is the Python code container ---------
        self.python_code_container = Window(
            width=130,  # Is there any way to use precentage?
            content=BufferControl(buffer=self.python_code_buffer,
                                  lexer=PythonLexer(self.python_code_buffer)),
            get_line_prefix=self.__get_line_prefix,
            wrap_lines=True)

        # Configure the output buffer to 'print' out results.
        self.output_buffer = Buffer()

        # --------- This is the Output container ---------
        self.output_container = Window(
            content=BufferControl(buffer=self.output_buffer), wrap_lines=True)

        self.session = Session()
        self.session.add_tab(Tab("Console", self.output_container))
        self.session.add_tab(
            Tab("Python Interpreter Environment", self.python_code_container))

        self.tabs_container = TabbedBuffersContainer(self.session)
        self.tabs_container.set_selected_tab(0)

        # Configure the application layout.
        root_container = HSplit([
            VSplit([
                # Window for python code.
                self.tabs_container,
            ]),
            # Seperation line.
            Window(height=1, char='-', style='class:line'),
            # Command line prompt.
            self.input_container,
        ])

        self.floating_container = FloatContainer(
            content=root_container,
            floats=[
                Float(xcursor=True,
                      ycursor=True,
                      content=CompletionsMenu(max_height=16, scroll_offset=1))
            ])

        self.body_layout = Layout(self.floating_container)
Example #38
0
    def init_prompt_toolkit_cli(self):
        if 'JUPYTER_CONSOLE_TEST' in os.environ:
            # Simple restricted interface for tests so we can find prompts with
            # pexpect. Multi-line input not supported.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            self.print_out_prompt = \
                lambda: print('Out[%d]: ' % self.execution_count, end='')
            return

        kbmanager = KeyBindingManager.for_prompt()
        insert_mode = ViInsertMode() | EmacsInsertMode()
        # Ctrl+J == Enter, seemingly
        @kbmanager.registry.add_binding(Keys.ControlJ,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                   ))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >= d.line_count
                                           - d.empty_line_count_at_the_end()):
                b.newline()
                return

            more, indent = self.check_complete(d.text)

            if (not more) and b.accept_action.is_returnable:
                b.accept_action.validate_and_handle(event.cli, b)
            else:
                b.insert_text('\n' + indent)

        @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER))
        def _(event):
            event.current_buffer.reset()

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for _, _, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)

        style_overrides = {
            Token.Prompt: '#009900',
            Token.PromptNum: '#00ff00 bold',
            Token.OutPrompt: '#ff2200',
            Token.OutPromptNum: '#ff0000 bold',
        }
        if self.highlighting_style:
            style_cls = get_style_by_name(self.highlighting_style)
        else:
            style_cls = get_style_by_name('default')
            # The default theme needs to be visible on both a dark background
            # and a light background, because we can't tell what the terminal
            # looks like. These tweaks to the default theme help with that.
            style_overrides.update({
                Token.Number: '#007700',
                Token.Operator: 'noinherit',
                Token.String: '#BB6622',
                Token.Name.Function: '#2080D0',
                Token.Name.Class: 'bold #2080D0',
                Token.Name.Namespace: 'bold #2080D0',
            })
        style_overrides.update(self.highlighting_style_overrides)
        style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
                                            style_dict=style_overrides)

        editing_mode = getattr(EditingMode, self.editing_mode.upper())
        langinfo = self.kernel_info.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', 'text'))
        app = create_prompt_application(multiline=True,
                            editing_mode=editing_mode,
                            lexer=PygmentsLexer(get_pygments_lexer(lexer)),
                            get_prompt_tokens=self.get_prompt_tokens,
                            get_continuation_tokens=self.get_continuation_tokens,
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=JupyterPTCompleter(self.Completer),
                            enable_history_search=True,
                            style=style,
        )

        self._eventloop = create_eventloop()
        self.pt_cli = CommandLineInterface(app,
                            eventloop=self._eventloop,
                            output=create_output(true_color=self.true_color),
        )
def _history():
    h = InMemoryHistory()
    h.append_string('line1 first input')
    h.append_string('line2 second input')
    h.append_string('line3 third input')
    return h
Example #40
0
    def __init__(self,
                 message: AnyFormattedText = '',
                 multiline: FilterOrBool = False,
                 wrap_lines: FilterOrBool = True,
                 is_password: FilterOrBool = False,
                 vi_mode: bool = False,
                 editing_mode: EditingMode = EditingMode.EMACS,
                 complete_while_typing: FilterOrBool = True,
                 validate_while_typing: FilterOrBool = True,
                 enable_history_search: FilterOrBool = False,
                 search_ignore_case: FilterOrBool = False,
                 lexer: Optional[Lexer] = None,
                 enable_system_prompt: FilterOrBool = False,
                 enable_suspend: FilterOrBool = False,
                 enable_open_in_editor: FilterOrBool = False,
                 validator: Optional[Validator] = None,
                 completer: Optional[Completer] = None,
                 complete_in_thread: bool = False,
                 reserve_space_for_menu: int = 8,
                 complete_style: CompleteStyle = CompleteStyle.COLUMN,
                 auto_suggest: Optional[AutoSuggest] = None,
                 style: Optional[BaseStyle] = None,
                 style_transformation: Optional[StyleTransformation] = None,
                 swap_light_and_dark_colors: FilterOrBool = False,
                 color_depth: Optional[ColorDepth] = None,
                 include_default_pygments_style: FilterOrBool = True,
                 history: Optional[History] = None,
                 clipboard: Optional[Clipboard] = None,
                 prompt_continuation: PromptContinuationText = '',
                 rprompt: AnyFormattedText = None,
                 bottom_toolbar: AnyFormattedText = None,
                 mouse_support: FilterOrBool = False,
                 input_processors: Optional[List[Processor]] = None,
                 key_bindings: Optional[KeyBindingsBase] = None,
                 erase_when_done: bool = False,
                 tempfile_suffix: str = '.txt',
                 refresh_interval: float = 0,
                 input: Optional[Input] = None,
                 output: Optional[Output] = None) -> None:

        history = history or InMemoryHistory()
        clipboard = clipboard or InMemoryClipboard()

        # Ensure backwards-compatibility, when `vi_mode` is passed.
        if vi_mode:
            editing_mode = EditingMode.VI

        # Store all settings in this class.
        self.input = input
        self.output = output

        # Store all settings in this class.

        # Store attributes.
        # (All except 'editing_mode'.)
        self.message = message
        self.lexer = lexer
        self.completer = completer
        self.complete_in_thread = complete_in_thread
        self.is_password = is_password
        self.key_bindings = key_bindings
        self.bottom_toolbar = bottom_toolbar
        self.style = style
        self.style_transformation = style_transformation
        self.swap_light_and_dark_colors = swap_light_and_dark_colors
        self.color_depth = color_depth
        self.include_default_pygments_style = include_default_pygments_style
        self.rprompt = rprompt
        self.multiline = multiline
        self.prompt_continuation = prompt_continuation
        self.wrap_lines = wrap_lines
        self.enable_history_search = enable_history_search
        self.search_ignore_case = search_ignore_case
        self.complete_while_typing = complete_while_typing
        self.validate_while_typing = validate_while_typing
        self.complete_style = complete_style
        self.mouse_support = mouse_support
        self.auto_suggest = auto_suggest
        self.clipboard = clipboard
        self.validator = validator
        self.refresh_interval = refresh_interval
        self.input_processors = input_processors
        self.enable_system_prompt = enable_system_prompt
        self.enable_suspend = enable_suspend
        self.enable_open_in_editor = enable_open_in_editor
        self.reserve_space_for_menu = reserve_space_for_menu
        self.tempfile_suffix = tempfile_suffix

        # Create buffers, layout and Application.
        self.history = history
        self.default_buffer = self._create_default_buffer()
        self.search_buffer = self._create_search_buffer()
        self.layout = self._create_layout()
        self.app = self._create_application(editing_mode, erase_when_done)
Example #41
0
    def init_prompt_toolkit_cli(self):
        if not sys.stdin.isatty():
            # Piped input - e.g. for tests. Fall back to plain non-interactive
            # output. This is very limited, and only accepts a single line.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            return

        kbmanager = KeyBindingManager.for_prompt(enable_vi_mode=self.vi_mode)
        insert_mode = ViStateFilter(kbmanager.get_vi_state, InputMode.INSERT)
        # Ctrl+J == Enter, seemingly
        @kbmanager.registry.add_binding(Keys.ControlJ,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                   ))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >= d.line_count
                                           - d.empty_line_count_at_the_end()):
                b.newline()
                return

            status, indent = self.input_splitter.check_complete(d.text)

            if (status != 'incomplete') and b.accept_action.is_returnable:
                b.accept_action.validate_and_handle(event.cli, b)
            else:
                b.insert_text('\n' + (' ' * (indent or 0)))

        @kbmanager.registry.add_binding(Keys.ControlC)
        def _(event):
            event.current_buffer.reset()

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for _, _, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)

        style_overrides = {
            Token.Prompt: '#009900',
            Token.PromptNum: '#00ff00 bold',
        }
        if self.highlighting_style:
            style_cls = get_style_by_name(self.highlighting_style)
        else:
            style_cls = get_style_by_name('default')
            # The default theme needs to be visible on both a dark background
            # and a light background, because we can't tell what the terminal
            # looks like. These tweaks to the default theme help with that.
            style_overrides.update({
                Token.Number: '#007700',
                Token.Operator: 'noinherit',
                Token.String: '#BB6622',
                Token.Name.Function: '#2080D0',
                Token.Name.Class: 'bold #2080D0',
                Token.Name.Namespace: 'bold #2080D0',
            })
        style_overrides.update(self.highlighting_style_overrides)
        style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
                                            style_dict=style_overrides)

        app = create_prompt_application(multiline=True,
                            lexer=PygmentsLexer(Python3Lexer if PY3 else PythonLexer),
                            get_prompt_tokens=self.get_prompt_tokens,
                            get_continuation_tokens=self.get_continuation_tokens,
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=IPythonPTCompleter(self.Completer),
                            enable_history_search=True,
                            style=style,
                            mouse_support=self.mouse_support,
        )

        self.pt_cli = CommandLineInterface(app,
                           eventloop=create_eventloop(self.inputhook))
Example #42
0
 def __init__(self, connector):
     self.connector = connector
     self.history = InMemoryHistory()
     self.completer = SQLDynamicCompleter()
Example #43
0
    def init_prompt_toolkit_cli(self):
        if self.simple_prompt or ('JUPYTER_CONSOLE_TEST' in os.environ):
            # Simple restricted interface for tests so we can find prompts with
            # pexpect. Multi-line input not supported.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            self.print_out_prompt = \
                lambda: print('Out[%d]: ' % self.execution_count, end='')
            return

        kb = KeyBindings()
        insert_mode = vi_insert_mode | emacs_insert_mode

        @kb.add("enter", filter=(has_focus(DEFAULT_BUFFER)
                                 & ~has_selection
                                 & insert_mode
                                 ))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >= d.line_count
                                           - d.empty_line_count_at_the_end()):
                b.newline()
                return

            # Pressing enter flushes any pending display. This also ensures
            # the displayed execution_count is correct.
            self.handle_iopub()

            more, indent = self.check_complete(d.text)

            if (not more) and b.accept_handler:
                b.validate_and_handle()
            else:
                b.insert_text('\n' + indent)

        @kb.add("c-c", filter=has_focus(DEFAULT_BUFFER))
        def _(event):
            event.current_buffer.reset()

        @kb.add("c-\\", filter=has_focus(DEFAULT_BUFFER))
        def _(event):
            raise EOFError

        @kb.add("c-z", filter=Condition(lambda: suspend_to_background_supported()))
        def _(event):
            event.cli.suspend_to_background()

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for _, _, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cast_unicode_py2(cell.rstrip())
            if cell and (cell != last_cell):
                history.append_string(cell)

        style_overrides = {
            Token.Prompt: '#009900',
            Token.PromptNum: '#00ff00 bold',
            Token.OutPrompt: '#ff2200',
            Token.OutPromptNum: '#ff0000 bold',
        }
        if self.highlighting_style:
            style_cls = get_style_by_name(self.highlighting_style)
        else:
            style_cls = get_style_by_name('default')
            # The default theme needs to be visible on both a dark background
            # and a light background, because we can't tell what the terminal
            # looks like. These tweaks to the default theme help with that.
            style_overrides.update({
                Token.Number: '#007700',
                Token.Operator: 'noinherit',
                Token.String: '#BB6622',
                Token.Name.Function: '#2080D0',
                Token.Name.Class: 'bold #2080D0',
                Token.Name.Namespace: 'bold #2080D0',
            })
        style_overrides.update(self.highlighting_style_overrides)
        style = merge_styles([
            style_from_pygments_cls(style_cls),
            style_from_pygments_dict(style_overrides),
        ])

        editing_mode = getattr(EditingMode, self.editing_mode.upper())
        langinfo = self.kernel_info.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', 'text'))

        # If enabled in the settings, highlight matching brackets
        # when the DEFAULT_BUFFER has the focus
        input_processors = [ConditionalProcessor(
            processor=HighlightMatchingBracketProcessor(chars='[](){}'),
            filter=has_focus(DEFAULT_BUFFER) & ~is_done &
            Condition(lambda: self.highlight_matching_brackets))
        ]

        self.pt_cli = PromptSession(
            message=(lambda: PygmentsTokens(self.get_prompt_tokens())),
            multiline=True,
            editing_mode=editing_mode,
            lexer=PygmentsLexer(get_pygments_lexer(lexer)),
            prompt_continuation=(
                lambda width, lineno, is_soft_wrap:
                PygmentsTokens(self.get_continuation_tokens(width))
            ),
            key_bindings=kb,
            history=history,
            completer=JupyterPTCompleter(self.Completer),
            enable_history_search=True,
            style=style,
            input_processors=input_processors,
            color_depth=(ColorDepth.TRUE_COLOR if self.true_color else None),

        )
Example #44
0
    def main(self):
        if empire_config.yaml.get('suppress-self-cert-warning', True):
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        # Create some history first. (Easy for testing.)
        history = InMemoryHistory()
        history.append_string("help")
        history.append_string('uselistener http')
        history.append_string('listeners')
        history.append_string("main")
        history.append_string("connect -c localhost")

        print_util.loading()
        print("\n")
        print("Use the 'connect' command to connect to your Empire server.")
        print(
            "'connect -c localhost' will connect to a local empire instance with all the defaults"
        )
        print("including the default username and password.")

        session = PromptSession(
            key_bindings=bindings,
            history=history,
            completer=self.completer,
            complete_in_thread=True,
            bottom_toolbar=self.bottom_toolbar,
        )
        t = threading.Thread(target=self.update_in_bg, args=[session])
        t.daemon = True
        t.start()

        menu_state.push(main_menu)

        autoserver = self.get_autoconnect_server()
        if autoserver:
            print(
                print_util.color(
                    f'[*] Attempting to connect to server: {autoserver}'))
            self.menus['MainMenu'].connect(autoserver, config=True)

        if args.resource:
            with open(args.resource) as resource_file:
                print(
                    print_util.color(
                        f"[*] Executing Resource File: {args.resource}"))
                for cmd in resource_file:
                    with patch_stdout(raw=True):
                        try:
                            time.sleep(1)
                            text = session.prompt(accept_default=True,
                                                  default=cmd.strip())
                            cmd_line = list(shlex.split(text))
                            self.parse_command_line(text,
                                                    cmd_line,
                                                    resource_file=True)
                        except CliExitException:
                            return
                        except Exception as e:
                            print(
                                print_util.color(
                                    f'[*] Error parsing resource command: ',
                                    text))

        while True:
            try:
                with patch_stdout(raw=True):
                    text = session.prompt(HTML(
                        menu_state.current_menu.get_prompt()),
                                          refresh_interval=None)
                    # cmd_line = list(map(lambda s: s.lower(), shlex.split(text)))
                    # TODO what to do about case sensitivity for parsing options.
                    cmd_line = list(shlex.split(text))
                    self.parse_command_line(text, cmd_line)
            except KeyboardInterrupt:
                print(print_util.color("[!] Type exit to quit"))
            except ValueError as e:
                print(print_util.color(f'[!] Error processing command: {e}'))
            except EOFError:
                break  # Control-D pressed.
            except CliExitException:
                break
Example #45
0
def cli(spec, url, http_options):
    click.echo('Version: %s' % __version__)

    copied, config_path = config.initialize()
    if copied:
        click.echo('Config file not found. Initialized a new one: %s' %
                   config_path)

    cfg = config.load()

    # Override pager/less options
    os.environ['PAGER'] = cfg['pager']
    os.environ['LESS'] = '-RXF'

    if spec:
        f = urlopen(spec)
        try:
            content = f.read().decode('utf-8')
            try:
                spec = json.loads(content)
            except json.JSONDecodeError:
                click.secho("Warning: Specification file '%s' is not JSON" %
                            spec, err=True, fg='red')
                spec = None
        finally:
            f.close()

    url = fix_incomplete_url(url)
    context = Context(url, spec=spec)

    output_style = cfg.get('output_style')
    if output_style:
        context.options['--style'] = output_style

    # For prompt-toolkit
    history = InMemoryHistory()
    lexer = PygmentsLexer(HttpPromptLexer)
    completer = HttpPromptCompleter(context)
    try:
        style_class = get_style_by_name(cfg['command_style'])
    except ClassNotFound:
        style_class = Solarized256Style
    style = style_from_pygments(style_class)

    listener = ExecutionListener(cfg)

    # Execute HTTPie options from CLI or load from last context
    if len(sys.argv) > 1:
        http_options = [smart_quote(a) for a in http_options]
        execute(' '.join(http_options), context, listener=listener)
    else:
        load_context(context)

    while True:
        try:
            text = prompt('%s> ' % context.url, completer=completer,
                          lexer=lexer, style=style, history=history,
                          auto_suggest=AutoSuggestFromHistory(),
                          on_abort=AbortAction.RETRY, vi_mode=cfg['vi'])
        except EOFError:
            break  # Control-D pressed
        else:
            execute(text, context, listener=listener, style=style_class)
            if context.should_exit:
                break

    click.echo("Goodbye!")
Example #46
0
    def init_prompt_toolkit_cli(self):
        self._app = None
        if self.simple_prompt:
            # Fall back to plain non-interactive output for tests.
            # This is very limited, and only accepts a single line.
            def prompt():
                return cast_unicode_py2(
                    input('In [%d]: ' % self.execution_count))

            self.prompt_for_code = prompt
            return

        kbmanager = KeyBindingManager.for_prompt()
        insert_mode = ViInsertMode() | EmacsInsertMode()
        # Ctrl+J == Enter, seemingly
        @kbmanager.registry.add_binding(Keys.ControlJ,
                                        filter=(HasFocus(DEFAULT_BUFFER)
                                                & ~HasSelection()
                                                & insert_mode))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >=
                    d.line_count - d.empty_line_count_at_the_end()):
                b.newline()
                return

            status, indent = self.input_splitter.check_complete(d.text)

            if (status != 'incomplete') and b.accept_action.is_returnable:
                b.accept_action.validate_and_handle(event.cli, b)
            else:
                b.insert_text('\n' + (' ' * (indent or 0)))

        @kbmanager.registry.add_binding(Keys.ControlC,
                                        filter=HasFocus(DEFAULT_BUFFER))
        def _reset_buffer(event):
            event.current_buffer.reset()

        @kbmanager.registry.add_binding(Keys.ControlC,
                                        filter=HasFocus(SEARCH_BUFFER))
        def _reset_search_buffer(event):
            if event.current_buffer.document.text:
                event.current_buffer.reset()
            else:
                event.cli.push_focus(DEFAULT_BUFFER)

        supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP'))

        @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend)
        def _suspend_to_bg(event):
            event.cli.suspend_to_background()

        @Condition
        def cursor_in_leading_ws(cli):
            before = cli.application.buffer.document.current_line_before_cursor
            return (not before) or before.isspace()

        # Ctrl+I == Tab
        @kbmanager.registry.add_binding(Keys.ControlI,
                                        filter=(HasFocus(DEFAULT_BUFFER)
                                                & ~HasSelection()
                                                & insert_mode
                                                & cursor_in_leading_ws))
        def _indent_buffer(event):
            event.current_buffer.insert_text(' ' * 4)

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for __, ___, cell in self.history_manager.get_tail(
                self.history_load_length, include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)

        self._style = self._make_style_from_name(self.highlighting_style)
        style = DynamicStyle(lambda: self._style)

        editing_mode = getattr(EditingMode, self.editing_mode.upper())

        self._app = create_prompt_application(
            editing_mode=editing_mode,
            key_bindings_registry=kbmanager.registry,
            history=history,
            completer=IPythonPTCompleter(self.Completer),
            enable_history_search=True,
            style=style,
            mouse_support=self.mouse_support,
            **self._layout_options())
        self._eventloop = create_eventloop(self.inputhook)
        self.pt_cli = CommandLineInterface(self._app,
                                           eventloop=self._eventloop)
Example #47
0
    def init_prompt_toolkit_cli(self):
        if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty():
            # Fall back to plain non-interactive output for tests.
            # This is very limited, and only accepts a single line.
            def prompt():
                return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
            self.prompt_for_code = prompt
            return

        kbmanager = KeyBindingManager.for_prompt(enable_vi_mode=self.vi_mode)
        insert_mode = ViStateFilter(kbmanager.get_vi_state, InputMode.INSERT)
        # Ctrl+J == Enter, seemingly
        @kbmanager.registry.add_binding(Keys.ControlJ,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                   ))
        def _(event):
            b = event.current_buffer
            d = b.document
            if not (d.on_last_line or d.cursor_position_row >= d.line_count
                                           - d.empty_line_count_at_the_end()):
                b.newline()
                return

            status, indent = self.input_splitter.check_complete(d.text)

            if (status != 'incomplete') and b.accept_action.is_returnable:
                b.accept_action.validate_and_handle(event.cli, b)
            else:
                b.insert_text('\n' + (' ' * (indent or 0)))

        @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER))
        def _(event):
            event.current_buffer.reset()

        @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER))
        def _(event):
            if event.current_buffer.document.text:
                event.current_buffer.reset()
            else:
                event.cli.push_focus(DEFAULT_BUFFER)

        supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP'))

        @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend)
        def _(event):
            event.cli.suspend_to_background()

        @Condition
        def cursor_in_leading_ws(cli):
            before = cli.application.buffer.document.current_line_before_cursor
            return (not before) or before.isspace()

        # Ctrl+I == Tab
        @kbmanager.registry.add_binding(Keys.ControlI,
                            filter=(HasFocus(DEFAULT_BUFFER)
                                    & ~HasSelection()
                                    & insert_mode
                                    & cursor_in_leading_ws
                                   ))
        def _(event):
            event.current_buffer.insert_text(' ' * 4)

        # Pre-populate history from IPython's history database
        history = InMemoryHistory()
        last_cell = u""
        for _, _, cell in self.history_manager.get_tail(self.history_load_length,
                                                        include_latest=True):
            # Ignore blank lines and consecutive duplicates
            cell = cell.rstrip()
            if cell and (cell != last_cell):
                history.append(cell)

        self._style = self._make_style_from_name(self.highlighting_style)
        style = DynamicStyle(lambda: self._style)

        self._app = create_prompt_application(
                            key_bindings_registry=kbmanager.registry,
                            history=history,
                            completer=IPythonPTCompleter(self.Completer),
                            enable_history_search=True,
                            style=style,
                            mouse_support=self.mouse_support,
                            **self._layout_options()
        )
        self.pt_cli = CommandLineInterface(self._app,
                           eventloop=create_eventloop(self.inputhook))
Example #48
0
async def repl(old_ctx,
               prompt_kwargs=None,
               allow_system_commands=True,
               allow_internal_commands=True,
               once=False,
               get_bottom_toolbar_tokens=_get_bottom_toolbar_tokens,
               get_prompt_tokens=None,
               style=_style):
    """
    Start an interactive shell. All subcommands are available in it.

    :param old_ctx: The current Click context.
    :param prompt_kwargs: Parameters passed to
        :py:func:`prompt_toolkit.shortcuts.prompt`.

    If stdin is not a TTY, no prompt will be printed, but only commands read
    from stdin.

    """
    # parent should be available, but we're not going to bother if not
    group_ctx = old_ctx.parent or old_ctx
    group = group_ctx.command
    isatty = sys.stdin.isatty()

    # Delete the REPL command from those available, as we don't want to allow
    # nesting REPLs (note: pass `None` to `pop` as we don't want to error if
    # REPL command already not present for some reason).
    repl_command_name = old_ctx.command.name
    available_commands = group_ctx.command.commands
    available_commands.pop(repl_command_name, None)

    if isatty:
        prompt_kwargs = prompt_kwargs or {}
        if not get_prompt_tokens:
            prompt_kwargs.setdefault('message', u'>> ')
        history = prompt_kwargs.pop('history', None) \
            or InMemoryHistory()
        completer = prompt_kwargs.pop('completer', None) \
            or ClickCompleter(group)

        def get_command():
            return prompt(
                completer=completer,
                history=history,
                # patch_stdout=False,
                # https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/get-multiline-input.py
                # multiline=True,
                # get_continuation_tokens=continuation_tokens,
                # get_bottom_toolbar_tokens=get_bottom_toolbar_tokens,
                # get_prompt_tokens=get_prompt_tokens,
                style=style,
                async_=True,
                **prompt_kwargs)
    else:
        get_command = sys.stdin.readline

    stopped = False
    while not stopped:
        try:
            command = await get_command()
        except KeyboardInterrupt:
            continue
        except EOFError:
            break
        finally:
            if once:
                stopped = True

        if not command:
            if isatty:
                continue
            else:
                break

        if allow_system_commands and dispatch_repl_commands(command):
            continue

        if allow_internal_commands:
            try:
                result = handle_internal_commands(command)
                if isinstance(result, six.string_types):
                    click.echo(result)
                    continue
            except ExitReplException:
                break

        args = shlex.split(command)

        try:
            with group.make_context(None, args, parent=group_ctx) as ctx:
                f = group.invoke(ctx)
                if f:
                    await f
                ctx.exit()

        except ApplicationError as e:
            click.echo(style_error(u'[{}] {}'.format(e.error, e.args[0])))

        except click.ClickException as e:
            e.show()

        except SystemExit:
            pass
def _history():
    " Prefilled history. "
    history = InMemoryHistory()
    history.append_string('alpha beta gamma delta')
    history.append_string('one two three four')
    return history
from prompt_toolkit import prompt
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory

history = InMemoryHistory()
for word in ["apple", "banana", "orange", "grape", "pineapple"]:
    history.append(word)

while True:
    text = prompt('> ', history=history, auto_suggest=AutoSuggestFromHistory())
    print('You said: ', text)