Mercurial > sqlpython
comparison cmd2.py @ 24:7a89805a47b1
load cleaned up
author | devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil |
---|---|
date | Wed, 19 Dec 2007 15:09:24 -0500 |
parents | 07aae987b754 |
children | c99853267a44 |
comparison
equal
deleted
inserted
replaced
23:07aae987b754 | 24:7a89805a47b1 |
---|---|
10 edit | 10 edit |
11 | 11 |
12 """ | 12 """ |
13 import cmd, re, os | 13 import cmd, re, os |
14 | 14 |
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 | |
15 class Cmd(cmd.Cmd): | 27 class Cmd(cmd.Cmd): |
16 caseInsensitive = True | 28 caseInsensitive = True |
17 multilineCommands = [] | 29 multilineCommands = [] |
18 continuationPrompt = '> ' | 30 continuationPrompt = '> ' |
19 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} | 31 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} |
154 if self.statementHasEnded(result[-1]): | 166 if self.statementHasEnded(result[-1]): |
155 result.append('') | 167 result.append('') |
156 | 168 |
157 def do_load(self, fname): | 169 def do_load(self, fname): |
158 """Runs command(s) from a file.""" | 170 """Runs command(s) from a file.""" |
159 stdin = self.stdin | 171 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt')) |
160 try: | 172 try: |
161 self.stdin = open(fname, 'r') | 173 self.stdin = open(fname, 'r') |
162 except IOError, e: | 174 except IOError, e: |
163 try: | 175 try: |
164 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r') | 176 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r') |
165 except: | 177 self.use_rawinput = False |
178 self.prompt = self.continuationPrompt = '' | |
179 self.cmdloop() | |
180 self.stdin.close() | |
181 self.lastcmd = '' | |
182 except IOError: | |
166 print 'Problem opening file %s: \n%s' % (fname, e) | 183 print 'Problem opening file %s: \n%s' % (fname, e) |
167 self.stdin = stdin | 184 finally: |
168 return | 185 keepstate.restore() |
169 use_rawinput = self.use_rawinput | 186 |
170 self.use_rawinput = False | |
171 self.cmdloop() | |
172 self.stdin.close() | |
173 self.stdin = stdin | |
174 self.use_rawinput = use_rawinput | |
175 self.stdin.flush() | |
176 self.lastcmd = '' | |
177 | |
178 class HistoryItem(str): | 187 class HistoryItem(str): |
179 def __init__(self, instr): | 188 def __init__(self, instr): |
180 str.__init__(self, instr) | 189 str.__init__(self, instr) |
181 self.lowercase = self.lower() | 190 self.lowercase = self.lower() |
182 self.idx = None | 191 self.idx = None |