def configure(repl: PythonRepl) -> None: if os.path.exists(config_file): run_config(repl, config_file) else: # from https://github.com/prompt-toolkit/ptpython/blob/master/examples/ptpython_config/config.py # Ask for confirmation on exit. repl.confirm_exit = False # embedded in other applications. repl.title = "Skepticoin %s " % __version__
def shell(globals_, locals_): """ Customized pypython.repl. """ # Create REPL. repl = PythonRepl( get_globals=lambda : globals_, get_locals=lambda : locals_, history_filename=os.path.expanduser("~/.pyhistory.shell"), ) run_config(repl) with DummyContext(): repl.run()
def shell(globals_, locals_): """ Customized pypython.repl. """ # Create REPL. repl = PythonRepl( get_globals=lambda: globals_, get_locals=lambda: locals_, history_filename=os.path.expanduser("~/.pyhistory.shell"), ) run_config(repl) with DummyContext(): repl.run()
def __init__(self, get_globals, get_locals=None): assert callable(get_globals) assert get_locals is None or callable(get_locals) self._chan = None def _globals(): data = get_globals() data.setdefault('print', self._print) return data repl = PythonRepl(get_globals=_globals, get_locals=get_locals or _globals) # Disable open-in-editor and system prompt. Because it would run and # display these commands on the server side, rather than in the SSH # client. repl.settings.enable_open_in_editor = False repl.settings.enable_system_bindings = False # PipInput object, for sending input in the CLI. # (This is something that we can use in the prompt_toolkit event loop, # but still write date in manually.) self._input_pipe = PipeInput() # Output object. Don't render to the real stdout, but write everything # in the SSH channel. class Stdout(object): def write(s, data): if self._chan is not None: self._chan.write(data.replace('\n', '\r\n')) def flush(s): pass # Create command line interface. self.cli = CommandLineInterface( application=repl.create_application(), eventloop=create_asyncio_eventloop(), input=self._input_pipe, output=Vt100_Output(Stdout(), self._get_size)) self._callbacks = self.cli.create_eventloop_callbacks()
def __init__(self, get_globals, get_locals=None): assert callable(get_globals) assert get_locals is None or callable(get_locals) self._chan = None def _globals(): data = get_globals() data.setdefault('print', self._print) return data repl = PythonRepl(get_globals=_globals, get_locals=get_locals or _globals) # Disable open-in-editor and system prompt. Because it would run and # display these commands on the server side, rather than in the SSH # client. repl.enable_open_in_editor = False repl.enable_system_bindings = False # PipInput object, for sending input in the CLI. # (This is something that we can use in the prompt_toolkit event loop, # but still write date in manually.) self._input_pipe = PipeInput() # Output object. Don't render to the real stdout, but write everything # in the SSH channel. class Stdout(object): def write(s, data): if self._chan is not None: self._chan.write(data.replace('\n', '\r\n')) def flush(s): pass # Create command line interface. self.cli = CommandLineInterface(application=repl.create_application(), eventloop=create_asyncio_eventloop(), input=self._input_pipe, output=Vt100_Output( Stdout(), self._get_size)) self._callbacks = self.cli.create_eventloop_callbacks()
def __init__(self, get_globals: _GetNamespace, get_locals: Optional[_GetNamespace] = None) -> None: self._chan = None def _globals() -> dict: data = get_globals() data.setdefault("print", self._print) return data # PipInput object, for sending input in the CLI. # (This is something that we can use in the prompt_toolkit event loop, # but still write date in manually.) self._input_pipe = create_pipe_input() # Output object. Don't render to the real stdout, but write everything # in the SSH channel. class Stdout: def write(s, data: str) -> None: if self._chan is not None: data = data.replace("\n", "\r\n") self._chan.write(data) def flush(s) -> None: pass self.repl = PythonRepl( get_globals=_globals, get_locals=get_locals or _globals, input=self._input_pipe, output=Vt100_Output(cast(TextIO, Stdout()), self._get_size), ) # Disable open-in-editor and system prompt. Because it would run and # display these commands on the server side, rather than in the SSH # client. self.repl.enable_open_in_editor = False self.repl.enable_system_bindings = False
def configure(repl: PythonRepl) -> None: repl.vi_mode = True repl.enable_fuzzy_completion = True # `history search` can not be used with `complete while typing` repl.enable_history_search = True repl.confirm_exit = False repl.highlight_matching_parenthesis = True repl.color_depth = "DEPTH_24_BIT" # The signatures shown aren't complete enough that I want to have them on # by default. # For example itertools.islice shows the `(iterable, stop)` signature, # but not the `(iterable, start, stop, step)` signature, which is # potentially confusing. repl.show_signature = False
def default_configure(repl: PythonRepl): """ Default REPL configuration function :param repl: :return: """ repl.show_signature = True repl.show_docstring = True repl.show_status_bar = True repl.show_sidebar_help = True repl.highlight_matching_parenthesis = True repl.wrap_lines = True repl.complete_while_typing = True repl.vi_mode = False repl.paste_mode = False repl.prompt_style = 'classic' # 'classic' or 'ipython' repl.insert_blank_line_after_output = False repl.enable_history_search = False repl.enable_auto_suggest = False repl.enable_open_in_editor = True repl.enable_system_bindings = False repl.confirm_exit = True repl.enable_input_validation = True
def configure(repl: PythonRepl): """ Configuration method. This is called during the start-up of ptpython. :param repl: `PythonRepl` instance. """ # Show function signature (bool). repl.show_signature = True # Show docstring (bool). repl.show_docstring = True # Show the "[Meta+Enter] Execute" message when pressing [Enter] only # inserts a newline instead of executing the code. repl.show_meta_enter_message = True # Show completions. (NONE, POP_UP, MULTI_COLUMN or TOOLBAR) repl.completion_visualisation = CompletionVisualisation.POP_UP # When CompletionVisualisation.POP_UP has been chosen, use this # scroll_offset in the completion menu. repl.completion_menu_scroll_offset = 0 # Show line numbers (when the input contains multiple lines.) repl.show_line_numbers = False # Show status bar. repl.show_status_bar = True # When the sidebar is visible, also show the help text. repl.show_sidebar_help = True # Swap light/dark colors on or off repl.swap_light_and_dark = False # Highlight matching parethesis. repl.highlight_matching_parenthesis = True # Line wrapping. (Instead of horizontal scrolling.) repl.wrap_lines = True # Mouse support. repl.enable_mouse_support = True # Complete while typing. (Don't require tab before the # completion menu is shown.) repl.complete_while_typing = True # Fuzzy and dictionary completion. repl.enable_fuzzy_completion = True repl.enable_dictionary_completion = False # Vi mode. repl.vi_mode = False # Paste mode. (When True, don't insert whitespace after new line.) repl.paste_mode = False # Use the classic prompt. (Display '>>>' instead of 'In [1]'.) repl.prompt_style = 'classic' # 'classic' or 'ipython' # Don't insert a blank line after the output. repl.insert_blank_line_after_output = False # History Search. # When True, going back in history will filter the history on the records # starting with the current input. (Like readline.) # Note: When enable, please disable the `complete_while_typing` option. # otherwise, when there is a completion available, the arrows will # browse through the available completions instead of the history. repl.enable_history_search = False # Enable auto suggestions. (Pressing right arrow will complete the input, # based on the history.) repl.enable_auto_suggest = True # Enable open-in-editor. Pressing C-x C-e in emacs mode or 'v' in # Vi navigation mode will open the input in the current editor. repl.enable_open_in_editor = True # Enable system prompt. Pressing meta-! will display the system prompt. # Also enables Control-Z suspend. repl.enable_system_bindings = True # Ask for confirmation on exit. repl.confirm_exit = True # Enable input validation. (Don't try to execute when the input contains # syntax errors.) repl.enable_input_validation = True # Use this colorscheme for the code. repl.use_code_colorscheme('monokai') # Set color depth (keep in mind that not all terminals support true color). # repl.color_depth = 'DEPTH_1_BIT' # Monochrome. # repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only. repl.color_depth = "DEPTH_8_BIT" # The default, 256 colors. # repl.color_depth = 'DEPTH_24_BIT' # True color. # Syntax. repl.enable_syntax_highlighting = True # Install custom colorscheme named 'my-colorscheme' and use it. """ repl.install_ui_colorscheme('my-colorscheme', Style.from_dict(_custom_ui_colorscheme)) repl.use_ui_colorscheme('my-colorscheme') """ # Add custom key binding for PDB. """ @repl.add_key_binding(Keys.ControlB) def _(event): ' Pressing Control-B will insert "pdb.set_trace()" ' event.cli.current_buffer.insert_text('\nimport pdb; pdb.set_trace()\n') """ # Typing ControlE twice should also execute the current command. # (Alternative for Meta-Enter.) """ @repl.add_key_binding(Keys.ControlE, Keys.ControlE) def _(event): event.current_buffer.validate_and_handle() """ # Typing 'jj' in Vi Insert mode, should send escape. (Go back to navigation # mode.) """ @repl.add_key_binding('j', 'j', filter=ViInsertMode()) def _(event): " Map 'jj' to Escape. " event.cli.key_processor.feed(KeyPress(Keys.Escape)) """ # Custom key binding for some simple autocorrection while typing. """ corrections = { 'impotr': 'import', 'pritn': 'print', } @repl.add_key_binding(' ') def _(event): ' When a space is pressed. Check & correct word before cursor. ' b = event.cli.current_buffer w = b.document.get_word_before_cursor() if w is not None: if w in corrections: b.delete_before_cursor(count=len(w)) b.insert_text(corrections[w]) b.insert_text(' ') """ # Add a custom title to the status bar. This is useful when ptpython is # embedded in other applications. """
class ReplSSHServerSession(asyncssh.SSHServerSession): """ SSH server session that runs a Python REPL. :param get_globals: callable that returns the current globals. :param get_locals: (optional) callable that returns the current locals. """ def __init__(self, get_globals: _GetNamespace, get_locals: Optional[_GetNamespace] = None) -> None: self._chan = None def _globals() -> dict: data = get_globals() data.setdefault("print", self._print) return data # PipInput object, for sending input in the CLI. # (This is something that we can use in the prompt_toolkit event loop, # but still write date in manually.) self._input_pipe = create_pipe_input() # Output object. Don't render to the real stdout, but write everything # in the SSH channel. class Stdout: def write(s, data: str) -> None: if self._chan is not None: data = data.replace("\n", "\r\n") self._chan.write(data) def flush(s) -> None: pass self.repl = PythonRepl( get_globals=_globals, get_locals=get_locals or _globals, input=self._input_pipe, output=Vt100_Output(cast(TextIO, Stdout()), self._get_size), ) # Disable open-in-editor and system prompt. Because it would run and # display these commands on the server side, rather than in the SSH # client. self.repl.enable_open_in_editor = False self.repl.enable_system_bindings = False def _get_size(self) -> Size: """ Callable that returns the current `Size`, required by Vt100_Output. """ if self._chan is None: return Size(rows=20, columns=79) else: width, height, pixwidth, pixheight = self._chan.get_terminal_size() return Size(rows=height, columns=width) def connection_made(self, chan): """ Client connected, run repl in coroutine. """ self._chan = chan # Run REPL interface. f = asyncio.ensure_future(self.repl.run_async()) # Close channel when done. def done(_) -> None: chan.close() self._chan = None f.add_done_callback(done) def shell_requested(self) -> bool: return True def terminal_size_changed(self, width, height, pixwidth, pixheight): """ When the terminal size changes, report back to CLI. """ self.repl.app._on_resize() def data_received(self, data, datatype): """ When data is received, send to inputstream of the CLI and repaint. """ self._input_pipe.send(data) def _print(self, *data, sep=" ", end="\n", file=None) -> None: """ Alternative 'print' function that prints back into the SSH channel. """ # Pop keyword-only arguments. (We cannot use the syntax from the # signature. Otherwise, Python2 will give a syntax error message when # installing.) data = sep.join(map(str, data)) self._chan.write(data + end)