Show More
@@ -1,120 +1,124 b'' | |||
|
1 | 1 | """ Tab completion support for a couple of linux package managers |
|
2 | 2 | |
|
3 | 3 | This is also an example of how to write custom completer plugins |
|
4 | 4 | or hooks. |
|
5 | 5 | |
|
6 | 6 | Practical use: |
|
7 | 7 | |
|
8 | 8 | [ipython]|1> import ipy_linux_package_managers |
|
9 | 9 | [ipython]|2> apt-get u<<< press tab here >>> |
|
10 | 10 | update upgrade |
|
11 | 11 | [ipython]|2> apt-get up |
|
12 | 12 | |
|
13 | 13 | """ |
|
14 | 14 | import IPython.ipapi |
|
15 | 15 | import glob,os,shlex |
|
16 | 16 | |
|
17 | 17 | ip = IPython.ipapi.get() |
|
18 | 18 | |
|
19 | 19 | def apt_completers(self, event): |
|
20 | 20 | """ This should return a list of strings with possible completions. |
|
21 | 21 | |
|
22 | 22 | Note that all the included strings that don't start with event.symbol |
|
23 | 23 | are removed, in order to not confuse readline. |
|
24 | 24 | |
|
25 | 25 | """ |
|
26 | 26 | # print event # dbg |
|
27 | 27 | |
|
28 | 28 | # commands are only suggested for the 'command' part of package manager |
|
29 | 29 | # invocation |
|
30 | 30 | |
|
31 | 31 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] |
|
32 | 32 | # print cmd |
|
33 | 33 | if cmd.endswith('apt-get') or cmd.endswith('yum'): |
|
34 | 34 | return ['update', 'upgrade', 'install', 'remove'] |
|
35 | 35 | |
|
36 | 36 | # later on, add dpkg -l / whatever to get list of possible |
|
37 | 37 | # packages, add switches etc. for the rest of command line |
|
38 | 38 | # filling |
|
39 | 39 | |
|
40 | 40 | raise IPython.ipapi.TryNext |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | # re_key specifies the regexp that triggers the specified completer |
|
44 | 44 | |
|
45 | 45 | ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get') |
|
46 | 46 | ip.set_hook('complete_command', apt_completers, re_key = '.*yum') |
|
47 | 47 | |
|
48 | 48 | pkg_cache = None |
|
49 | 49 | |
|
50 | 50 | def module_completer(self,event): |
|
51 | 51 | """ Give completions after user has typed 'import' """ |
|
52 | 52 | |
|
53 | 53 | global pkg_cache |
|
54 | 54 | import pkgutil,imp,time |
|
55 | 55 | #current = |
|
56 | 56 | if pkg_cache is None: |
|
57 | 57 | print "\n\n[Standby while scanning modules, this can take a while]\n\n" |
|
58 | 58 | pkg_cache = list(pkgutil.walk_packages()) |
|
59 | 59 | |
|
60 | already = set() | |
|
60 | 61 | for ld, name, ispkg in pkg_cache: |
|
61 | yield name | |
|
62 | if name.count('.') < event.symbol.count('.') + 1: | |
|
63 | if name not in already: | |
|
64 | already.add(name) | |
|
65 | yield name + (ispkg and '.' or '') | |
|
62 | 66 | return |
|
63 | 67 | |
|
64 | 68 | ip.set_hook('complete_command', module_completer, str_key = 'import') |
|
65 | 69 | ip.set_hook('complete_command', module_completer, str_key = 'from') |
|
66 | 70 | |
|
67 | 71 | svn_commands = """\ |
|
68 | 72 | add blame praise annotate ann cat checkout co cleanup commit ci copy |
|
69 | 73 | cp delete del remove rm diff di export help ? h import info list ls |
|
70 | 74 | lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit |
|
71 | 75 | pe propget pget pg proplist plist pl propset pset ps resolved revert |
|
72 | 76 | status stat st switch sw unlock update |
|
73 | 77 | """ |
|
74 | 78 | |
|
75 | 79 | def svn_completer(self,event): |
|
76 | 80 | if len((event.line + 'placeholder').split()) > 2: |
|
77 | 81 | # the rest are probably file names |
|
78 | 82 | return ip.IP.Completer.file_matches(event.symbol) |
|
79 | 83 | |
|
80 | 84 | return svn_commands.split() |
|
81 | 85 | |
|
82 | 86 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') |
|
83 | 87 | |
|
84 | 88 | def runlistpy(self, event): |
|
85 | 89 | comps = shlex.split(event.line) |
|
86 | 90 | relpath = (len(comps) > 1 and comps[-1] or '') |
|
87 | 91 | |
|
88 | 92 | print "rp",relpath |
|
89 | 93 | if relpath.startswith('~'): |
|
90 | 94 | relpath = os.path.expanduser(relpath) |
|
91 | 95 | dirs = [f.replace('\\','/') + "/" for f in glob.glob(relpath+'*') if os.path.isdir(f)] |
|
92 | 96 | pys = [f.replace('\\','/') for f in glob.glob(relpath+'*.py')] |
|
93 | 97 | return dirs + pys |
|
94 | 98 | |
|
95 | 99 | ip.set_hook('complete_command', runlistpy, str_key = '%run') |
|
96 | 100 | |
|
97 | 101 | def cd_completer(self, event): |
|
98 | 102 | relpath = event.symbol |
|
99 | 103 | |
|
100 | 104 | if '-b' in event.line: |
|
101 | 105 | # return only bookmark completions |
|
102 | 106 | bkms = self.db.get('bookmarks',{}) |
|
103 | 107 | return bkms.keys() |
|
104 | 108 | |
|
105 | 109 | if event.symbol == '-': |
|
106 | 110 | # jump in directory history by number |
|
107 | 111 | ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] |
|
108 | 112 | if len(ents) > 1: |
|
109 | 113 | return ents |
|
110 | 114 | return [] |
|
111 | 115 | |
|
112 | 116 | |
|
113 | 117 | if relpath.startswith('~'): |
|
114 | 118 | relpath = os.path.expanduser(relpath).replace('\\','/') |
|
115 | 119 | found = [f.replace('\\','/')+'/' for f in glob.glob(relpath+'*') if os.path.isdir(f)] |
|
116 | 120 | if not found: |
|
117 | 121 | return [relpath] |
|
118 | 122 | return found |
|
119 | 123 | |
|
120 | 124 | ip.set_hook('complete_command', cd_completer, str_key = '%cd') No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now