Mercurial > sqlpython
comparison cmd2.py @ 20:d6d64c2e3b98
shortcuts
author | devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil |
---|---|
date | Wed, 19 Dec 2007 11:49:23 -0500 |
parents | 25f22e742a68 |
children | 8b55aaa52ce9 |
comparison
equal
deleted
inserted
replaced
19:25f22e742a68 | 20:d6d64c2e3b98 |
---|---|
1 import cmd, re | 1 """Variant on standard library's cmd with extra features: |
2 | 2 |
3 class HistoryItem(str): | 3 Searchable command history |
4 def __init__(self, instr): | 4 Multi-line commands |
5 str.__init__(self, instr) | 5 Case-insensitive commands |
6 self.lowercase = self.lower() | 6 Special-character shortcut commands |
7 self.idx = None | 7 """ |
8 def pr(self): | 8 import cmd, re, os |
9 print '-------------------------[%d]' % (self.idx) | |
10 print self | |
11 | |
12 class History(list): | |
13 rangeFrom = re.compile(r'^([\d])+\s*\-$') | |
14 def append(self, new): | |
15 new = HistoryItem(new) | |
16 list.append(self, new) | |
17 new.idx = len(self) | |
18 def extend(self, new): | |
19 for n in new: | |
20 self.append(n) | |
21 def get(self, getme): | |
22 try: | |
23 getme = int(getme) | |
24 if getme < 0: | |
25 return self[:(-1 * getme)] | |
26 else: | |
27 return [self[getme-1]] | |
28 except IndexError: | |
29 return [] | |
30 except (ValueError, TypeError): | |
31 getme = getme.strip() | |
32 mtch = self.rangeFrom.search(getme) | |
33 if mtch: | |
34 return self[(int(mtch.group(1))-1):] | |
35 if getme.startswith(r'/') and getme.endswith(r'/'): | |
36 finder = re.compile(getme[1:-1], re.DOTALL | re.MULTILINE | re.IGNORECASE) | |
37 def isin(hi): | |
38 return finder.search(hi) | |
39 else: | |
40 def isin(hi): | |
41 return (getme.lower() in hi.lowercase) | |
42 return [itm for itm in self if isin(itm)] | |
43 | 9 |
44 class Cmd(cmd.Cmd): | 10 class Cmd(cmd.Cmd): |
45 excludeFromHistory = '''run r list l history hi ed li'''.split() | 11 excludeFromHistory = '''run r list l history hi ed li'''.split() |
46 caseInsensitive = True | 12 caseInsensitive = True |
47 multilineCommands = [] | 13 multilineCommands = [] |
48 continuationPrompt = '> ' | 14 continuationPrompt = '> ' |
15 shortcuts = {'?': 'help', '!': 'shell'} | |
49 def __init__(self, *args, **kwargs): | 16 def __init__(self, *args, **kwargs): |
50 cmd.Cmd.__init__(self, *args, **kwargs) | 17 cmd.Cmd.__init__(self, *args, **kwargs) |
51 self.history = History() | 18 self.history = History() |
52 | 19 |
53 def precmd(self, line): | 20 def precmd(self, line): |
89 def statementHasEnded(self, lines): | 56 def statementHasEnded(self, lines): |
90 """This version lets statements end with ; or with a blank line. | 57 """This version lets statements end with ; or with a blank line. |
91 Override for your own needs.""" | 58 Override for your own needs.""" |
92 return bool(self.statementEndPattern.search(lines)) | 59 return bool(self.statementEndPattern.search(lines)) |
93 | 60 |
61 def parseline(self, line): | |
62 """Parse the line into a command name and a string containing | |
63 the arguments. Returns a tuple containing (command, args, line). | |
64 'command' and 'args' may be None if the line couldn't be parsed. | |
65 """ | |
66 line = line.strip() | |
67 if not line: | |
68 return None, None, line | |
69 shortcut = self.shortcuts.get(line[0]) | |
70 if shortcut and hasattr(self, 'do_%s' % shortcut): | |
71 line = '%s %s' % (shortcut, line[1:]) | |
72 i, n = 0, len(line) | |
73 while i < n and line[i] in self.identchars: i = i+1 | |
74 cmd, arg = line[:i], line[i:].strip() | |
75 return cmd, arg, line | |
76 | |
77 def do_shell(self, arg): | |
78 'execute a command as if at the OS prompt.' | |
79 os.system(arg) | |
80 | |
94 def do_history(self, arg): | 81 def do_history(self, arg): |
95 """history [arg]: lists past commands issued | 82 """history [arg]: lists past commands issued |
96 | 83 |
97 no arg -> list all | 84 no arg -> list all |
98 arg is integer -> list one history item, by index | 85 arg is integer -> list one history item, by index |
127 except: | 114 except: |
128 pass | 115 pass |
129 do_hi = do_history | 116 do_hi = do_history |
130 do_l = do_list | 117 do_l = do_list |
131 do_li = do_list | 118 do_li = do_list |
132 | 119 |
120 class HistoryItem(str): | |
121 def __init__(self, instr): | |
122 str.__init__(self, instr) | |
123 self.lowercase = self.lower() | |
124 self.idx = None | |
125 def pr(self): | |
126 print '-------------------------[%d]' % (self.idx) | |
127 print self | |
128 | |
129 class History(list): | |
130 rangeFrom = re.compile(r'^([\d])+\s*\-$') | |
131 def append(self, new): | |
132 new = HistoryItem(new) | |
133 list.append(self, new) | |
134 new.idx = len(self) | |
135 def extend(self, new): | |
136 for n in new: | |
137 self.append(n) | |
138 def get(self, getme): | |
139 try: | |
140 getme = int(getme) | |
141 if getme < 0: | |
142 return self[:(-1 * getme)] | |
143 else: | |
144 return [self[getme-1]] | |
145 except IndexError: | |
146 return [] | |
147 except (ValueError, TypeError): | |
148 getme = getme.strip() | |
149 mtch = self.rangeFrom.search(getme) | |
150 if mtch: | |
151 return self[(int(mtch.group(1))-1):] | |
152 if getme.startswith(r'/') and getme.endswith(r'/'): | |
153 finder = re.compile(getme[1:-1], re.DOTALL | re.MULTILINE | re.IGNORECASE) | |
154 def isin(hi): | |
155 return finder.search(hi) | |
156 else: | |
157 def isin(hi): | |
158 return (getme.lower() in hi.lowercase) | |
159 return [itm for itm in self if isin(itm)] | |
160 | |
133 | 161 |
134 | 162 |