Mercurial > sqlpython
comparison cmd2.py @ 25:c99853267a44
introducing parameters
author | devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil |
---|---|
date | Wed, 19 Dec 2007 15:37:45 -0500 |
parents | 7a89805a47b1 |
children | bb3fb24b6f5f |
comparison
equal
deleted
inserted
replaced
24:7a89805a47b1 | 25:c99853267a44 |
---|---|
2 | 2 |
3 Searchable command history | 3 Searchable command history |
4 Multi-line commands | 4 Multi-line commands |
5 Case-insensitive commands | 5 Case-insensitive commands |
6 Special-character shortcut commands | 6 Special-character shortcut commands |
7 Load commands from file | |
7 | 8 |
8 still to do: | 9 still to do: |
9 environment (maxrows, etc.) | 10 environment (maxrows, etc.) |
10 edit | 11 edit |
11 | 12 |
12 """ | 13 """ |
13 import cmd, re, os | 14 import cmd, re, os |
14 | 15 |
15 class Statekeeper(object): | |
16 def __init__(self, obj, attribs): | |
17 self.obj = obj | |
18 self.attribs = attribs | |
19 self.save() | |
20 def save(self): | |
21 for attrib in self.attribs: | |
22 setattr(self, attrib, getattr(self.obj, attrib)) | |
23 def restore(self): | |
24 for attrib in self.attribs: | |
25 setattr(self.obj, attrib, getattr(self, attrib)) | |
26 | |
27 class Cmd(cmd.Cmd): | 16 class Cmd(cmd.Cmd): |
28 caseInsensitive = True | 17 caseInsensitive = True |
29 multilineCommands = [] | 18 multilineCommands = [] |
30 continuationPrompt = '> ' | 19 continuationPrompt = '> ' |
31 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} | 20 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} |
32 excludeFromHistory = '''run r list l history hi ed li eof'''.split() | 21 excludeFromHistory = '''run r list l history hi ed li eof'''.split() |
33 defaultExtension = 'txt' | 22 defaultExtension = 'txt' |
23 settable = [] | |
24 terminators = r';\n' | |
25 def do_cmdenvironment(self, args): | |
26 self.stdout.write(""" | |
27 Commands are %(casesensitive)scase-sensitive. | |
28 Commands may be terminated with: %(terminators)s | |
29 Settable parameters: %(settable)s | |
30 """ % | |
31 { 'casesensitive': 'not ' if self.caseInsensitive else '', | |
32 'terminators': ' '.join(self.terminators), | |
33 'settable': ' '.join(self.settable) | |
34 }) | |
35 | |
34 def __init__(self, *args, **kwargs): | 36 def __init__(self, *args, **kwargs): |
35 cmd.Cmd.__init__(self, *args, **kwargs) | 37 cmd.Cmd.__init__(self, *args, **kwargs) |
36 self.history = History() | 38 self.history = History() |
37 | 39 |
38 def precmd(self, line): | 40 def precmd(self, line): |
90 | 92 |
91 def do_EOF(self, arg): | 93 def do_EOF(self, arg): |
92 return True | 94 return True |
93 do_eof = do_EOF | 95 do_eof = do_EOF |
94 | 96 |
95 statementEndPattern = re.compile(r'[\n;]\s*$') | 97 statementEndPattern = re.compile(r'[%s]\s*$' % self.terminators) |
96 def statementHasEnded(self, lines): | 98 def statementHasEnded(self, lines): |
97 """This version lets statements end with ; or with a blank line. | |
98 Override for your own needs.""" | |
99 return bool(self.statementEndPattern.search(lines)) | 99 return bool(self.statementEndPattern.search(lines)) |
100 | 100 |
101 def clean(self, s): | |
102 """cleans up a string""" | |
103 if self.caseInsensitive: | |
104 return s.strip().lower() | |
105 return s.strip() | |
106 | |
101 def parseline(self, line): | 107 def parseline(self, line): |
102 """Parse the line into a command name and a string containing | 108 """Parse the line into a command name and a string containing |
103 the arguments. Returns a tuple containing (command, args, line). | 109 the arguments. Returns a tuple containing (command, args, line). |
104 'command' and 'args' may be None if the line couldn't be parsed. | 110 'command' and 'args' may be None if the line couldn't be parsed. |
105 """ | 111 """ |
112 i, n = 0, len(line) | 118 i, n = 0, len(line) |
113 while i < n and line[i] in self.identchars: i = i+1 | 119 while i < n and line[i] in self.identchars: i = i+1 |
114 cmd, arg = line[:i], line[i:].strip() | 120 cmd, arg = line[:i], line[i:].strip() |
115 return cmd, arg, line | 121 return cmd, arg, line |
116 | 122 |
123 def showParam(self, param): | |
124 param = self.clean(param) | |
125 if param in self.settable: | |
126 val = getattr(self, param) | |
127 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param)))) | |
128 | |
129 def do_show(self, arg): | |
130 'Shows value of a parameter' | |
131 if arg.strip(): | |
132 self.showParam(arg) | |
133 else: | |
134 for param in self.settable: | |
135 self.showParam(param) | |
136 | |
137 def cast(self, current, new): | |
138 typ = type(current) | |
139 if typ == bool: | |
140 new = new.lower() | |
141 try: | |
142 if (new=='on') or (new[0] in ('y','t')): | |
143 return True | |
144 return False | |
145 except TypeError: | |
146 None | |
147 try: | |
148 return typ(new) | |
149 except: | |
150 print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new) | |
151 return current | |
152 | |
153 def do_set(self, arg): | |
154 'Sets a parameter' | |
155 try: | |
156 paramName, val = arg.split(None, 1) | |
157 paramName = self.clean(paramName) | |
158 if paramName not in self.settable: | |
159 raise NotSettableError | |
160 currentVal = getattr(self, paramName) | |
161 val = self.cast(currentVal, val.strip(self.terminators)) | |
162 setattr(self, paramName, val) | |
163 self.stdout.write(paramName, ' - was: %s\nnow: %s\n' % (currentVal, val)) | |
164 except ValueError, AttributeError, NotSettableError: | |
165 self.do_show(arg) | |
166 | |
117 def do_shell(self, arg): | 167 def do_shell(self, arg): |
118 'execute a command as if at the OS prompt.' | 168 'execute a command as if at the OS prompt.' |
119 os.system(arg) | 169 os.system(arg) |
120 | 170 |
121 def do_history(self, arg): | 171 def do_history(self, arg): |
222 else: | 272 else: |
223 def isin(hi): | 273 def isin(hi): |
224 return (getme.lower() in hi.lowercase) | 274 return (getme.lower() in hi.lowercase) |
225 return [itm for itm in self if isin(itm)] | 275 return [itm for itm in self if isin(itm)] |
226 | 276 |
227 | 277 class NotSettableError(Exception): |
228 | 278 None |
279 | |
280 class Statekeeper(object): | |
281 def __init__(self, obj, attribs): | |
282 self.obj = obj | |
283 self.attribs = attribs | |
284 self.save() | |
285 def save(self): | |
286 for attrib in self.attribs: | |
287 setattr(self, attrib, getattr(self.obj, attrib)) | |
288 def restore(self): | |
289 for attrib in self.attribs: | |
290 setattr(self.obj, attrib, getattr(self, attrib)) |