comparison cmd2.py @ 80:1e4ba836539e

fixed unit tests
author catherine@Elli.myhome.westell.com
date Fri, 04 Jul 2008 07:13:57 -0400
parents f583663c610f
children a3b0ef23146f
comparison
equal deleted inserted replaced
79:f583663c610f 80:1e4ba836539e
173 pass 173 pass
174 174
175 def __init__(self, *args, **kwargs): 175 def __init__(self, *args, **kwargs):
176 cmd.Cmd.__init__(self, *args, **kwargs) 176 cmd.Cmd.__init__(self, *args, **kwargs)
177 self.history = History() 177 self.history = History()
178 self.punctuationPattern = self.terminators ^ self.pipePattern ^ \
179 self.redirectInPattern ^ \
180 self.redirectOutPattern
181 self.punctuationPattern.ignore(pyparsing.sglQuotedString)
182 self.punctuationPattern.ignore(pyparsing.dblQuotedString)
183 178
184 def do_shortcuts(self, args): 179 def do_shortcuts(self, args):
185 """Lists single-key shortcuts available.""" 180 """Lists single-key shortcuts available."""
186 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 181 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
187 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 182 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
195 pipePattern = pyparsing.Literal('|')('pipe') + pyparsing.restOfLine('pipeTo') 190 pipePattern = pyparsing.Literal('|')('pipe') + pyparsing.restOfLine('pipeTo')
196 redirectOutPattern = (pyparsing.Literal('>>') ^ '>')('output') \ 191 redirectOutPattern = (pyparsing.Literal('>>') ^ '>')('output') \
197 + pyparsing.Optional(filenamePattern)('outputTo') 192 + pyparsing.Optional(filenamePattern)('outputTo')
198 redirectInPattern = pyparsing.Literal('<')('input') \ 193 redirectInPattern = pyparsing.Literal('<')('input') \
199 + pyparsing.Optional(filenamePattern)('inputFrom') 194 + pyparsing.Optional(filenamePattern)('inputFrom')
200 for p in (terminators, pipePattern, redirectInPattern, redirectOutPattern): 195 punctuationPattern = pipePattern ^ redirectInPattern ^ redirectOutPattern
196 for p in (terminators, pipePattern, redirectInPattern, redirectOutPattern, punctuationPattern):
201 p.ignore(pyparsing.sglQuotedString) 197 p.ignore(pyparsing.sglQuotedString)
202 p.ignore(pyparsing.dblQuotedString) 198 p.ignore(pyparsing.dblQuotedString)
203 199
204 def parsed(self, s): 200 def parsed(self, s):
205 ''' 201 '''
206 >>> c = Cmd() 202 >>> c = Cmd()
207 >>> c.parsed('quotes "are > ignored" < inp.txt').asDict() 203 >>> c.parsed('quotes "are > ignored" < inp.txt').asDict()
208 {'args': ' "are > ignored"', 'inputFrom': 'inp.txt', 'command': 'quotes', 'statement': 'quotes "are > ignored"', 'input': '<', 'fullStatement': 'quotes "are > ignored" < inp.txt'} 204 {'args': '"are > ignored"', 'inputFrom': 'inp.txt', 'command': 'quotes', 'statement': 'quotes "are > ignored"', 'input': '<', 'fullStatement': 'quotes "are > ignored" < inp.txt'}
209 >>> c.parsed('very complex; < from.txt >> to.txt etc.').asDict() 205 >>> c.parsed('very complex; < from.txt >> to.txt etc.').asDict()
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.'} 206 {'args': 'complex;', 'inputFrom': 'from.txt', 'command': 'very', 'statement': 'very complex;', 'input': '<', 'output': '>>', 'outputTo': 'to.txt', 'fullStatement': 'very complex; < from.txt >> to.txt etc.'}
211 >>> c.parsed('nothing to parse').asDict() 207 >>> c.parsed('nothing to parse').asDict()
212 {'args': ' to parse', 'command': 'nothing', 'statement': 'nothing to parse', 'fullStatement': 'nothing to parse'} 208 {'args': 'to parse', 'command': 'nothing', 'statement': 'nothing to parse', 'fullStatement': 'nothing to parse'}
213 >>> c.parsed('send it to | sort | wc').asDict() 209 >>> c.parsed('send it to | sort | wc').asDict()
214 {'args': ' it to', 'pipe': '|', 'pipeTo': ' sort | wc', 'command': 'send', 'statement': 'send it to', 'fullStatement': 'send it to | sort | wc'} 210 {'args': 'it to', 'pipe': '|', 'pipeTo': ' sort | wc', 'command': 'send', 'statement': 'send it to', 'fullStatement': 'send it to | sort | wc'}
215 >>> r = c.parsed('got from < thisfile.txt plus blah blah') 211 >>> r = c.parsed('got from < thisfile.txt plus blah blah')
216 >>> r.asDict() 212 >>> r.asDict()
217 {'args': ' from', 'inputFrom': 'thisfile.txt', 'command': 'got', 'statement': 'got from', 'input': '<', 'fullStatement': 'got from < thisfile.txt plus blah blah'} 213 {'args': 'from', 'inputFrom': 'thisfile.txt', 'command': 'got', 'statement': 'got from', 'input': '<', 'fullStatement': 'got from < thisfile.txt plus blah blah'}
218 >>> c.parsed(r).asDict() 214 >>> c.parsed(r).asDict()
219 {'args': ' from', 'inputFrom': 'thisfile.txt', 'command': 'got', 'statement': 'got from', 'input': '<', 'fullStatement': 'got from < thisfile.txt plus blah blah'} 215 {'args': 'from', 'inputFrom': 'thisfile.txt', 'command': 'got', 'statement': 'got from', 'input': '<', 'fullStatement': 'got from < thisfile.txt plus blah blah'}
220 ''' 216 '''
221 if isinstance(s, pyparsing.ParseResults): 217 if isinstance(s, pyparsing.ParseResults):
222 return s 218 return s
223 result = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement').parseString(s) 219 result = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement').parseString(s)
224 result['statement'] = result.fullStatement 220 result['statement'] = result.fullStatement