Show More
@@ -1,90 +1,94 b'' | |||||
1 | """ Tab completion support for a couple of linux package managers |
|
1 | """ Tab completion support for a couple of linux package managers | |
2 |
|
2 | |||
3 | This is also an example of how to write custom completer plugins |
|
3 | This is also an example of how to write custom completer plugins | |
4 | or hooks. |
|
4 | or hooks. | |
5 |
|
5 | |||
6 | Practical use: |
|
6 | Practical use: | |
7 |
|
7 | |||
8 | [ipython]|1> import ipy_linux_package_managers |
|
8 | [ipython]|1> import ipy_linux_package_managers | |
9 | [ipython]|2> apt-get u<<< press tab here >>> |
|
9 | [ipython]|2> apt-get u<<< press tab here >>> | |
10 | update upgrade |
|
10 | update upgrade | |
11 | [ipython]|2> apt-get up |
|
11 | [ipython]|2> apt-get up | |
12 |
|
12 | |||
13 | """ |
|
13 | """ | |
14 | import IPython.ipapi |
|
14 | import IPython.ipapi | |
15 |
|
15 | |||
16 | ip = IPython.ipapi.get() |
|
16 | ip = IPython.ipapi.get() | |
17 |
|
17 | |||
18 | def apt_completers(self, event): |
|
18 | def apt_completers(self, event): | |
19 | """ This should return a list of strings with possible completions. |
|
19 | """ This should return a list of strings with possible completions. | |
20 |
|
20 | |||
21 | Note that all the included strings that don't start with event.symbol |
|
21 | Note that all the included strings that don't start with event.symbol | |
22 | are removed, in order to not confuse readline. |
|
22 | are removed, in order to not confuse readline. | |
23 |
|
23 | |||
24 | """ |
|
24 | """ | |
25 | # print event # dbg |
|
25 | # print event # dbg | |
26 |
|
26 | |||
27 | # commands are only suggested for the 'command' part of package manager |
|
27 | # commands are only suggested for the 'command' part of package manager | |
28 | # invocation |
|
28 | # invocation | |
29 |
|
29 | |||
30 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] |
|
30 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] | |
31 | # print cmd |
|
31 | # print cmd | |
32 | if cmd.endswith('apt-get') or cmd.endswith('yum'): |
|
32 | if cmd.endswith('apt-get') or cmd.endswith('yum'): | |
33 | return ['update', 'upgrade', 'install', 'remove'] |
|
33 | return ['update', 'upgrade', 'install', 'remove'] | |
34 |
|
34 | |||
35 | # later on, add dpkg -l / whatever to get list of possible |
|
35 | # later on, add dpkg -l / whatever to get list of possible | |
36 | # packages, add switches etc. for the rest of command line |
|
36 | # packages, add switches etc. for the rest of command line | |
37 | # filling |
|
37 | # filling | |
38 |
|
38 | |||
39 | raise IPython.ipapi.TryNext |
|
39 | raise IPython.ipapi.TryNext | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | # re_key specifies the regexp that triggers the specified completer |
|
42 | # re_key specifies the regexp that triggers the specified completer | |
43 |
|
43 | |||
44 | ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get') |
|
44 | ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get') | |
45 | ip.set_hook('complete_command', apt_completers, re_key = '.*yum') |
|
45 | ip.set_hook('complete_command', apt_completers, re_key = '.*yum') | |
46 |
|
46 | |||
47 | py_std_modules = """\ |
|
47 | py_std_modules = """\ | |
48 | BaseHTTPServer Bastion CGIHTTPServer ConfigParser Cookie |
|
48 | BaseHTTPServer Bastion CGIHTTPServer ConfigParser Cookie | |
49 | DocXMLRPCServer HTMLParser MimeWriter Queue SimpleHTTPServer |
|
49 | DocXMLRPCServer HTMLParser MimeWriter Queue SimpleHTTPServer | |
50 | SimpleXMLRPCServer SocketServer StringIO UserDict UserList UserString |
|
50 | SimpleXMLRPCServer SocketServer StringIO UserDict UserList UserString | |
51 | _LWPCookieJar _MozillaCookieJar __future__ __phello__.foo _strptime |
|
51 | _LWPCookieJar _MozillaCookieJar __future__ __phello__.foo _strptime | |
52 | _threading_local aifc anydbm asynchat asyncore atexit audiodev base64 |
|
52 | _threading_local aifc anydbm asynchat asyncore atexit audiodev base64 | |
53 | bdb binhex bisect cProfile calendar cgi cgitb chunk cmd code codecs |
|
53 | bdb binhex bisect cProfile calendar cgi cgitb chunk cmd code codecs | |
54 | codeop colorsys commands compileall contextlib cookielib copy copy_reg |
|
54 | codeop colorsys commands compileall contextlib cookielib copy copy_reg | |
55 | csv dbhash decimal difflib dircache dis doctest dumbdbm dummy_thread |
|
55 | csv dbhash decimal difflib dircache dis doctest dumbdbm dummy_thread | |
56 | dummy_threading filecmp fileinput fnmatch formatter fpformat ftplib |
|
56 | dummy_threading filecmp fileinput fnmatch formatter fpformat ftplib | |
57 | functools getopt getpass gettext glob gopherlib gzip hashlib heapq |
|
57 | functools getopt getpass gettext glob gopherlib gzip hashlib heapq | |
58 | hmac htmlentitydefs htmllib httplib ihooks imaplib imghdr imputil |
|
58 | hmac htmlentitydefs htmllib httplib ihooks imaplib imghdr imputil | |
59 | inspect keyword linecache locale macpath macurl2path mailbox mailcap |
|
59 | inspect keyword linecache locale macpath macurl2path mailbox mailcap | |
60 | markupbase md5 mhlib mimetools mimetypes mimify modulefinder multifile |
|
60 | markupbase md5 mhlib mimetools mimetypes mimify modulefinder multifile | |
61 | mutex netrc new nntplib ntpath nturl2path opcode optparse os |
|
61 | mutex netrc new nntplib ntpath nturl2path opcode optparse os | |
62 | os2emxpath pdb pickle pickletools pipes pkgutil platform popen2 poplib |
|
62 | os2emxpath pdb pickle pickletools pipes pkgutil platform popen2 poplib | |
63 | posixfile posixpath pprint profile pstats pty py_compile pyclbr pydoc |
|
63 | posixfile posixpath pprint profile pstats pty py_compile pyclbr pydoc | |
64 | quopri random re repr rexec rfc822 rlcompleter robotparser runpy sched |
|
64 | quopri random re repr rexec rfc822 rlcompleter robotparser runpy sched | |
65 | sets sgmllib sha shelve shlex shutil site smtpd smtplib sndhdr socket |
|
65 | sets sgmllib sha shelve shlex shutil site smtpd smtplib sndhdr socket | |
66 | sre sre_compile sre_constants sre_parse stat statvfs string stringold |
|
66 | sre sre_compile sre_constants sre_parse stat statvfs string stringold | |
67 | stringprep struct subprocess sunau sunaudio symbol symtable tabnanny |
|
67 | stringprep struct subprocess sunau sunaudio symbol symtable tabnanny | |
68 | tarfile telnetlib tempfile textwrap this threading timeit toaiff token |
|
68 | tarfile telnetlib tempfile textwrap this threading timeit toaiff token | |
69 | tokenize trace traceback tty types unittest urllib urllib2 urlparse |
|
69 | tokenize trace traceback tty types unittest urllib urllib2 urlparse | |
70 | user uu uuid warnings wave weakref webbrowser whichdb xdrlib xmllib |
|
70 | user uu uuid warnings wave weakref webbrowser whichdb xdrlib xmllib | |
71 | xmlrpclib zipfile""" |
|
71 | xmlrpclib zipfile""" | |
72 |
|
72 | |||
73 | def module_completer(self,event): |
|
73 | def module_completer(self,event): | |
74 | """ Give completions after user has typed 'import' """ |
|
74 | """ Give completions after user has typed 'import' """ | |
75 | return py_std_modules.split() |
|
75 | return py_std_modules.split() | |
76 |
|
76 | |||
77 | ip.set_hook('complete_command', module_completer, str_key = 'import') |
|
77 | ip.set_hook('complete_command', module_completer, str_key = 'import') | |
78 |
|
78 | |||
79 | svn_commands = """\ |
|
79 | svn_commands = """\ | |
80 | add blame praise annotate ann cat checkout co cleanup commit ci copy |
|
80 | add blame praise annotate ann cat checkout co cleanup commit ci copy | |
81 | cp delete del remove rm diff di export help ? h import info list ls |
|
81 | cp delete del remove rm diff di export help ? h import info list ls | |
82 | lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit |
|
82 | lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit | |
83 | pe propget pget pg proplist plist pl propset pset ps resolved revert |
|
83 | pe propget pget pg proplist plist pl propset pset ps resolved revert | |
84 | status stat st switch sw unlock |
|
84 | status stat st switch sw unlock update | |
85 | """ |
|
85 | """ | |
86 |
|
86 | |||
87 | def svn_completer(self,even): |
|
87 | def svn_completer(self,event): | |
|
88 | if len((event.line + 'placeholder').split()) > 2: | |||
|
89 | # the rest are probably file names | |||
|
90 | return ip.IP.Completer.file_matches(event.symbol) | |||
|
91 | ||||
88 | return svn_commands.split() |
|
92 | return svn_commands.split() | |
89 |
|
93 | |||
90 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') No newline at end of file |
|
94 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') |
General Comments 0
You need to be logged in to leave comments.
Login now