Python的raw_input()中的制表符补全

Python的raw_input()中的制表符补全,第1张

Python的raw_input()中的制表符补全

这是有关如何执行文件系统路径的增量完成的快速示例。我已经修改了您的示例,将其组织到一个类中,其中名为的方法

complete_[name]
表示顶级命令。

我已将完成功能切换为使用内部readline缓冲区确定整体完成的状态,这使状态逻辑更加简单。路径完成位于

_complete_path(path)
方法中,我已经连接了
额外的 命令以对其参数执行路径完成。

我相信代码可以进一步简化,但是它应该为您提供一个不错的起点:

import osimport reimport readlineCOMMANDS = ['extra', 'extension', 'stuff', 'errors', 'email', 'foobar', 'foo']RE_SPACE = re.compile('.*s+$', re.M)class Completer(object):    def _listdir(self, root):        "List directory 'root' appending the path separator to subdirs."        res = []        for name in os.listdir(root): path = os.path.join(root, name) if os.path.isdir(path):     name += os.sep res.append(name)        return res    def _complete_path(self, path=None):        "Perform completion of filesystem path."        if not path: return self._listdir('.')        dirname, rest = os.path.split(path)        tmp = dirname if dirname else '.'        res = [os.path.join(dirname, p)     for p in self._listdir(tmp) if p.startswith(rest)]        # more than one match, or single match which does not exist (typo)        if len(res) > 1 or not os.path.exists(path): return res        # resolved to a single directory, so return list of files below it        if os.path.isdir(path): return [os.path.join(path, p) for p in self._listdir(path)]        # exact file match terminates this completion        return [path + ' ']    def complete_extra(self, args):        "Completions for the 'extra' command."        if not args: return self._complete_path('.')        # treat the last arg as a path and complete it        return self._complete_path(args[-1])    def complete(self, text, state):        "Generic readline completion entry point."        buffer = readline.get_line_buffer()        line = readline.get_line_buffer().split()        # show all commands        if not line: return [c + ' ' for c in COMMANDS][state]        # account for last argument ending in a space        if RE_SPACE.match(buffer): line.append('')        # resolve command to the implementation function        cmd = line[0].strip()        if cmd in COMMANDS: impl = getattr(self, 'complete_%s' % cmd) args = line[1:] if args:     return (impl(args) + [None])[state] return [cmd + ' '][state]        results = [c + ' ' for c in COMMANDS if c.startswith(cmd)] + [None]        return results[state]comp = Completer()# we want to treat '/' as part of a word, so override the delimitersreadline.set_completer_delims(' tn;')readline.parse_and_bind("tab: complete")readline.set_completer(comp.complete)raw_input('Enter section name: ')

用法:

% python complete.py Enter section name: ext<tab>extension extraEnter section name: extra foo<tab>foo.py foo.txt foo/Enter section name: extra foo/<tab>foo/bar.txt foo/baz.txtEnter section name: extra foo/bar.txt

*如果用户键入

/
以下内容,它将从根目录 *更新 路径:

% python complete.pyEnter section name: extra /Use<tab>/Users/.localized  /Users/Shared/  /Users/user1 /Users/user2Enter section name: extra /Users/use<tab>/Users/user1  /Users/user2


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5631558.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存