def setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
python类set_completer_delims()的实例源码
def setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
def setup(histfn=None, button='tab',verbose=1):
if histfn is None:
base = os.getenv('HOME')
if not base:
import tempfile
base = tempfile.gettempdir()
histfn = os.path.join(base, '.pythonhist')
import readline
completer = Completer()
readline.set_completer_delims('')
readline.set_completer(completer.rl_complete)
readline.parse_and_bind(button+': complete')
if histfn:
setup_readline_history(histfn)
if verbose:
print ("Welcome to rlcompleter2 %s" % __version__)
print ("for nice experiences hit <%s> multiple times"%(button))
def run(self, line):
ishellCompleter = readline.get_completer()
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
sample_file = raw_input('Please enter the filename you want to open: ')
try:
sample_fh = open(sample_file,'r')
feathermodules.samples.extend([sample.strip() for sample in sample_fh.readlines()])
sample_fh.close()
feathermodules.samples = filter(lambda x: x != '' and x != None, feathermodules.samples)
except:
print 'Something went wrong. Sorry! Please try again.'
finally:
readline.set_completer(ishellCompleter)
def run(self, line):
ishellCompleter = readline.get_completer()
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
sample_file = raw_input('Please enter the filename you want to open: ')
try:
sample_fh = open(sample_file,'r')
feathermodules.samples.append(sample_fh.read())
sample_fh.close()
feathermodules.samples = filter(lambda x: x != '' and x != None, feathermodules.samples)
except:
print 'Something went wrong. Sorry! Please try again.'
finally:
readline.set_completer(ishellCompleter)
def ask_list(label, items, alt=None, default=None):
completer = AutoCompleter(items)
readline.set_completer(completer.complete)
readline.set_completer_delims('')
readline.parse_and_bind('tab: complete')
item = None
while not item:
item = ask_string(label, default=default)
if item not in items:
if alt and item in alt:
item = items[alt.index(item)]
else:
print("Invalid entry (try pressing TAB)")
item = None
readline.set_completer(None)
return item
def get_command(self, prompt, auto_complete_fn=None):
try:
if auto_complete_fn != None:
import readline
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(auto_complete_fn)
except:
pass
# python3 changes raw input name
if sys.version_info[0] == 3:
raw_input = input
else:
raw_input = __builtins__['raw_input']
cmd = raw_input("%s" % prompt)
return cmd.strip()
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 setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
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 setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
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 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 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 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 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 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 set_path_autocomplete() -> None:
"""Enable file path autocompletion for GNU readline."""
def autocomplete(text: str, state: int) -> str:
expanded_path = os.path.expanduser(text)
if os.path.isdir(expanded_path):
possible_paths = glob.glob(os.path.join(expanded_path, "*"))
else:
possible_paths = glob.glob(expanded_path + "*")
if expanded_path != text:
possible_paths = [contract_user(path) for path in possible_paths]
possible_paths.append(None)
return possible_paths[state]
readline.parse_and_bind("tab: complete")
readline.set_completer_delims("")
readline.set_completer(autocomplete)
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 main(args):
# setup the line parser for user input
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
settings_path = home_dir / settings_file_name
if args.circleci:
install_for_circleci(settings_path)
return
if settings_path.exists() and (not args.overwrite):
raise FileExistsError(
'{FAIL}Configuration files found, these can be overwritten using the "-o" flag.{END_C}'.format(
**text_colours))
install(settings_path, basic=args.basic)
print('{BOLD}{HEADER}Configuration completed successfully.{END_C}'.format(**text_colours))
return
def read_storage_path_from_prompt():
readline.set_completer_delims(' \t\n')
readline.parse_and_bind("tab: complete")
choice = input('Set storage directory [Enter for "%s"]: ' % DEFAULT_STORAGE)
return choice or DEFAULT_STORAGE
def __init__(self, shell):
self.shell = shell
self.context = None
self.matches = None
readline.set_completer_delims(".")
def __init__(self, shell):
self.shell = shell
self.context = None
self.matches = None
readline.set_completer_delims(".")
def init_completer(self):
readline.set_pre_input_hook(self.pre_input_hook)
readline.set_completer_delims(" \t")
def __enter__(self):
self._original_completer = readline.get_completer()
self._original_delims = readline.get_completer_delims()
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
# readline can be implemented using GNU readline or libedit
# which have different configuration syntax
if 'libedit' in readline.__doc__:
readline.parse_and_bind('bind ^I rl_complete')
else:
readline.parse_and_bind('tab: complete')
def __exit__(self, unused_type, unused_value, unused_traceback):
readline.set_completer_delims(self._original_delims)
readline.set_completer(self._original_completer)
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', '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 run(self):
readline.set_completer(completer.check)
readline.set_completer_delims("")
readline.parse_and_bind("tab: complete")
def init_completer(self):
readline.set_pre_input_hook(self.pre_input_hook)
readline.set_completer_delims(" \t")
def __init__(self, filename=None, timing=False, **kwargs):
cmd.Cmd.__init__(self, **kwargs)
if 'stdin' in kwargs:
cmd.Cmd.use_rawinput = 0
self.real_stdout = self.stdout
self.smart_stdout = SmartFile(self.stdout)
self.stderr = SmartFile(sys.stderr)
self.filename = filename
self.line_num = 0
self.timing = timing
global cur_dir
cur_dir = os.getcwd()
self.prev_dir = cur_dir
self.columns = shutil.get_terminal_size().columns
self.redirect_dev = None
self.redirect_filename = ''
self.redirect_mode = ''
self.quit_when_no_output = False
self.quit_serial_reader = False
readline.set_completer_delims(DELIMS)
self.set_prompt()