comparison cmd2.py @ 82:bbf0afc6868b

new parseResults working
author catherine@Elli.myhome.westell.com
date Fri, 04 Jul 2008 14:34:37 -0400
parents a3b0ef23146f
children 2176ce847939
comparison
equal deleted inserted replaced
81:a3b0ef23146f 82:bbf0afc6868b
128 def parseSearchResults(pattern, s): 128 def parseSearchResults(pattern, s):
129 generator = pattern.scanString(s) 129 generator = pattern.scanString(s)
130 try: 130 try:
131 result, start, stop = generator.next() 131 result, start, stop = generator.next()
132 result['before'], result['after'] = s[:start], s[stop:] 132 result['before'], result['after'] = s[:start], s[stop:]
133 result['upToIncluding'] = s[:stop]
133 except StopIteration: 134 except StopIteration:
134 result = pyparsing.ParseResults('') 135 result = pyparsing.ParseResults('')
135 result['before'] = s 136 result['before'] = s
136 return result 137 return result
137 138
202 p.ignore(pyparsing.dblQuotedString) 203 p.ignore(pyparsing.dblQuotedString)
203 204
204 def parsed(self, s): 205 def parsed(self, s):
205 ''' 206 '''
206 >>> c = Cmd() 207 >>> c = Cmd()
207 >>> c.parsed('quotes "are > ignored" < inp.txt').asDict() 208 >>> r = c.parsed('quotes "are > ignored" < inp.txt')
208 {'args': '"are > ignored"', 'inputFrom': 'inp.txt', 'command': 'quotes', 'statement': 'quotes "are > ignored"', 'input': '<', 'fullStatement': 'quotes "are > ignored" < inp.txt'} 209 >>> r.statement, r.input, r.inputFrom, r.output, r.outputFrom
209 >>> c.parsed('very complex; < from.txt >> to.txt etc.').asDict() 210 ('quotes "are > ignored" ', '<', 'inp.txt', '', '')
210 {'args': 'complex;', 'inputFrom': 'from.txt', 'command': 'very', 'terminator': ';', 'statement': 'very complex;', 'input': '<', 'output': '>>', 'outputTo': 'to.txt', 'fullStatement': 'very complex; < from.txt >> to.txt etc.'} 211 >>> r = c.parsed('very complex; < from.txt >> to.txt etc.')
211 >>> c.parsed('nothing to parse').asDict() 212 >>> r.statement, r.terminator, r.input, r.inputFrom, r.output, r.outputTo
212 {'args': 'to parse', 'command': 'nothing', 'statement': 'nothing to parse', 'fullStatement': 'nothing to parse'} 213 ('very complex;', ';', '<', 'from.txt', '>>', 'to.txt')
213 >>> c.parsed('send it to | sort | wc').asDict() 214 >>> c.parsed('nothing to parse').statement
214 {'args': 'it to', 'pipe': '|', 'pipeTo': ' sort | wc', 'command': 'send', 'statement': 'send it to', 'fullStatement': 'send it to | sort | wc'} 215 'nothing to parse'
216 >>> r = c.parsed('ignore > within a terminated statement; > out.txt')
217 >>> r.statement, r.terminator, r.input, r.inputFrom, r.output, r.outputTo
218 ('ignore > within a terminated statement;', ';', '', '', '>', 'out.txt')
219 >>> r = c.parsed('send it to | sort | wc')
220 >>> r.statement, r.pipe, r.pipeTo
221 ('send it to ', '|', ' sort | wc')
215 >>> r = c.parsed('got from < thisfile.txt plus blah blah') 222 >>> r = c.parsed('got from < thisfile.txt plus blah blah')
216 >>> r.asDict() 223 >>> r.statement, r.input, r.inputFrom
217 {'args': 'from', 'inputFrom': 'thisfile.txt', 'command': 'got', 'statement': 'got from', 'input': '<', 'fullStatement': 'got from < thisfile.txt plus blah blah'} 224 ('got from ', '<', 'thisfile.txt')
218 >>> c.parsed(r).asDict()
219 {'args': 'from', 'inputFrom': 'thisfile.txt', 'command': 'got', 'statement': 'got from', 'input': '<', 'fullStatement': 'got from < thisfile.txt plus blah blah'}
220 ''' 225 '''
221 if isinstance(s, pyparsing.ParseResults): 226 if isinstance(s, pyparsing.ParseResults):
222 return s 227 return s
223 result = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement').parseString(s) 228 result = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement').parseString(s)
224 result['statement'] = result.fullStatement 229 result['statement'] = result.fullStatement
225 result['parseable'] = result.fullStatement 230 result['parseable'] = result.fullStatement
226 result += parseSearchResults( 231 result += parseSearchResults(self.terminatorPattern, s)
227 pyparsing.SkipTo(self.terminatorPattern, include=True)('statement') +
228 pyparsing.SkipTo(pyparsing.StringEnd())('parseable'), s)
229 if result.terminator: 232 if result.terminator:
230 result['statement'] = ''.join(result.statement) 233 result['statement'] = result.upToIncluding
234 result['parseable'] = result.after
231 else: 235 else:
232 try: 236 result += parseSearchResults(self.punctuationPattern, s)
233 result += pyparsing.SkipTo(self.punctuationPattern)('statement').parseString(s) 237 result['statement'] = result.before
234 except pyparsing.ParseException:
235 pass
236 result += parseSearchResults(self.pipePattern, result.parseable) 238 result += parseSearchResults(self.pipePattern, result.parseable)
237 result += parseSearchResults(self.redirectInPattern, result.parseable) 239 result += parseSearchResults(self.redirectInPattern, result.parseable)
238 result += parseSearchResults(self.redirectOutPattern, result.parseable) 240 result += parseSearchResults(self.redirectOutPattern, result.parseable)
239 result += parseSearchResults(self.argSeparatorPattern, result.statement) 241 result += parseSearchResults(self.argSeparatorPattern, result.statement)
240 if self.caseInsensitive: 242 if self.caseInsensitive: