comparison cmd2.py @ 137:308e99cebbb8

comments working?
author catherine@dellzilla
date Mon, 10 Nov 2008 16:44:14 -0500
parents 67558b85ab38
children 4b428849783d
comparison
equal deleted inserted replaced
136:67558b85ab38 137:308e99cebbb8
149 return result 149 return result
150 150
151 class Cmd(cmd.Cmd): 151 class Cmd(cmd.Cmd):
152 echo = False 152 echo = False
153 caseInsensitive = True 153 caseInsensitive = True
154 multilineCommands = ['_multiline_comment']
155 commentGrammars = [pyparsing.cStyleComment, pyparsing.pythonStyleComment]
156 continuationPrompt = '> ' 154 continuationPrompt = '> '
157 shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '/*': '_multiline_comment', '#': '_comment'} 155 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' }
158 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 156 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
159 noSpecialParse = 'set ed edit exit'.split() 157 noSpecialParse = 'set ed edit exit'.split()
160 defaultExtension = 'txt' 158 defaultExtension = 'txt'
161 defaultFileName = 'command.txt' 159 defaultFileName = 'command.txt'
162 editor = os.environ.get('EDITOR') 160 editor = os.environ.get('EDITOR')
194 cmd.Cmd.__init__(self, *args, **kwargs) 192 cmd.Cmd.__init__(self, *args, **kwargs)
195 self.history = History() 193 self.history = History()
196 for p in (self.terminatorPattern, self.pipePattern, self.redirectInPattern, self.redirectOutPattern, self.punctuationPattern): 194 for p in (self.terminatorPattern, self.pipePattern, self.redirectInPattern, self.redirectOutPattern, self.punctuationPattern):
197 p.ignore(pyparsing.sglQuotedString) 195 p.ignore(pyparsing.sglQuotedString)
198 p.ignore(pyparsing.dblQuotedString) 196 p.ignore(pyparsing.dblQuotedString)
199 p.ignore(self.comments) 197 p.ignore(self.commentGrammars)
200 p.ignore(self.commentInProgress) 198 p.ignore(self.commentInProgress)
201
202 def do__comment(self, arg):
203 pass
204 def do__multiline_comment(self, arg):
205 pass
206 199
207 def do_shortcuts(self, args): 200 def do_shortcuts(self, args):
208 """Lists single-key shortcuts available.""" 201 """Lists single-key shortcuts available."""
209 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 202 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
210 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 203 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
211 204
212 comments = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment]) 205 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment])
213 comments.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') 206 commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '')
214 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd) 207 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd)
215 commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment) 208 commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment)
216 209
217 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') } 210 specialTerminators = {'/*': pyparsing.Literal('*/')('terminator') }
218 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n')) 211 terminatorPattern = ((pyparsing.Literal(';') ^ pyparsing.Literal('\n\n'))
249 >>> r.statement, r.input, r.inputFrom 242 >>> r.statement, r.input, r.inputFrom
250 ('got from ', '<', 'thisfile.txt') 243 ('got from ', '<', 'thisfile.txt')
251 ''' 244 '''
252 if isinstance(s, pyparsing.ParseResults): 245 if isinstance(s, pyparsing.ParseResults):
253 return s 246 return s
254 s = self.comments.transformString(s) 247 s = self.commentGrammars.transformString(s)
255 result = (pyparsing.SkipTo(pyparsing.StringEnd()))("fullStatement").parseString(s) 248 result = (pyparsing.SkipTo(pyparsing.StringEnd()))("fullStatement").parseString(s)
256 command = s.split()[0] 249 command = s.split()[0]
257 if self.caseInsensitive: 250 if self.caseInsensitive:
258 command = command.lower() 251 command = command.lower()
259 result['command'] = command 252 result['command'] = command