comparison cmd2.py @ 134:c28ae4f75c15

incorporated changes from branch at work
author catherine@Elli.myhome.westell.com
date Sat, 08 Nov 2008 21:33:47 -0500
parents 31674148b13c
children 7c0a89fccf2b
comparison
equal deleted inserted replaced
133:31674148b13c 134:c28ae4f75c15
201 def do_shortcuts(self, args): 201 def do_shortcuts(self, args):
202 """Lists single-key shortcuts available.""" 202 """Lists single-key shortcuts available."""
203 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 203 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
204 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 204 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
205 205
206 commentRemover = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement')
207 commentRemover.suppress(pyparsing.pythonStyleComment)
206 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') } 208 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') }
207 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n')) 209 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n'))
208 ^ (pyparsing.Literal('\nEOF') + pyparsing.lineEnd))('terminator') 210 ^ (pyparsing.Literal('\nEOF') + pyparsing.lineEnd))('terminator')
209 argSeparatorPattern = pyparsing.Word(pyparsing.printables)('command') \ 211 argSeparatorPattern = pyparsing.Word(pyparsing.printables)('command') \
210 + pyparsing.SkipTo(pyparsing.StringEnd())('args') 212 + pyparsing.SkipTo(pyparsing.StringEnd())('args')
241 >>> r.statement, r.input, r.inputFrom 243 >>> r.statement, r.input, r.inputFrom
242 ('got from ', '<', 'thisfile.txt') 244 ('got from ', '<', 'thisfile.txt')
243 ''' 245 '''
244 if isinstance(s, pyparsing.ParseResults): 246 if isinstance(s, pyparsing.ParseResults):
245 return s 247 return s
246 result = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement').parseString(s) 248 result = self.commentRemover.parseString(s)
247 command = s.split()[0] 249 command = s.split()[0]
248 if self.caseInsensitive: 250 if self.caseInsensitive:
249 command = command.lower() 251 command = command.lower()
250 '''if command in self.noSpecialParse: 252 if command in self.noSpecialParse:
251 result['statement'] = result['upToIncluding'] = result['unterminated'] = result.fullStatement
252 result['command'] = command 253 result['command'] = command
253 result['args'] = ' '.join(result.fullStatement.split()[1:]) 254 result['statement'] = result.fullStatement
254 return result''' 255 return result
255 for (shortcut, fullCommand) in self.shortcuts.items(): 256 if s[0] in self.shortcuts:
256 if s.startswith(shortcut): 257 s = self.shortcuts[s[0]] + ' ' + s[1:]
257 s = s.replace(shortcut, fullCommand + ' ', 1)
258 break
259 result['statement'] = s 258 result['statement'] = s
260 result['parseable'] = s 259 result['parseable'] = s
261 terminator = self.specialTerminators.get(command) or self.terminatorPattern 260 terminator = self.specialTerminators.get(command) or self.terminatorPattern
262 result += parseSearchResults(terminator, s) 261 result += parseSearchResults(terminator, s)
263 if result.terminator: 262 if result.terminator:
347 self.history.append(statement.fullStatement) 346 self.history.append(statement.fullStatement)
348 finally: 347 finally:
349 if statekeeper: 348 if statekeeper:
350 if statement.output and not statement.outputTo: 349 if statement.output and not statement.outputTo:
351 self.stdout.seek(0) 350 self.stdout.seek(0)
352 writeToPasteBuffer(self.stdout.read()) 351 try:
352 writeToPasteBuffer(self.stdout.read())
353 except Exception, e:
354 print str(e)
353 elif statement.pipe: 355 elif statement.pipe:
354 for result in redirect.communicate(): 356 for result in redirect.communicate():
355 statekeeper.stdout.write(result or '') 357 statekeeper.stdout.write(result or '')
356 self.stdout.close() 358 self.stdout.close()
357 statekeeper.restore() 359 statekeeper.restore()