comparison cmd2.py @ 427:3c17dae50252

use packrat
author catherine.devlin@gmail.com
date Thu, 28 Jul 2011 17:06:46 -0400
parents 05a44bf4b449
children 47301bf13ec3
comparison
equal deleted inserted replaced
426:05a44bf4b449 427:3c17dae50252
39 import platform 39 import platform
40 import copy 40 import copy
41 from code import InteractiveConsole, InteractiveInterpreter 41 from code import InteractiveConsole, InteractiveInterpreter
42 from optparse import make_option 42 from optparse import make_option
43 import pyparsing 43 import pyparsing
44 pyparsing.ParserElement.enablePackrat()
44 45
45 __version__ = '0.6.3' 46 __version__ = '0.6.3'
46 47
47 class OptionParser(optparse.OptionParser): 48 class OptionParser(optparse.OptionParser):
48 def exit(self, status=0, msg=None): 49 def exit(self, status=0, msg=None):
249 new.parser = self.parser 250 new.parser = self.parser
250 new.parsed['args'] = newargs 251 new.parsed['args'] = newargs
251 new.parsed.statement['args'] = newargs 252 new.parsed.statement['args'] = newargs
252 return new 253 return new
253 254
254 class SkipToLast(pyparsing.SkipTo):
255 def parseImpl( self, instring, loc, doActions=True ):
256 self.original_includeMatch = self.includeMatch
257 self.includeMatch = True
258 original_loc = loc
259 oldpos = loc
260 accumulated = []
261 while True:
262 try:
263 res = pyparsing.SkipTo.parseImpl(self, instring, loc, False)
264 oldpos = loc
265 accumulated.append(res)
266 loc = res[0]
267 except pyparsing.ParseException:
268 self.includeMatch = self.original_includeMatch
269 res = pyparsing.SkipTo.parseImpl(self, instring, oldpos, doActions)
270 newres = list(res)
271 newres[0] = res[0] + sum([r[0] for r in accumulated[:-1]])
272 newres[1][0] = res[1][0] + ''.join([r[1][0] for r in accumulated[:-1]])
273 return tuple(newres)
274
275 class StubbornDict(dict): 255 class StubbornDict(dict):
276 '''Dictionary that tolerates many input formats. 256 '''Dictionary that tolerates many input formats.
277 Create it with stubbornDict(arg) factory function. 257 Create it with stubbornDict(arg) factory function.
278 258
279 >>> d = StubbornDict(large='gross', small='klein') 259 >>> d = StubbornDict(large='gross', small='klein')