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