Mercurial > python-cmd2
comparison cmd2.py @ 65:4e028e9ec4c2
punctFinder going to go in
author | catherine@Elli.myhome.westell.com |
---|---|
date | Mon, 23 Jun 2008 12:51:37 -0400 |
parents | 682588392eaf |
children | f373aaa2390c |
comparison
equal
deleted
inserted
replaced
64:a0f7702f2a4b | 65:4e028e9ec4c2 |
---|---|
16 | 16 |
17 CHANGES: | 17 CHANGES: |
18 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | 18 As of 0.3.0, options should be specified as `optparse` options. See README.txt. |
19 flagReader.py options are still supported for backward compatibility | 19 flagReader.py options are still supported for backward compatibility |
20 """ | 20 """ |
21 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing | 21 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
22 from optparse import make_option | 22 from optparse import make_option |
23 | 23 |
24 class OptionParser(optparse.OptionParser): | 24 class OptionParser(optparse.OptionParser): |
25 def exit(self, status=0, msg=None): | 25 def exit(self, status=0, msg=None): |
26 self.values._exit = True | 26 self.values._exit = True |
121 xclipproc.stdin.close() | 121 xclipproc.stdin.close() |
122 else: | 122 else: |
123 def getPasteBuffer(): | 123 def getPasteBuffer(): |
124 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') | 124 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') |
125 setPasteBuffer = getPasteBuffer | 125 setPasteBuffer = getPasteBuffer |
126 | 126 |
127 def punctuationParser(punctuators): | |
128 """ | |
129 Produces a string parser based on a list of targets to search for. | |
130 Output is a parser function. | |
131 Parser's output is a tuple: (before the target, [elements of the target], after the target) | |
132 >>> p = punctuationParser([';', 'EOF']) | |
133 >>> p('is terminated;') | |
134 ('is terminated', [';'], '') | |
135 >>> p('is terminated EOF after the end') | |
136 ('is terminated', ['EOF'], ' after the end') | |
137 >>> p('is not terminated') | |
138 >>> pattern1 = pyparsing.Literal(';') + pyparsing.Optional(pyparsing.Word(pyparsing.nums)) | |
139 >>> p2 = punctuationParser([pattern1, 'EOF']) | |
140 >>> p2('the quick brown fox;4') | |
141 ('the quick brown fox', [';', '4'], '') | |
142 >>> p2('the quick brown foxEOF') | |
143 ('the quick brown fox', ['EOF'], '') | |
144 >>> p2('nothing') | |
145 """ | |
146 processed = punctuators[:] | |
147 if not hasattr(processed[0], 'parseString'): | |
148 processed[0] = pyparsing.Literal(processed[0]) | |
149 processed = reduce(lambda x, y: x ^ y, processed) | |
150 processed.ignore(pyparsing.sglQuotedString) | |
151 processed.ignore(pyparsing.dblQuotedString) | |
152 pattern = pyparsing.SkipTo(processed) + processed + pyparsing.restOfLine | |
153 def parser(txt): | |
154 result = pattern.searchString(txt) | |
155 if result: | |
156 return result[0][0], result[0][1:-1], result[0][-1] | |
157 else: | |
158 return None | |
159 return parser | |
160 | |
127 class Cmd(cmd.Cmd): | 161 class Cmd(cmd.Cmd): |
128 caseInsensitive = True | 162 caseInsensitive = True |
129 multilineCommands = [] | 163 multilineCommands = [] |
130 continuationPrompt = '> ' | 164 continuationPrompt = '> ' |
131 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} | 165 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} |
141 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']: | 175 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']: |
142 if not os.system('which %s' % (editor)): | 176 if not os.system('which %s' % (editor)): |
143 break | 177 break |
144 | 178 |
145 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive'] | 179 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive'] |
146 terminators = ';\n' | 180 terminators = [';','\n'] |
147 _TO_PASTE_BUFFER = 1 | 181 _TO_PASTE_BUFFER = 1 |
148 def do_cmdenvironment(self, args): | 182 def do_cmdenvironment(self, args): |
149 self.stdout.write(""" | 183 self.stdout.write(""" |
150 Commands are %(casesensitive)scase-sensitive. | 184 Commands are %(casesensitive)scase-sensitive. |
151 Commands may be terminated with: %(terminators)s | 185 Commands may be terminated with: %(terminators)s |
152 Settable parameters: %(settable)s | 186 Settable parameters: %(settable)s |
153 """ % | 187 """ % |
154 { 'casesensitive': ('not ' and self.caseInsensitive) or '', | 188 { 'casesensitive': ('not ' and self.caseInsensitive) or '', |
155 'terminators': ' '.join(self.terminators), | 189 'terminators': ' '.join(str(self.terminators)), |
156 'settable': ' '.join(self.settable) | 190 'settable': ' '.join(self.settable) |
157 }) | 191 }) |
158 | 192 |
159 def do_help(self, arg): | 193 def do_help(self, arg): |
160 cmd.Cmd.do_help(self, arg) | 194 cmd.Cmd.do_help(self, arg) |
171 | 205 |
172 def do_shortcuts(self, args): | 206 def do_shortcuts(self, args): |
173 """Lists single-key shortcuts available.""" | 207 """Lists single-key shortcuts available.""" |
174 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) | 208 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) |
175 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) | 209 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) |
176 | 210 |
211 commmand_terminator_finder = punctuationParser(terminators) | |
212 outputTo_finder = punctuationParser(['>']) | |
213 inputFrom_finder = punctuationParser(['<']) | |
214 pipe_finder = punctuationParser(['|']) | |
215 | |
177 notAPipe = pyparsing.SkipTo('|') | 216 notAPipe = pyparsing.SkipTo('|') |
178 notAPipe.ignore(pyparsing.sglQuotedString) | 217 notAPipe.ignore(pyparsing.sglQuotedString) |
179 notAPipe.ignore(pyparsing.dblQuotedString) | 218 notAPipe.ignore(pyparsing.dblQuotedString) |
180 pipeFinder = notAPipe + '|' + pyparsing.SkipTo(pyparsing.StringEnd()) | 219 pipeFinder = notAPipe + '|' + pyparsing.SkipTo(pyparsing.StringEnd()) |
181 def parsePipe(self, statement, mustBeTerminated): | 220 def parsePipe(self, statement, mustBeTerminated): |
648 for attrib in self.attribs: | 687 for attrib in self.attribs: |
649 setattr(self, attrib, getattr(self.obj, attrib)) | 688 setattr(self, attrib, getattr(self.obj, attrib)) |
650 def restore(self): | 689 def restore(self): |
651 for attrib in self.attribs: | 690 for attrib in self.attribs: |
652 setattr(self.obj, attrib, getattr(self, attrib)) | 691 setattr(self.obj, attrib, getattr(self, attrib)) |
692 | |
693 if __name__ == '__main__': | |
694 doctest.testmod() | |
695 |