Show More
The requested changes are too big and content was truncated. Show full diff
@@ -0,0 +1,226 b'' | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | ||||
|
3 | """ Implementations for various useful completers | |||
|
4 | ||||
|
5 | See Extensions/ipy_stock_completers.py on examples of how to enable a completer, | |||
|
6 | but the basic idea is to do: | |||
|
7 | ||||
|
8 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') | |||
|
9 | ||||
|
10 | """ | |||
|
11 | ||||
|
12 | import IPython.ipapi | |||
|
13 | import glob,os,shlex,sys | |||
|
14 | ||||
|
15 | ip = IPython.ipapi.get() | |||
|
16 | ||||
|
17 | def vcs_completer(commands, event): | |||
|
18 | """ utility to make writing typical version control app completers easier | |||
|
19 | ||||
|
20 | VCS command line apps typically have the format: | |||
|
21 | ||||
|
22 | [sudo ]PROGNAME [help] [command] file file... | |||
|
23 | ||||
|
24 | """ | |||
|
25 | ||||
|
26 | ||||
|
27 | cmd_param = event.line.split() | |||
|
28 | if event.line.endswith(' '): | |||
|
29 | cmd_param.append('') | |||
|
30 | ||||
|
31 | if cmd_param[0] == 'sudo': | |||
|
32 | cmd_param = cmd_param[1:] | |||
|
33 | ||||
|
34 | if len(cmd_param) == 2 or 'help' in cmd_param: | |||
|
35 | return commands.split() | |||
|
36 | ||||
|
37 | return ip.IP.Completer.file_matches(event.symbol) | |||
|
38 | ||||
|
39 | ||||
|
40 | ||||
|
41 | def apt_completers(self, event): | |||
|
42 | """ This should return a list of strings with possible completions. | |||
|
43 | ||||
|
44 | Note that all the included strings that don't start with event.symbol | |||
|
45 | are removed, in order to not confuse readline. | |||
|
46 | ||||
|
47 | """ | |||
|
48 | # print event # dbg | |||
|
49 | ||||
|
50 | # commands are only suggested for the 'command' part of package manager | |||
|
51 | # invocation | |||
|
52 | ||||
|
53 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] | |||
|
54 | # print cmd | |||
|
55 | if cmd.endswith('apt-get') or cmd.endswith('yum'): | |||
|
56 | return ['update', 'upgrade', 'install', 'remove'] | |||
|
57 | ||||
|
58 | # later on, add dpkg -l / whatever to get list of possible | |||
|
59 | # packages, add switches etc. for the rest of command line | |||
|
60 | # filling | |||
|
61 | ||||
|
62 | raise IPython.ipapi.TryNext | |||
|
63 | ||||
|
64 | ||||
|
65 | ||||
|
66 | pkg_cache = None | |||
|
67 | ||||
|
68 | def module_completer(self,event): | |||
|
69 | """ Give completions after user has typed 'import'. | |||
|
70 | ||||
|
71 | Note that only possible completions in the local directory are returned.""" | |||
|
72 | ||||
|
73 | # This works in all versions of python. While 2.5 has | |||
|
74 | # pkgutil.walk_packages(), that particular routine is fairly dangerous, | |||
|
75 | # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full | |||
|
76 | # of possibly problematic side effects. At some point we may implement | |||
|
77 | # something that searches sys.path in a saner/safer way, but for now we'll | |||
|
78 | # restrict ourselves to local completions only. | |||
|
79 | for el in [f[:-3] for f in glob.glob("*.py")]: | |||
|
80 | yield el | |||
|
81 | return | |||
|
82 | ||||
|
83 | ||||
|
84 | svn_commands = """\ | |||
|
85 | add blame praise annotate ann cat checkout co cleanup commit ci copy | |||
|
86 | cp delete del remove rm diff di export help ? h import info list ls | |||
|
87 | lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit | |||
|
88 | pe propget pget pg proplist plist pl propset pset ps resolved revert | |||
|
89 | status stat st switch sw unlock update | |||
|
90 | """ | |||
|
91 | ||||
|
92 | def svn_completer(self,event): | |||
|
93 | return vcs_completer(svn_commands, event) | |||
|
94 | ||||
|
95 | ||||
|
96 | hg_commands = """ | |||
|
97 | add addremove annotate archive backout branch branches bundle cat | |||
|
98 | clone commit copy diff export grep heads help identify import incoming | |||
|
99 | init locate log manifest merge outgoing parents paths pull push | |||
|
100 | qapplied qclone qcommit qdelete qdiff qfold qguard qheader qimport | |||
|
101 | qinit qnew qnext qpop qprev qpush qrefresh qrename qrestore qsave | |||
|
102 | qselect qseries qtop qunapplied recover remove rename revert rollback | |||
|
103 | root serve showconfig status strip tag tags tip unbundle update verify | |||
|
104 | version | |||
|
105 | """ | |||
|
106 | ||||
|
107 | def hg_completer(self,event): | |||
|
108 | """ Completer for mercurial commands """ | |||
|
109 | ||||
|
110 | return vcs_completer(hg_commands, event) | |||
|
111 | ||||
|
112 | ||||
|
113 | ||||
|
114 | bzr_commands = """ | |||
|
115 | add annotate bind branch break-lock bundle-revisions cat check | |||
|
116 | checkout commit conflicts deleted diff export gannotate gbranch | |||
|
117 | gcommit gdiff help ignore ignored info init init-repository inventory | |||
|
118 | log merge missing mkdir mv nick pull push reconcile register-branch | |||
|
119 | remerge remove renames resolve revert revno root serve sign-my-commits | |||
|
120 | status testament unbind uncommit unknowns update upgrade version | |||
|
121 | version-info visualise whoami | |||
|
122 | """ | |||
|
123 | ||||
|
124 | def bzr_completer(self,event): | |||
|
125 | """ Completer for bazaar commands """ | |||
|
126 | cmd_param = event.line.split() | |||
|
127 | if event.line.endswith(' '): | |||
|
128 | cmd_param.append('') | |||
|
129 | ||||
|
130 | if len(cmd_param) > 2: | |||
|
131 | cmd = cmd_param[1] | |||
|
132 | param = cmd_param[-1] | |||
|
133 | output_file = (param == '--output=') | |||
|
134 | if cmd == 'help': | |||
|
135 | return bzr_commands.split() | |||
|
136 | elif cmd in ['bundle-revisions','conflicts', | |||
|
137 | 'deleted','nick','register-branch', | |||
|
138 | 'serve','unbind','upgrade','version', | |||
|
139 | 'whoami'] and not output_file: | |||
|
140 | return [] | |||
|
141 | else: | |||
|
142 | # the rest are probably file names | |||
|
143 | return ip.IP.Completer.file_matches(event.symbol) | |||
|
144 | ||||
|
145 | return bzr_commands.split() | |||
|
146 | ||||
|
147 | ||||
|
148 | def shlex_split(x): | |||
|
149 | """Helper function to split lines into segments.""" | |||
|
150 | #shlex.split raise exception if syntax error in sh syntax | |||
|
151 | #for example if no closing " is found. This function keeps dropping | |||
|
152 | #the last character of the line until shlex.split does not raise | |||
|
153 | #exception. Adds end of the line to the result of shlex.split | |||
|
154 | #example: %run "c:/python -> ['%run','"c:/python'] | |||
|
155 | endofline=[] | |||
|
156 | while x!="": | |||
|
157 | try: | |||
|
158 | comps=shlex.split(x) | |||
|
159 | if len(endofline)>=1: | |||
|
160 | comps.append("".join(endofline)) | |||
|
161 | return comps | |||
|
162 | except ValueError: | |||
|
163 | endofline=[x[-1:]]+endofline | |||
|
164 | x=x[:-1] | |||
|
165 | return ["".join(endofline)] | |||
|
166 | ||||
|
167 | def runlistpy(self, event): | |||
|
168 | comps = shlex_split(event.line) | |||
|
169 | relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") | |||
|
170 | ||||
|
171 | #print "\nev=",event # dbg | |||
|
172 | #print "rp=",relpath # dbg | |||
|
173 | #print 'comps=',comps # dbg | |||
|
174 | ||||
|
175 | lglob = glob.glob | |||
|
176 | isdir = os.path.isdir | |||
|
177 | if relpath.startswith('~'): | |||
|
178 | relpath = os.path.expanduser(relpath) | |||
|
179 | dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') | |||
|
180 | if isdir(f)] | |||
|
181 | ||||
|
182 | # Find if the user has already typed the first filename, after which we | |||
|
183 | # should complete on all files, since after the first one other files may | |||
|
184 | # be arguments to the input script. | |||
|
185 | #filter( | |||
|
186 | if filter(lambda f: f.endswith('.py') or f.endswith('.ipy'),comps): | |||
|
187 | pys = [f.replace('\\','/') for f in lglob('*')] | |||
|
188 | else: | |||
|
189 | pys = [f.replace('\\','/') | |||
|
190 | for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy')] | |||
|
191 | return dirs + pys | |||
|
192 | ||||
|
193 | ||||
|
194 | def cd_completer(self, event): | |||
|
195 | relpath = event.symbol | |||
|
196 | #print event # dbg | |||
|
197 | if '-b' in event.line: | |||
|
198 | # return only bookmark completions | |||
|
199 | bkms = self.db.get('bookmarks',{}) | |||
|
200 | return bkms.keys() | |||
|
201 | ||||
|
202 | ||||
|
203 | if event.symbol == '-': | |||
|
204 | # jump in directory history by number | |||
|
205 | ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] | |||
|
206 | if len(ents) > 1: | |||
|
207 | return ents | |||
|
208 | return [] | |||
|
209 | ||||
|
210 | if relpath.startswith('~'): | |||
|
211 | relpath = os.path.expanduser(relpath).replace('\\','/') | |||
|
212 | found = [] | |||
|
213 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') | |||
|
214 | if os.path.isdir(f)]: | |||
|
215 | if ' ' in d: | |||
|
216 | # we don't want to deal with any of that, complex code | |||
|
217 | # for this is elsewhere | |||
|
218 | raise IPython.ipapi.TryNext | |||
|
219 | found.append( d ) | |||
|
220 | ||||
|
221 | if not found: | |||
|
222 | if os.path.isdir(relpath): | |||
|
223 | return [relpath] | |||
|
224 | raise IPython.ipapi.TryNext | |||
|
225 | return found | |||
|
226 |
@@ -1,239 +1,30 b'' | |||||
1 | """ Tab completion support for a couple of linux package managers |
|
1 | """ Install various IPython completers | |
2 |
|
2 | |||
3 | This is also an example of how to write custom completer plugins |
|
3 | IPython extension that installs most of the implemented | |
4 | or hooks. |
|
4 | custom completers. | |
5 |
|
5 | |||
6 | Practical use: |
|
6 | The actual implementations are in Extensions/ipy_completers.py | |
7 |
|
||||
8 | [ipython]|1> import ipy_linux_package_managers |
|
|||
9 | [ipython]|2> apt-get u<<< press tab here >>> |
|
|||
10 | update upgrade |
|
|||
11 | [ipython]|2> apt-get up |
|
|||
12 |
|
7 | |||
13 | """ |
|
8 | """ | |
14 | import IPython.ipapi |
|
9 | import IPython.ipapi | |
15 | import glob,os,shlex,sys |
|
|||
16 |
|
10 | |||
17 | ip = IPython.ipapi.get() |
|
11 | ip = IPython.ipapi.get() | |
18 |
|
12 | |||
19 | def vcs_completer(commands, event): |
|
13 | from ipy_completers import * | |
20 | """ utility to make writing typical version control app completers easier |
|
|||
21 |
|
||||
22 | VCS command line apps typically have the format: |
|
|||
23 |
|
||||
24 | [sudo ]PROGNAME [help] [command] file file... |
|
|||
25 |
|
||||
26 | """ |
|
|||
27 |
|
||||
28 |
|
||||
29 | cmd_param = event.line.split() |
|
|||
30 | if event.line.endswith(' '): |
|
|||
31 | cmd_param.append('') |
|
|||
32 |
|
||||
33 | if cmd_param[0] == 'sudo': |
|
|||
34 | cmd_param = cmd_param[1:] |
|
|||
35 |
|
||||
36 | if len(cmd_param) == 2 or 'help' in cmd_param: |
|
|||
37 | return commands.split() |
|
|||
38 |
|
||||
39 | return ip.IP.Completer.file_matches(event.symbol) |
|
|||
40 |
|
||||
41 |
|
||||
42 |
|
||||
43 | def apt_completers(self, event): |
|
|||
44 | """ This should return a list of strings with possible completions. |
|
|||
45 |
|
||||
46 | Note that all the included strings that don't start with event.symbol |
|
|||
47 | are removed, in order to not confuse readline. |
|
|||
48 |
|
||||
49 | """ |
|
|||
50 | # print event # dbg |
|
|||
51 |
|
||||
52 | # commands are only suggested for the 'command' part of package manager |
|
|||
53 | # invocation |
|
|||
54 |
|
||||
55 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] |
|
|||
56 | # print cmd |
|
|||
57 | if cmd.endswith('apt-get') or cmd.endswith('yum'): |
|
|||
58 | return ['update', 'upgrade', 'install', 'remove'] |
|
|||
59 |
|
||||
60 | # later on, add dpkg -l / whatever to get list of possible |
|
|||
61 | # packages, add switches etc. for the rest of command line |
|
|||
62 | # filling |
|
|||
63 |
|
||||
64 | raise IPython.ipapi.TryNext |
|
|||
65 |
|
||||
66 |
|
||||
67 | # re_key specifies the regexp that triggers the specified completer |
|
|||
68 |
|
14 | |||
69 | ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get') |
|
15 | ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get') | |
70 | ip.set_hook('complete_command', apt_completers, re_key = '.*yum') |
|
16 | ip.set_hook('complete_command', apt_completers, re_key = '.*yum') | |
71 |
|
17 | |||
72 | pkg_cache = None |
|
|||
73 |
|
||||
74 | def module_completer(self,event): |
|
|||
75 | """ Give completions after user has typed 'import'. |
|
|||
76 |
|
||||
77 | Note that only possible completions in the local directory are returned.""" |
|
|||
78 |
|
||||
79 | # This works in all versions of python. While 2.5 has |
|
|||
80 | # pkgutil.walk_packages(), that particular routine is fairly dangerous, |
|
|||
81 | # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full |
|
|||
82 | # of possibly problematic side effects. At some point we may implement |
|
|||
83 | # something that searches sys.path in a saner/safer way, but for now we'll |
|
|||
84 | # restrict ourselves to local completions only. |
|
|||
85 | for el in [f[:-3] for f in glob.glob("*.py")]: |
|
|||
86 | yield el |
|
|||
87 | return |
|
|||
88 |
|
18 | |||
89 | ip.set_hook('complete_command', module_completer, str_key = 'import') |
|
19 | ip.set_hook('complete_command', module_completer, str_key = 'import') | |
90 | ip.set_hook('complete_command', module_completer, str_key = 'from') |
|
20 | ip.set_hook('complete_command', module_completer, str_key = 'from') | |
91 |
|
21 | |||
92 | svn_commands = """\ |
|
|||
93 | add blame praise annotate ann cat checkout co cleanup commit ci copy |
|
|||
94 | cp delete del remove rm diff di export help ? h import info list ls |
|
|||
95 | lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit |
|
|||
96 | pe propget pget pg proplist plist pl propset pset ps resolved revert |
|
|||
97 | status stat st switch sw unlock update |
|
|||
98 | """ |
|
|||
99 |
|
||||
100 | def svn_completer(self,event): |
|
|||
101 | return vcs_completer(svn_commands, event) |
|
|||
102 |
|
||||
103 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') |
|
22 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') | |
104 |
|
23 | |||
105 | hg_commands = """ |
|
|||
106 | add addremove annotate archive backout branch branches bundle cat |
|
|||
107 | clone commit copy diff export grep heads help identify import incoming |
|
|||
108 | init locate log manifest merge outgoing parents paths pull push |
|
|||
109 | qapplied qclone qcommit qdelete qdiff qfold qguard qheader qimport |
|
|||
110 | qinit qnew qnext qpop qprev qpush qrefresh qrename qrestore qsave |
|
|||
111 | qselect qseries qtop qunapplied recover remove rename revert rollback |
|
|||
112 | root serve showconfig status strip tag tags tip unbundle update verify |
|
|||
113 | version |
|
|||
114 | """ |
|
|||
115 |
|
||||
116 | def hg_completer(self,event): |
|
|||
117 | """ Completer for mercurial commands """ |
|
|||
118 |
|
||||
119 | return vcs_completer(hg_commands, event) |
|
|||
120 |
|
||||
121 | ip.set_hook('complete_command', hg_completer, str_key = 'hg') |
|
24 | ip.set_hook('complete_command', hg_completer, str_key = 'hg') | |
122 |
|
25 | |||
123 |
|
||||
124 | bzr_commands = """ |
|
|||
125 | add annotate bind branch break-lock bundle-revisions cat check |
|
|||
126 | checkout commit conflicts deleted diff export gannotate gbranch |
|
|||
127 | gcommit gdiff help ignore ignored info init init-repository inventory |
|
|||
128 | log merge missing mkdir mv nick pull push reconcile register-branch |
|
|||
129 | remerge remove renames resolve revert revno root serve sign-my-commits |
|
|||
130 | status testament unbind uncommit unknowns update upgrade version |
|
|||
131 | version-info visualise whoami |
|
|||
132 | """ |
|
|||
133 |
|
||||
134 | def bzr_completer(self,event): |
|
|||
135 | """ Completer for bazaar commands """ |
|
|||
136 | cmd_param = event.line.split() |
|
|||
137 | if event.line.endswith(' '): |
|
|||
138 | cmd_param.append('') |
|
|||
139 |
|
||||
140 | if len(cmd_param) > 2: |
|
|||
141 | cmd = cmd_param[1] |
|
|||
142 | param = cmd_param[-1] |
|
|||
143 | output_file = (param == '--output=') |
|
|||
144 | if cmd == 'help': |
|
|||
145 | return bzr_commands.split() |
|
|||
146 | elif cmd in ['bundle-revisions','conflicts', |
|
|||
147 | 'deleted','nick','register-branch', |
|
|||
148 | 'serve','unbind','upgrade','version', |
|
|||
149 | 'whoami'] and not output_file: |
|
|||
150 | return [] |
|
|||
151 | else: |
|
|||
152 | # the rest are probably file names |
|
|||
153 | return ip.IP.Completer.file_matches(event.symbol) |
|
|||
154 |
|
||||
155 | return bzr_commands.split() |
|
|||
156 |
|
||||
157 | ip.set_hook('complete_command', bzr_completer, str_key = 'bzr') |
|
26 | ip.set_hook('complete_command', bzr_completer, str_key = 'bzr') | |
158 |
|
27 | |||
159 | def shlex_split(x): |
|
|||
160 | """Helper function to split lines into segments.""" |
|
|||
161 | #shlex.split raise exception if syntax error in sh syntax |
|
|||
162 | #for example if no closing " is found. This function keeps dropping |
|
|||
163 | #the last character of the line until shlex.split does not raise |
|
|||
164 | #exception. Adds end of the line to the result of shlex.split |
|
|||
165 | #example: %run "c:/python -> ['%run','"c:/python'] |
|
|||
166 | endofline=[] |
|
|||
167 | while x!="": |
|
|||
168 | try: |
|
|||
169 | comps=shlex.split(x) |
|
|||
170 | if len(endofline)>=1: |
|
|||
171 | comps.append("".join(endofline)) |
|
|||
172 | return comps |
|
|||
173 | except ValueError: |
|
|||
174 | endofline=[x[-1:]]+endofline |
|
|||
175 | x=x[:-1] |
|
|||
176 | return ["".join(endofline)] |
|
|||
177 |
|
||||
178 | def runlistpy(self, event): |
|
|||
179 | comps = shlex_split(event.line) |
|
|||
180 | relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") |
|
|||
181 |
|
||||
182 | #print "\nev=",event # dbg |
|
|||
183 | #print "rp=",relpath # dbg |
|
|||
184 | #print 'comps=',comps # dbg |
|
|||
185 |
|
||||
186 | lglob = glob.glob |
|
|||
187 | isdir = os.path.isdir |
|
|||
188 | if relpath.startswith('~'): |
|
|||
189 | relpath = os.path.expanduser(relpath) |
|
|||
190 | dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') |
|
|||
191 | if isdir(f)] |
|
|||
192 |
|
||||
193 | # Find if the user has already typed the first filename, after which we |
|
|||
194 | # should complete on all files, since after the first one other files may |
|
|||
195 | # be arguments to the input script. |
|
|||
196 | #filter( |
|
|||
197 | if filter(lambda f: f.endswith('.py') or f.endswith('.ipy'),comps): |
|
|||
198 | pys = [f.replace('\\','/') for f in lglob('*')] |
|
|||
199 | else: |
|
|||
200 | pys = [f.replace('\\','/') |
|
|||
201 | for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy')] |
|
|||
202 | return dirs + pys |
|
|||
203 |
|
||||
204 | ip.set_hook('complete_command', runlistpy, str_key = '%run') |
|
28 | ip.set_hook('complete_command', runlistpy, str_key = '%run') | |
205 |
|
29 | |||
206 | def cd_completer(self, event): |
|
|||
207 | relpath = event.symbol |
|
|||
208 | #print event # dbg |
|
|||
209 | if '-b' in event.line: |
|
|||
210 | # return only bookmark completions |
|
|||
211 | bkms = self.db.get('bookmarks',{}) |
|
|||
212 | return bkms.keys() |
|
|||
213 |
|
||||
214 |
|
||||
215 | if event.symbol == '-': |
|
|||
216 | # jump in directory history by number |
|
|||
217 | ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] |
|
|||
218 | if len(ents) > 1: |
|
|||
219 | return ents |
|
|||
220 | return [] |
|
|||
221 |
|
||||
222 | if relpath.startswith('~'): |
|
|||
223 | relpath = os.path.expanduser(relpath).replace('\\','/') |
|
|||
224 | found = [] |
|
|||
225 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') |
|
|||
226 | if os.path.isdir(f)]: |
|
|||
227 | if ' ' in d: |
|
|||
228 | # we don't want to deal with any of that, complex code |
|
|||
229 | # for this is elsewhere |
|
|||
230 | raise IPython.ipapi.TryNext |
|
|||
231 | found.append( d ) |
|
|||
232 |
|
||||
233 | if not found: |
|
|||
234 | if os.path.isdir(relpath): |
|
|||
235 | return [relpath] |
|
|||
236 | raise IPython.ipapi.TryNext |
|
|||
237 | return found |
|
|||
238 |
|
||||
239 | ip.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
30 | ip.set_hook('complete_command', cd_completer, str_key = '%cd') |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now