comparison cmd2.py @ 27:ca6f34be3397

working on ed
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 16:22:53 -0500
parents bb3fb24b6f5f
children a6f68b25ae16
comparison
equal deleted inserted replaced
26:bb3fb24b6f5f 27:ca6f34be3397
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 Load commands from file
8 Settable environment parameters
8 9
9 still to do: 10 still to do:
10 environment (maxrows, etc.)
11 edit 11 edit
12 12 >
13 """ 13 """
14 import cmd, re, os 14 import cmd, re, os
15 15
16 class Cmd(cmd.Cmd): 16 class Cmd(cmd.Cmd):
17 caseInsensitive = True 17 caseInsensitive = True
18 multilineCommands = [] 18 multilineCommands = []
19 continuationPrompt = '> ' 19 continuationPrompt = '> '
20 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} 20 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
21 excludeFromHistory = '''run r list l history hi ed li eof'''.split() 21 excludeFromHistory = '''run r list l history hi ed li eof'''.split()
22 defaultExtension = 'txt' 22 defaultExtension = 'txt'
23 settable = ['prompt', 'continuationPrompt'] 23 defaultFileName = 'command.txt'
24 editor = os.environ.get('EDITOR') or ''
25 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor']
24 terminators = ';\n' 26 terminators = ';\n'
25 def do_cmdenvironment(self, args): 27 def do_cmdenvironment(self, args):
26 self.stdout.write(""" 28 self.stdout.write("""
27 Commands are %(casesensitive)scase-sensitive. 29 Commands are %(casesensitive)scase-sensitive.
28 Commands may be terminated with: %(terminators)s 30 Commands may be terminated with: %(terminators)s
189 pass 191 pass
190 do_hi = do_history 192 do_hi = do_history
191 do_l = do_list 193 do_l = do_list
192 do_li = do_list 194 do_li = do_list
193 195
194 def do_load(self, fname): 196 def do_ed(self, arg):
197 'ed [N]: brings up SQL from N commands ago in text editor, and puts result in SQL buffer.'
198 if not self.editor:
199 self.do_show('editor')
200 return
201 buffer = self.last_matching(arg)
202 if not buffer:
203 print 'Nothing appropriate in buffer to edit.'
204 return
205 f = open(self.defaultFileName, 'w')
206 f.write(buffer)
207 f.close()
208 os.system('%s %s' % (self.editor, self.defaultFileName))
209 self.load(self.defaultFileName)
210 do_edit = do_ed
211
212 def do_save(self, fname=None):
213 """Saves most recent command to a file."""
214
215 if fname is None:
216 fname = self.defaultFileName
217 try:
218 f = open(fname, 'w')
219 f.write(self.history[-1])
220 f.close()
221 except Exception, e:
222 print 'Error saving %s: %s' % (fname, str(e))
223
224 def do_load(self, fname=None):
195 """Runs command(s) from a file.""" 225 """Runs command(s) from a file."""
226 if fname is None:
227 fname = self.defaultFileName
196 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt')) 228 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt'))
197 try: 229 try:
198 self.stdin = open(fname, 'r') 230 self.stdin = open(fname, 'r')
199 except IOError, e: 231 except IOError, e:
200 try: 232 try: