Mercurial > python-cmd2
comparison cmd2.py @ 203:68a609fc516b
limit < infile slurp to cases where infile found
author | catherine@dellzilla |
---|---|
date | Mon, 02 Mar 2009 14:53:27 -0500 |
parents | da8c265934ee |
children | 5d22be6d2f88 |
comparison
equal
deleted
inserted
replaced
202:da8c265934ee | 203:68a609fc516b |
---|---|
156 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') | 156 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') |
157 | 157 |
158 class ParsedString(str): | 158 class ParsedString(str): |
159 pass | 159 pass |
160 | 160 |
161 class SkipToLast(pyparsing.SkipTo): | |
162 def parseImpl( self, instring, loc, doActions=True ): | |
163 resultStore = [] | |
164 startLoc = loc | |
165 instrlen = len(instring) | |
166 expr = self.expr | |
167 failParse = False | |
168 while loc <= instrlen: | |
169 try: | |
170 if self.failOn: | |
171 failParse = True | |
172 self.failOn.tryParse(instring, loc) | |
173 failParse = False | |
174 loc = expr._skipIgnorables( instring, loc ) | |
175 expr._parse( instring, loc, doActions=False, callPreParse=False ) | |
176 skipText = instring[startLoc:loc] | |
177 if self.includeMatch: | |
178 loc,mat = expr._parse(instring,loc,doActions,callPreParse=False) | |
179 if mat: | |
180 skipRes = ParseResults( skipText ) | |
181 skipRes += mat | |
182 resultStore.append((loc, [ skipRes ])) | |
183 else: | |
184 resultStore,append((loc, [ skipText ])) | |
185 else: | |
186 resultStore.append((loc, [ skipText ])) | |
187 loc += 1 | |
188 except (pyparsing.ParseException,IndexError): | |
189 if failParse: | |
190 raise | |
191 else: | |
192 loc += 1 | |
193 if resultStore: | |
194 return resultStore[-1] | |
195 else: | |
196 exc = self.myException | |
197 exc.loc = loc | |
198 exc.pstr = instring | |
199 raise exc | |
200 | |
201 def replace_with_file_contents(fname): | |
202 if fname: | |
203 try: | |
204 result = open(os.path.expanduser(fname[0])).read() | |
205 except IOError: | |
206 result = '< %s' % fname[0] # wasn't a file after all | |
207 else: | |
208 result = getPasteBuffer() | |
209 return result | |
210 | |
161 class Cmd(cmd.Cmd): | 211 class Cmd(cmd.Cmd): |
162 echo = False | 212 echo = False |
163 caseInsensitive = True | 213 caseInsensitive = True |
164 continuationPrompt = '> ' | 214 continuationPrompt = '> ' |
165 timing = False | 215 timing = False |
167 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' } | 217 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' } |
168 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() | 218 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() |
169 noSpecialParse = 'set ed edit exit'.split() | 219 noSpecialParse = 'set ed edit exit'.split() |
170 defaultExtension = 'txt' | 220 defaultExtension = 'txt' |
171 defaultFileName = 'command.txt' | 221 defaultFileName = 'command.txt' |
172 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive', 'echo', 'timing'] | 222 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive', |
223 'echo', 'timing'] | |
173 settable.sort() | 224 settable.sort() |
174 | 225 |
175 editor = os.environ.get('EDITOR') | 226 editor = os.environ.get('EDITOR') |
176 _STOP_AND_EXIT = 2 | 227 _STOP_AND_EXIT = 2 |
177 if not editor: | 228 if not editor: |
383 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') | 434 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') |
384 stringEnd = pyparsing.stringEnd ^ '\nEOF' | 435 stringEnd = pyparsing.stringEnd ^ '\nEOF' |
385 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand') | 436 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand') |
386 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command') | 437 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command') |
387 pipe = pyparsing.Keyword('|', identChars='|') | 438 pipe = pyparsing.Keyword('|', identChars='|') |
388 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') | 439 self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '') |
389 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment) | 440 self.commentInProgress.ignore(pyparsing.quotedString).ignore(pyparsing.cStyleComment) |
390 afterElements = \ | 441 afterElements = \ |
391 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ | 442 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ |
392 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) | 443 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) |
393 if self.caseInsensitive: | 444 if self.caseInsensitive: |
394 self.multilineCommand.setParseAction(lambda x: x[0].lower()) | 445 self.multilineCommand.setParseAction(lambda x: x[0].lower()) |
397 self.blankLineTerminationParser = pyparsing.NoMatch | 448 self.blankLineTerminationParser = pyparsing.NoMatch |
398 else: | 449 else: |
399 self.blankLineTerminator = (pyparsing.lineEnd + pyparsing.lineEnd)('terminator') | 450 self.blankLineTerminator = (pyparsing.lineEnd + pyparsing.lineEnd)('terminator') |
400 self.blankLineTerminator.setResultsName('terminator') | 451 self.blankLineTerminator.setResultsName('terminator') |
401 self.blankLineTerminationParser = ((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(self.blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + self.blankLineTerminator)('statement') | 452 self.blankLineTerminationParser = ((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(self.blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + self.blankLineTerminator)('statement') |
402 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + | 453 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + SkipToLast(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + |
403 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements) | 454 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements) |
404 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + | 455 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + |
405 pyparsing.Optional(terminatorParser) + afterElements) | 456 pyparsing.Optional(terminatorParser) + afterElements) |
406 #self.multilineParser = self.multilineParser.setResultsName('multilineParser') | 457 #self.multilineParser = self.multilineParser.setResultsName('multilineParser') |
407 #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser') | 458 #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser') |
411 self.multilineParser | | 462 self.multilineParser | |
412 self.singleLineParser | | 463 self.singleLineParser | |
413 self.blankLineTerminationParser | | 464 self.blankLineTerminationParser | |
414 self.multilineCommand + pyparsing.SkipTo(stringEnd) | 465 self.multilineCommand + pyparsing.SkipTo(stringEnd) |
415 ) | 466 ) |
416 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) | 467 self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) |
417 | 468 |
418 inputMark = pyparsing.Literal('<') | 469 inputMark = pyparsing.Literal('<') |
419 inputMark.setParseAction(lambda x: '') | 470 inputMark.setParseAction(lambda x: '') |
420 inputFrom = pyparsing.Word(self.legalChars + '/\\')('inputFrom') | 471 inputFrom = pyparsing.Word(self.legalChars + '/\\')('inputFrom') |
421 inputFrom.setParseAction(lambda x: (x and open(os.path.expanduser(x[0])).read()) or getPasteBuffer()) | 472 inputFrom.setParseAction(replace_with_file_contents) |
422 self.inputParser = inputMark + pyparsing.Optional(inputFrom) | 473 self.inputParser = inputMark + pyparsing.Optional(inputFrom) |
423 self.inputParser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) | 474 self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) |
424 | 475 |
425 def parsed(self, raw, **kwargs): | 476 def parsed(self, raw, **kwargs): |
426 if isinstance(raw, ParsedString): | 477 if isinstance(raw, ParsedString): |
427 p = raw | 478 p = raw |
428 else: | 479 else: |