def test_context_manager_with_unmocked_readline(self): from letsencrypt.display import completer reload_module(completer) original_completer = readline.get_completer() original_delims = readline.get_completer_delims() with completer.Completer(): pass self.assertEqual(readline.get_completer(), original_completer) self.assertEqual(readline.get_completer_delims(), original_delims)
def launch_ui(args): # Setup tab completion try: import readline except ImportError: print('%s[!] Module \'readline\' not available. Tab complete disabled.%s' % (Colors.R, Colors.N)) else: import rlcompleter if 'libedit' in readline.__doc__: readline.parse_and_bind('bind ^I rl_complete') else: readline.parse_and_bind('tab: complete') readline.set_completer_delims(re.sub('[/-]', '', readline.get_completer_delims())) # Instantiate the UI object x = cli.CLI(cli.Mode.CONSOLE) # check for and run version check if args.check: if not x.version_check(): return # Check for and run script session if args.script_file: x.do_resource(args.script_file) # Run the UI try: x.cmdloop() except KeyboardInterrupt: print('')
def improved_rlcompleter(self): """Enhances the default rlcompleter The function enhances the default rlcompleter by also doing pathname completion and module name completion for import statements. Additionally, it inserts a tab instead of attempting completion if there is no preceding text. """ completer = rlcompleter.Completer(namespace=self.locals) # - remove / from the delimiters to help identify possibility for path completion readline.set_completer_delims(readline.get_completer_delims().replace('/', '')) modlist = frozenset(name for _, name, _ in pkgutil.iter_modules()) def complete_wrapper(text, state): line = readline.get_line_buffer().strip() if line == '': return None if state > 0 else self.tab if state == 0: if line.startswith('import') or line.startswith('from'): completer.matches = [name for name in modlist if name.startswith(text)] else: match = completer.complete(text, state) if match is None and '/' in text: completer.matches = glob.glob(text+'*') try: match = completer.matches[state] return '{}{}'.format(match, ' ' if keyword.iskeyword(match) else '') except IndexError: return None return complete_wrapper
def __init__(self, admin_cli): # remove stdout stream encoding while in 'shell' mode, becuase this breaks readline # (autocompletion and shell history). In 'shell' mode the stdout # is encoded just for time necessary for command execution see precmd a postcmd sys.stdout = stdout_origin self.stdout_with_codec = encode_stream(sys.stdout, "utf-8") self.completion_matches = None Cmd.__init__(self) self.admin_cli = admin_cli self.completion = Completion(self.admin_cli) try: Config() self.prompt = Config.parser.get('shell', 'prompt') + ' ' except (ConfigFileError, ConfigParser.Error): self.prompt = 'katello> ' try: # don't split on hyphens during tab completion (important for completing parameters) newdelims = readline.get_completer_delims() newdelims = re.sub('-', '', newdelims) readline.set_completer_delims(newdelims) if (Config.parser.get('shell', 'nohistory').lower() != 'true'): self.__init_history() except ConfigParser.Error: pass self.__init_commands()
def run(self): """ Start logging shell. """ exit_commands = ['exit', 'Exit', 'EXIT', 'q', 'quit', 'Quit'] pathes = os.getenv('PATH').split(':') pathes.append('./') self._commands = [] for path in pathes: out, err = self._execute('ls %(path)s' % {'path': path}) self._commands.extend(out.split('\n')) self._commands.extend(exit_commands) self._commands.sort() readline.set_completer(self.complete) original_delims = readline.get_completer_delims() new_delims = original_delims.replace('.', '').replace('/', '') readline.set_completer_delims(new_delims) print 'Start logging to "%(file)s"' % {'file': self._file.name} print 'exit logging by typing one of %(exit)s' % \ {'exit': exit_commands} readline.parse_and_bind('tab: complete') stdin = raw_input(self._prefix) while (stdin not in exit_commands): try: self.execute(stdin) stdin = raw_input(self._prefix) except EOFError: print 'The input file has finished reading.' break print 'End logging. Log file is "%(file)s"' % {'file': self._file.name}
def __init__(self, command_line): self.status = {} self.status['log_started'] = False self.status['resource_found'] = False self.status['command_line'] = command_line self.status['file_found'] = False self.global_config = {} self.tools = [] self.assessments = [] self.job_queue = [] self.arguments = ddict_options = defaultdict(lambda : '') self.instance = {} self.instance['tool'] = [] self.instance['config'] = {} self.load("tools") self.load("assessments") self.load("global_config") # Remove / from completer delim so tab completion works # with tool/nmap, for example old_delims = readline.get_completer_delims() readline.set_completer_delims(old_delims.replace('/', ''))
def rlinput(prompt, prefill='', oneline=False, ctxkey=''): """ Get user input with readline editing support. """ sentinel = '' if prefill is None: prefill = '' def only_once(text): """ generator for startup hook """ readline.insert_text(text) yield while True: yield savedhist = NamedTemporaryFile() readline.write_history_file(savedhist.name) ctxhistname = ".tl" + ctxkey + "history" ctxhistfile = os.path.join(G.ProjectFolder, ctxhistname) try: readline.clear_history() except AttributeError: print "This readline doesn't support clear_history()" raise savedcompleter = readline.get_completer() try: ulines = uniqify(ctxhistfile) readline.read_history_file(ctxhistfile) readline.set_completer(HistoryCompleter(ulines).complete) except IOError: pass readline.parse_and_bind('tab: complete') saveddelims = readline.get_completer_delims() readline.set_completer_delims('') ## No delims. Complete entire lines. readline.set_completion_display_matches_hook(match_display_hook) gen = only_once(prefill) readline.set_startup_hook(gen.next) try: if oneline: edited = raw_input(prompt) else: print prompt edited = "\n".join(iter(raw_input, sentinel)) if edited.endswith(r'%%'): ## Invoke external editor edited = external_edit(edited[0:-2]) return edited finally: ## Restore readline state readline.write_history_file(ctxhistfile) readline.clear_history() readline.read_history_file(savedhist.name) savedhist.close() readline.set_completer(savedcompleter) readline.set_completer_delims(saveddelims) readline.set_startup_hook()
def __init__(self, complete_key='tab', prompt='> ', stdin=None, stdout=None): ''' Instantiate a simple line-oriented interpreter framework. The optional argument 'complete_key' is the readline name of a completion key; it defaults to the Tab key ('tab'). If complete_key is not None, command completion is done by the 'complete()' method. The optional arguments stdin and stdout specify alternate input and output file objects; if not specified, sys.stdin and sys.stdout are used. ''' # key used to trigger autocomplete self.complete_key = complete_key self.stdin = stdin or sys.stdin self.stdout = stdout or sys.stdout # the prompt issued to the user to gather input self.prompt = prompt # delimiters for readline to use during completion self.completer_delimiters = readline.get_completer_delims() # the intro displayed before the first prompt is issued self.intro = None
def exec_cmdloop(self, args, options): try: import readline delims = readline.get_completer_delims() delims = delims.replace(':', '') # "group:process" as one word delims = delims.replace('*', '') # "group:*" as one word delims = delims.replace('-', '') # names with "-" as one word readline.set_completer_delims(delims) if options.history_file: try: readline.read_history_file(options.history_file) except IOError: pass def save(): try: readline.write_history_file(options.history_file) except IOError: pass import atexit atexit.register(save) except ImportError: pass try: self.cmdqueue.append('status') self.cmdloop() except KeyboardInterrupt: self.output('') pass
def startup(): # python startup file import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') readline.set_completer(completer) # do not use - as delimiter old_delims = readline.get_completer_delims() # <- readline.set_completer_delims(old_delims.replace('-', '')) # <- # history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter import readline
def interact(PS1, PS2, BANNER, *arg, **kwarg): def Completer(text, stat): if text.startswith('.') or text.startswith('/'): ret = path_matches(text) elif '.' not in text: ret = global_matches(text) else: ret = attr_matches(text) try: return ret[stat] except IndexError: return None @utils.regExitCallback def exit_interact(): """ Clean all when exit """ print "Goodbye..." ## Compatible for Mac OS since Mac OS ship libedit for readline if "libedit" in readline.__doc__: import rlcompleter readline.parse_and_bind("bind ^I rl_complete") else: readline.parse_and_bind("tab: complete") ## Change PS sys.ps1, sys.ps2 = PS1, PS2 delims = readline.get_completer_delims().replace('/','') readline.set_completer_delims(delims) readline.set_completer(Completer) ## Run Interpreter code.interact(banner=BANNER, local=globals())
def initialize(self, region='us-west-1', host='localhost', port=8000, access_key=None, secret_key=None, config_dir=None): """ Set up the repl for execution. """ # Tab-complete names with a '-' in them import readline delims = set(readline.get_completer_delims()) if '-' in delims: delims.remove('-') readline.set_completer_delims(''.join(delims)) self._conf_dir = (config_dir or os.path.join(os.environ.get('HOME', '.'), '.config')) self.session = botocore.session.get_session() if access_key: self.session.set_credentials(access_key, secret_key) if region == 'local': conn = DynamoDBConnection.connect_to_host(host, port, session=self.session) else: conn = DynamoDBConnection.connect_to_region(region, self.session) self.engine = FragmentEngine(conn) conf = self.load_config() display_name = conf.get('display') if display_name is not None: self.display = DISPLAYS[display_name] else: self.display = get_default_display() self.formatter = SmartFormat(pagesize=conf.get('pagesize', 1000), width=conf.get('width', 80)) for line in conf.get('autorun', []): six.exec_(line, self.engine.scope)
def main(self, argv): cmd_args = argv[1:] if cmd_args: cmd_line = u' '.join(cmd_args) cmds = cmd_line.split(';') for cmd in cmds: ret = self.onecmd(cmd) if ret: return ret elif self.DISABLE_REPL: self._parser.print_help() self._parser.exit() else: try: import readline except ImportError: pass else: # Remove '-' from delims readline.set_completer_delims(readline.get_completer_delims().replace('-', '')) history_filepath = os.path.join(self.weboob.workdir, '%s_history' % self.APPNAME) try: readline.read_history_file(history_filepath) except IOError: pass def savehist(): readline.write_history_file(history_filepath) atexit.register(savehist) self.intro += '\nLoaded backends: %s\n' % ', '.join(sorted(backend.name for backend in self.weboob.iter_backends())) self._interactive = True self.cmdloop()
def __init__(self, verbose=False): Cmd.__init__(self) self.pp = pprint.PrettyPrinter(indent=4) try: self.conn = boto.connect_dynamodb() except Exception as e: self.conn = None print e print "Cannot connect to dynamodb - Check your credentials in ~/.boto or use the 'login' command" # by default readline thinks - and other characters are word delimiters :( if readline: readline.set_completer_delims(re.sub('[-~]', '', readline.get_completer_delims())) path = os.path.join(os.environ.get('HOME', ''), HISTORY_FILE) self.history_file = os.path.abspath(path) else: self.history_file = None self.tables = [] self.table = None self.consistent = False self.consumed = False self.verbose = verbose self.next_key = None self.schema = {} if verbose: self._onchange_verbose(None, verbose)
def sftp_cmd(*args): sftp = Channel.get_instance().get_transport().open_sftp_client() old_completer = readline.get_completer() readline.set_completer(sftp_completer(sftp)) old_delim = readline.get_completer_delims() readline.set_completer_delims(' /') global rcwd try: try: cmd = raw_input('SFTP> ') except EOFError: return except KeyboardInterrupt: return while not cmd or not 'quit'.startswith(cmd): args = [x for x in cmd.split(' ') if x] if args and args[0] in all_cmd: all_cmd[args[0]](sftp, args) else: print('invalid command') try: cmd = raw_input('SFTP> ') except EOFError: return except KeyboardInterrupt: return finally: readline.set_completer(old_completer) readline.set_completer_delims(old_delim)
def cmdloop(self, intro=None): self.old_completer = readline.get_completer() self.old_completer_delims = readline.get_completer_delims() readline.set_completer(self.complete) readline.parse_and_bind(self.completekey+": complete") readline.parse_and_bind("set bell-style none") readline.parse_and_bind("set show-all-if-ambiguous") readline.parse_and_bind("set completion-query-items -1") # If press help key, add the character and accept the line readline.parse_and_bind('"?": "\C-q?\C-j"') # Register a function for execute before read user # input. We can use it for insert text at command line readline.set_pre_input_hook(self.pre_input_hook) readline.set_completer_delims(' \t\n') try: stop = None while not stop: try: line = raw_input(self.prompt) except EOFError: line = 'EOF' stop = self.onecmd(line) stop = self.postcmd(stop, line) finally: readline.set_completer(self.old_completer) readline.set_completer_delims(self.old_completer_delims)
def setup_readline(): readline.set_history_length(100) readline.parse_and_bind("tab: complete") readline.set_completer(completer) readline.set_completer_delims(\ readline.get_completer_delims().replace('-','').replace('/','').replace('=','')) try: readline.read_history_file(vars.hist_file) except: pass
def __init__(self): cmd.Cmd.__init__(self) self.prompt = "BN> " self.ic = None default_delims = readline.get_completer_delims() new_delims = ''.join((c not in '-/[]') and c or '' for c in default_delims) readline.set_completer_delims(new_delims)
def configure_readline(): """Configure the readline module""" if rlmodule is not None: # pragma: no cover delims = list(rlmodule.get_completer_delims()) for char in {'-', '"', "'", '!', '@', '?'}: if char in delims: delims.remove(char) rlmodule.set_completer_delims(''.join(delims))
def main(): readline.set_completer(compl1) print(readline.get_completer_delims()) while True: s = input("input str: ") print(s) print("buf: " + readline.get_line_buffer()) if s == "quit": break
def main(): # Set up readline to tab complete. readline.parse_and_bind('tab: complete') # Remove '-' from the word delimiters. delims = readline.get_completer_delims() readline.set_completer_delims(delims.replace('-', '')) cli = CLI(sys.argv[1]) cli.run()
def preloop(self): Cmd.preloop(self) if self.completekey: try: import readline delims = readline.get_completer_delims() delims = "".join([x for x in delims if x not in "-:@"]) readline.set_completer_delims(delims) except ImportError: pass
def __init__(self, namespace = None, Config=None): rlcompleter.Completer.__init__(self, namespace) self.config = self.get_config(Config) if self.config.use_colors: readline.parse_and_bind('set dont-escape-ctrl-chars on') if self.config.consider_getitems: delims = readline.get_completer_delims() delims = delims.replace('[', '') delims = delims.replace(']', '') readline.set_completer_delims(delims)
def __enter__(self): # store the old values self.cmpl_function = readline.get_completer() self.delims = readline.get_completer_delims() # no completion delimiters readline.set_completer_delims('') # this class works as completer readline.set_completer(self) # tab completes readline.parse_and_bind('tab: complete')
def preloop(self): """Initialization before prompting user for commands. Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub. """ cmd.Cmd.preloop(self) ## sets up command completion self._history = "" ## No historyory yet self._locals = {} ## Initialize execution namespace for user self._globals = {} old_delims = readline.get_completer_delims() readline.set_completer_delims(old_delims.replace('-', ''))
def cmdloop(self, intro=None): """ Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument. """ self.preloop() if self.use_rawinput and self.completekey: try: import readline self.old_completer = readline.get_completer() readline.set_completer(self.complete) readline.set_completer_delims(readline.get_completer_delims().replace("/", "")) if self.history_file != None and os.path.exists(self.history_file): readline.read_history_file(self.history_file) readline.parse_and_bind(self.completekey + ": complete") except ImportError: pass try: if intro is not None: self.intro = intro if self.intro: self.stdout.write(str(self.intro)+"\n") stop = None while not stop: if self.cmdqueue: line = self.cmdqueue.pop(0) else: if self.use_rawinput: try: line = raw_input(self.prompt) except EOFError: line = 'EOF' else: self.stdout.write(self.prompt) self.stdout.flush() line = self.stdin.readline() if not len(line): line = 'EOF' else: line = line.rstrip('\r\n') line = self.precmd(line) stop = self.onecmd(line) stop = self.postcmd(stop, line) self.postloop() finally: if self.use_rawinput and self.completekey: try: import readline if self.history_file != None: readline.write_history_file(self.history_file) readline.set_completer(self.old_completer) except ImportError: pass
def _ReadlinePrepare(self): """Prepare readline for use.""" readline.set_completion_display_matches_hook( self.root.FormatCompleterOptions) readline.set_completer(self.root.ReadlineCompleter) readline.parse_and_bind('tab: complete') readline.parse_and_bind('?: possible-completions') readline.set_completer_delims(' \t') self._old_delims = readline.get_completer_delims() readline.set_completer_delims(' ')
def setup_readline(): import readline readline.set_history_length(100) readline.parse_and_bind("tab: complete") readline.set_completer(completer) readline.set_completer_delims(readline.get_completer_delims().replace("-", "").replace("/", "").replace("=", "")) try: readline.read_history_file(vars.hist_file) except: pass
def complete_loadscript(self, text, line, start_index, end_index): # tab-completion for the file path: try: # remove specific delimeters from the readline parser # to allow completion of filenames with dashes import readline delims = readline.get_completer_delims( ) delims = delims.replace('-', '') readline.set_completer_delims(delims) except ImportError: pass return get_possible_filename_completions(extract_full_argument(line,end_index))
def init_readline_hist(): readline.parse_and_bind("tab: complete") histfile = os.path.join(os.path.expanduser("~"), ".audit_hist") try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) # also fix up delims so we don't have annoying dir elt behavior delims = readline.get_completer_delims() delims = delims.replace("/", "") readline.set_completer_delims(delims)
def complete(text, state): line_buffer = readline.get_line_buffer() try: line = split(line_buffer) except ValueError: return None if not line: line = [""] index = len(line) if not line_buffer.endswith(tuple(readline.get_completer_delims())): index -= 1 return [ option + " " for option in resolve(options, line[:index]) if option.startswith(text) ][state]
def main(): # resolving the '-' issue readline.set_completer_delims(readline.get_completer_delims().replace( '-', '')) cli = CLI() if len(sys.argv) > 1: try: cli.onecmd(' '.join(sys.argv[1:])) except BadResponseError as e: print(str(e)) else: prompt_for_credentials() cli.cmdloop()
def preloop(self): try: if 'libedit' in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") else: readline.parse_and_bind("tab: complete") readline.set_completer_delims( readline.get_completer_delims().replace('/', '')) readline.set_history_length(100) readline.read_history_file( abspath(__file__)[:abspath(__file__).rindex("/")] + "/CLIMEM/NIEPMEM") return True except Exception as e: return False
def set_readline_opt(): try: delims = readline.get_completer_delims().replace("-", "^") readline.set_completer_delims(delims) # mac OS if 'libedit' in readline.__doc__: readline.parse_and_bind("bind -e") readline.parse_and_bind("bind '\t' rl_complete") else: readline.parse_and_bind("tab:complete") readline.parse_and_bind('set editing-mode vi') except: LOG.exception_err_write()
def push_completer(self, completer, history_file=None): if "readline" in sys.modules: self.__completer_stack.append(readline.get_completer()) readline.set_completer(completer) readline.set_completer_delims( readline.get_completer_delims().replace("/", "")) if len(self.__history_stack) > 0 and self.__history_stack[-1]: readline.write_history_file(self.__history_stack[-1]) self.__history_stack.append(history_file) readline.clear_history() if history_file is not None and os.path.exists(history_file): readline.read_history_file(history_file) readline.parse_and_bind(self.completekey + ": complete")
def init_readline(self): """Activates history and tab completion """ # - mainly borrowed from site.enablerlcompleter() from py3.4+ # Reading the initialization (config) file may not be enough to set a # completion key, so we set one first and then read the file. readline_doc = getattr(readline, '__doc__', '') if readline_doc is not None and 'libedit' in readline_doc: readline.parse_and_bind('bind ^I rl_complete') else: readline.parse_and_bind('tab: complete') try: readline.read_init_file() except OSError: # An OSError here could have many causes, but the most likely one # is that there's no .inputrc file (or .editrc file in the case of # Mac OS X + libedit) in the expected location. In that case, we # want to ignore the exception. pass if readline.get_current_history_length() == 0: # If no history was loaded, default to .python_history. # The guard is necessary to avoid doubling history size at # each interpreter exit when readline was already configured # see: http://bugs.python.org/issue5845#msg198636 try: readline.read_history_file(config['HISTFILE']) except IOError: pass atexit.register(readline.write_history_file, config['HISTFILE']) readline.set_history_length(config['HISTSIZE']) # - replace default completer readline.set_completer(self.improved_rlcompleter()) # - enable auto-indenting if config['AUTO_INDENT']: readline.set_pre_input_hook(self.auto_indent_hook) # - remove '/' and '~' from delimiters to help with path completion completer_delims = readline.get_completer_delims() completer_delims = completer_delims.replace('/', '') if config.get('COMPLETION_EXPANDS_TILDE'): completer_delims = completer_delims.replace('~', '') readline.set_completer_delims(completer_delims)
def setup_interactive(self): # Load the `readline` history; initialize completion. if readline is not None: path = os.path.abspath(os.path.expanduser(self.history_path)) if os.path.exists(path): readline.read_history_file(path) self.state.completer = readline.get_completer() self.state.completer_delims = readline.get_completer_delims() readline.set_completer(self.completer) readline.set_completer_delims( " \t\n`~!@#$%^&*()-=+[{]}\\|;:\'\",<.>/?") readline.parse_and_bind("tab: complete") # Display the welcome notice. intro = self.get_intro() if intro: self.ctl.out(intro)
def __init__(self): self.intro = "" \ "Welcome to the Duckietown shell.\n" \ "Version: %s\n\n" \ "Type help or ? to list commands.\n" % self.VERSION self.config_path = os.path.expanduser(DTShellConstants.ROOT) self.config_file = join(self.config_path, 'config') # define commands_path V = DTShellConstants.ENV_COMMANDS if V in os.environ: self.commands_path = os.environ[V] self.commands_path_leave_alone = True msg = 'Using path %r as prescribed by env variable %s.' % ( self.commands_path, V) print(msg) else: self.commands_path = join(self.config_path, 'commands') self.commands_path_leave_alone = False # add commands_path to the path of this session sys.path.insert(0, self.commands_path) # add third-party libraries dir to the path of this session sys.path.insert(0, join(self.commands_path, 'lib')) # create config if it does not exist if not exists(self.config_path): makedirs(self.config_path, mode=0755) if not exists(self.config_file): self.save_config() # load config self.load_config() # init commands if exists(self.commands_path) and isfile(self.commands_path): remove(self.commands_path) if not exists(self.commands_path): if not self._init_commands(): exit() # discover commands self.reload_commands() # call super constructor super(DTShell, self).__init__() # remove the char `-` from the list of word separators, this allows us to suggest flags if self.use_rawinput and self.completekey: import readline readline.set_completer_delims( readline.get_completer_delims().replace('-', '', 1))
def init_readline(complete_method, histfile=None): """Init the readline library if available.""" try: import readline readline.parse_and_bind("tab: complete") readline.set_completer(complete_method) string = readline.get_completer_delims().replace(':', '') readline.set_completer_delims(string) if histfile is not None: try: readline.read_history_file(histfile) except IOError: pass import atexit atexit.register(readline.write_history_file, histfile) except: print 'readline is not available :-('
def cmdloop(self, intro=None): # pylint: disable=too-many-branches self.preloop() if self.completekey and readline: self.old_completer = readline.get_completer() self.old_delims = readline.get_completer_delims() readline.set_completer(self.complete) readline.set_completer_delims(' \n\t') to_parse = self.completekey + ': complete' if 'libedit' in readline.__doc__: # Special case for mac OSX to_parse = 'bind ^I rl_complete' readline.parse_and_bind(to_parse) try: if intro is not None: self.intro = intro if self.intro: click.echo(self.intro, file=self.stdout) stop = None while not stop: if self.cmdqueue: line = self.cmdqueue.pop(0) else: try: line = get_input(self.get_prompt()) except EOFError: # We just want to quit here instead of changing the arg to EOF click.echo(file=self.stdout) break except KeyboardInterrupt: # We don't want to exit the shell on a keyboard interrupt click.echo(file=self.stdout) click.echo('KeyboardInterrupt', file=self.stdout) continue line = self.precmd(line) stop = self.onecmd(line) stop = self.postcmd(stop, line) finally: self.postloop() if self.completekey and readline: readline.set_completer(self.old_completer) readline.set_completer_delims(self.old_delims) # Finisher callback on the context if self.on_finished: self.on_finished(self.ctx)
def __init__(self, options, conf_dir, config_parser): Cmd.__init__(self) self.session = '' self.current_user = '' self.server = '' self.ssm = {} self.config = {} self.postcmd(False, '') # make the options available everywhere self.options = options # make the configuration file available everywhere self.config_parser = config_parser # this is used when loading and saving caches self.conf_dir = conf_dir self.history_file = os.path.join(self.conf_dir, 'history') try: # don't split on hyphens or colons during tab completion newdelims = readline.get_completer_delims() newdelims = re.sub(':|-|/', '', newdelims) readline.set_completer_delims(newdelims) if not options.nohistory: try: if os.path.isfile(self.history_file): readline.read_history_file(self.history_file) readline.set_history_length(self.HISTORY_LENGTH) # always write the history file on exit atexit.register(readline.write_history_file, self.history_file) except IOError: logging.error('Could not read history file') # pylint: disable=W0702 except Exception as exc: # pylint: disable=W0702 logging.error("Exception occurred: {}".format(exc)) sys.exit(1)
def setup_readline(self): """ Configure our tab completion settings for a context and then restore them to previous settings on exit. """ readline.parse_and_bind('tab: complete') completer_save = readline.get_completer() delims_save = readline.get_completer_delims() delims = set(delims_save) delims |= self.completer_delim_includes delims -= self.completer_delim_excludes readline.set_completer(self.completer_hook) try: readline.set_completer_delims(''.join(delims)) try: yield finally: readline.set_completer_delims(delims_save) finally: readline.set_completer(completer_save)
def custom_prompt(msg, delims="", completer=lambda: None): """Start up a prompt that with particular delims and completer""" try: orig_delims = readline.get_completer_delims() orig_completer = readline.get_completer() readline.set_completer_delims(delims) readline.set_completer(completer) try: ret = input(msg) finally: readline.set_completer_delims(orig_delims) readline.set_completer(orig_completer) return ret except EOFError: raise UserQuit()
def __init__(self, *arg, **opt): """Init history and line continuation""" self.log = True # string table for history self.history = [] # beginning of the incomplete line (line break with '\') self.save_line = '' cmd.Cmd.__init__(self, *arg, **opt) self.__initpos = os.path.abspath(os.getcwd()) # set completer delimiter delims = readline.get_completer_delims().replace("[", "") delims = delims.replace("]", "") delims = delims.replace("=", "") readline.set_completer_delims(delims)
def get_input(prompt=None, options=None, shell_client=None): if readline is not None and options is not None: completer_delims = readline.get_completer_delims() completer = readline.get_completer() readline.parse_and_bind("tab: complete") if shell_client is None: readline.set_completer_delims("") readline.set_completer(list_completer(options)) else: readline.set_completer_delims(" ") readline.set_completer(shell_completer(shell_client, options)) try: line = raw_input() if prompt is None else raw_input(prompt) return line if shell_client is None else split(line) finally: if readline is not None and options is not None: readline.set_completer_delims(completer_delims) readline.set_completer(completer)
def __init__(self, admin_cli): self.admin_cli = admin_cli try: self.prompt = Config.parser.get('shell', 'prompt') + ' ' except: self.prompt = 'katello> ' try: # don't split on hyphens during tab completion (important for completing parameters) newdelims = readline.get_completer_delims() newdelims = re.sub('-', '', newdelims) readline.set_completer_delims(newdelims) if (Config.parser.get('shell', 'nohistory').lower() != 'true'): self.__init_history() except Exception: pass self.__init_commands()
def initialize(self, region="us-west-1", host=None, port=8000, config_dir=None, session=None): """ Set up the repl for execution. """ try: import readline import rlcompleter except ImportError: # Windows doesn't have readline, so gracefully ignore. pass else: # Mac OS X readline compatibility from http://stackoverflow.com/a/7116997 if "libedit" in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") else: readline.parse_and_bind("tab: complete") # Tab-complete names with a '-' in them delims = set(readline.get_completer_delims()) if "-" in delims: delims.remove("-") readline.set_completer_delims("".join(delims)) self._conf_dir = config_dir or os.path.join( os.environ.get("HOME", "."), ".config") self.session = session or botocore.session.get_session() self.engine = FragmentEngine() self.engine.caution_callback = self.caution_callback if host is not None: self._local_endpoint = (host, port) self.engine.connect(region, session=self.session, host=host, port=port, is_secure=(host is None)) self.conf = self.load_config() for key, value in iteritems(DEFAULT_CONFIG): self.conf.setdefault(key, value) self.display = DISPLAYS[self.conf["display"]] self.throttle = TableLimits() self.throttle.load(self.conf["_throttle"])
def inspect(self, index_location, index_type, workdir='.', feature=None, interactive=False): if feature is None and not interactive: logger.error("Must either use --feature or --interactive") return if interactive: completer = SimpleCompleter([ r'\quit', 'summary', 'synonym-summary', 'context-summary', 'list-nodes', 'list-hyperedges' ]) readline.parse_and_bind("tab: complete") readline.set_completer(completer.complete) readline.set_completer_delims( readline.get_completer_delims().replace('\\', '')) try: loop = asyncio.get_event_loop() while True: try: if interactive: feature = input('feature> ') if feature == r'\quit': break if feature.strip() == '': continue index = Index.open(index_location, index_type, loop) loop.run_until_complete(index.inspect(feature, workdir)) except ArmyAntException as e: logger.error(e) except (EOFError, KeyboardInterrupt): print("\\quit") break if not interactive: break finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def setup_readline(self): ''' Initialize readline history and tab completion. ''' class SrfshCompleter(object): def __init__(self, words): self.words = words self.prefix = None def complete(self, prefix, index): if prefix != self.prefix: self.prefix = prefix # find all words that start with this prefix self.matching_words = [ w for w in self.words if w.startswith(prefix) ] if len(self.matching_words) == 0: return None if len(self.matching_words) == 1: return self.matching_words[0] # re-print the prompt w/ all of the possible word completions sys.stdout.write('\n%s\nsrfsh# %s' % (' '.join( self.matching_words), readline.get_line_buffer())) return None completer = SrfshCompleter(tuple(self.tab_complete_words)) readline.parse_and_bind("tab: complete") readline.set_completer(completer.complete) histfile = os.path.join(self.get_var('HOME'), ".srfsh_history") try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) readline.set_completer_delims(readline.get_completer_delims().replace( '-', ''))
def interact(self): if self._cmds == None: raise Exception("Console must be connected") readline.parse_and_bind('tab: complete') readline.set_completer_delims(readline.get_completer_delims().replace( '-', '')) while True: try: readline.set_completer(self._cmds.get_completer()) line = raw_input(self._cmds.prompt() + "> ") self.execute_line(line) except EOFError: print break except KeyboardInterrupt: print # skip a line continue
def test_shell_delimeters(self): """ Test shell delimieters are set without hyphens or colons during the tab completion. """ cfg_dir = "/tmp/shell/{}/conf".format(int(time.time())) m_logger = MagicMock() cpl_setter = MagicMock() with patch("spacecmd.shell.logging", m_logger) as lgr, \ patch("spacecmd.shell.readline.set_completer_delims", cpl_setter): options = MagicMock() options.nohistory = True shell = SpacewalkShell(options, cfg_dir, None) assert shell.history_file == "{}/history".format(cfg_dir) assert not m_logger.error.called assert cpl_setter.call_args[0][0] != readline.get_completer_delims() assert cpl_setter.call_args[0][0] == ' \t\n`~!@#$%^&*()=+[{]}\\|;\'",<>?'
def cmdloop_with_history(self): """ Better command loop, with history file and tweaked readline completion delimiters. """ old_completer_delims = readline.get_completer_delims() if self.histfile is not None: try: readline.read_history_file(self.histfile) except IOError: pass try: readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars))) self.cmdloop() finally: if self.histfile is not None and readline.get_current_history_length(): readline.write_history_file(self.histfile) readline.set_completer_delims(old_completer_delims)
def completer(text, state): token = readline.get_line_buffer().split()[-1] path = os.path.join(shell_current_dir, os.path.dirname(token)) if not path.endswith("/"): path += "/" if path not in shell_complete_list: populate_complete_list(path) if 'libedit' in readline.__doc__: suffix = re.sub( ".*[%s]" % (re.escape(readline.get_completer_delims())), "", text) prefix = re.sub("%s$" % (suffix), "", text) result = [ prefix + x for x in shell_complete_list[path] if x.startswith(suffix) ][state] else: result = [x for x in shell_complete_list[path] if x.startswith(text)][state] return result
def __init__(self, check_rc_file=True, stdin=None, stdout=None, log_handler=None): OverrideCmd.__init__(self, stdin=stdin, stdout=stdout) if stdin is not None: self.use_rawinput = False # No 'use_rawinput' will cause problems with the ipy command so disable it for now self.__disabled_commands__.append('ipy') if not its.on_linux: self.__hidden_commands__.append('prep_driver') self.__hidden_commands__.append('cd') self.__hidden_commands__.append('exploit') self.last_module = None self.log_handler = log_handler if self.log_handler is None: self.__disabled_commands__.append('logging') self.logger = logging.getLogger(self.__package__ + '.interpreter') self.frmwk = Framework(stdout=stdout) self.print_exception = self.frmwk.print_exception self.print_error = self.frmwk.print_error self.print_good = self.frmwk.print_good self.print_line = self.frmwk.print_line self.print_status = self.frmwk.print_status if check_rc_file: if check_rc_file: check_rc_file = self.frmwk.directories.user_data + 'console.rc' if os.path.isfile(check_rc_file) and os.access(check_rc_file, os.R_OK): self.print_status('Running commands from resource file: ' + check_rc_file) self.run_rc_file(check_rc_file) elif isinstance(check_rc_file, str): if os.path.isfile(check_rc_file) and os.access(check_rc_file, os.R_OK): self.print_status('Running commands from resource file: ' + check_rc_file) self.run_rc_file(check_rc_file) else: self.logger.error('could not access resource file: ' + check_rc_file) self.print_error('Could not access resource file: ' + check_rc_file) try: import readline readline.read_history_file(self.frmwk.directories.user_data + 'history.txt') readline.set_completer_delims(readline.get_completer_delims().replace('/', '')) except (ImportError, IOError): pass
def start(self): readline.set_completer(self._completer) readline.set_completer_delims(''.join([ c for c in readline.get_completer_delims() if c != '/' and c != '-' ])) readline.parse_and_bind("tab: complete") cmd = None while True: try: cmd = raw_input(self.PROMPT) except EOFError: print 'exit' EXIT(0) except KeyboardInterrupt: print 'type ctrl-D to exit yfs shell' continue if cmd: self._parse_cmd(cmd)
def raw_input(self, prompt): """ I'm overloading raw_input here so that I can swap out the completer before and after each line of input. This is because the completer is global. There might be a better way of doing this. TODO: I think I need to do a similar hack to fix readline history, as history currently gets munged between PDB and template-repl. """ orig_delims = readline.get_completer_delims() orig_completer = readline.get_completer() readline.set_completer(self.completer.complete) readline.set_completer_delims('') output = super(TemplateREPL, self).raw_input(prompt) readline.set_completer(orig_completer) readline.set_completer_delims(orig_delims) return output
def __init__(self, options): cmd.Cmd.__init__(self) self.__options = options if not options.isWindows(): import readline old_delims = readline.get_completer_delims() old_delims = old_delims.replace('-', '') old_delims = old_delims.replace('/', '') readline.set_completer_delims(old_delims) #ää Fix completion on mac os import rlcompleter if 'libedit' in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") else: readline.parse_and_bind("tab: complete") self.__rs = RopperService(self.__options.ropper_options, callbacks=CallbackClass(self)) self.__currentFileName = '' self.__cprinter = ConsolePrinter() self.__dataPrinter = {} self.__updatePrompt()
def complete_set(self, text, line, begidx, endidx): """ Provide completion for the "set" option Autocomplete the option name and append a "=" Autocomplete accessible random value generators available for the concerned parameter. """ try: # complete with available options - suppose only one =, no spaces if "=" in line: option_name, arg_typed = line[line.find(' ')+1:].split("=") l_val = self._module_inst.get_possible_values( option_name, arg_typed, self.get_pkg_abs_path() ) last_completer_delim_index = -1 for delim in readline.get_completer_delims(): last_completer_delim_index = max( last_completer_delim_index, arg_typed.rfind(delim) ) return [ val for val in l_val if self._module_inst.is_a_valid_value_for_this_option( option_name, arg_typed[:last_completer_delim_index+1] + val ) ] else: return [ '{}='.format(i) for i in type(self._module_inst).get_option_list() if i.startswith(text) ] except Exception: # display completion method crash message until not covered # by tests traceback.print_exc()
def _find_actions(self, subparsers, actions_module): for attr in (a for a in dir(actions_module) if a.startswith('do_')): # I prefer to be hypen-separated instead of underscores. command = attr[3:].replace('_', '-') callback = getattr(actions_module, attr) desc = callback.__doc__ or '' help = desc.strip().split('\n')[0] arguments = getattr(callback, 'arguments', []) subparser = subparsers.add_parser(command, help=help, description=desc, add_help=False, formatter_class=HelpFormatter) subparser.add_argument( '-h', '--help', action='help', help=argparse.SUPPRESS, ) self.subcommands[command] = subparser _args = [] for (args, kwargs) in arguments: for item in args: _args.append(item) subparser.add_argument(*args, **kwargs) subparser.set_defaults(func=callback) self.command_dict.setdefault(command, _args) readline.set_completer( BufferAwareCompleter(self.command_dict).complete) # Deal with special characters('-', '<', '>') DEFAULT_DELIMS = readline.get_completer_delims() DEFAULT_DELIMS = DEFAULT_DELIMS.replace('-', '') DEFAULT_DELIMS = DEFAULT_DELIMS.replace('<', '') DEFAULT_DELIMS = DEFAULT_DELIMS.replace('>', '') readline.set_completer_delims(DEFAULT_DELIMS) # Use the tab key for completion readline.parse_and_bind('tab: complete')
def run(self): """Loops and executes commands in interactive mode.""" if self._skip_delims: delims = readline.get_completer_delims() for delim in self._skip_delims: delims = delims.replace(delim, '') readline.set_completer_delims(delims) readline.parse_and_bind("tab: complete") readline.set_completer(self._completer.complete) if self._history_file: # Ensure history file exists if not os.path.isfile(self._history_file): open(self._history_file, 'w').close() readline.read_history_file(self._history_file) self._running = True try: while self._running: try: command = input(self._format_prompt()) if command: result = self.execute(*shlex.split(command)) if result: print(result) except CLIException as exc: print(exc) except (KeyboardInterrupt, EOFError): self._running = False print() except Exception as exc: if self._verbose: traceback.print_exc() else: print(exc) finally: if self._history_file: readline.write_history_file(self._history_file)