diff 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
line wrap: on
line diff
--- a/cmd2.py	Sat Jun 21 19:54:57 2008 -0400
+++ b/cmd2.py	Mon Jun 23 12:51:37 2008 -0400
@@ -18,7 +18,7 @@
 As of 0.3.0, options should be specified as `optparse` options.  See README.txt.
 flagReader.py options are still supported for backward compatibility
 """
-import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing
+import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest
 from optparse import make_option
 
 class OptionParser(optparse.OptionParser):
@@ -123,7 +123,41 @@
         def getPasteBuffer():
             raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
         setPasteBuffer = getPasteBuffer
-                
+
+def punctuationParser(punctuators):
+    """
+    Produces a string parser based on a list of targets to search for.
+    Output is a parser function.
+    Parser's output is a tuple: (before the target, [elements of the target], after the target)
+    >>> p = punctuationParser([';', 'EOF'])
+    >>> p('is terminated;')
+    ('is terminated', [';'], '')
+    >>> p('is terminated EOF after the end')    
+    ('is terminated', ['EOF'], ' after the end')
+    >>> p('is not terminated')
+    >>> pattern1 = pyparsing.Literal(';') + pyparsing.Optional(pyparsing.Word(pyparsing.nums))
+    >>> p2 = punctuationParser([pattern1, 'EOF'])
+    >>> p2('the quick brown fox;4')
+    ('the quick brown fox', [';', '4'], '')
+    >>> p2('the quick brown foxEOF')
+    ('the quick brown fox', ['EOF'], '')
+    >>> p2('nothing')
+    """
+    processed = punctuators[:]
+    if not hasattr(processed[0], 'parseString'):
+        processed[0] = pyparsing.Literal(processed[0])
+    processed = reduce(lambda x, y: x ^ y, processed)
+    processed.ignore(pyparsing.sglQuotedString)
+    processed.ignore(pyparsing.dblQuotedString)
+    pattern = pyparsing.SkipTo(processed) + processed + pyparsing.restOfLine
+    def parser(txt):
+        result = pattern.searchString(txt)
+        if result:
+            return result[0][0], result[0][1:-1], result[0][-1]
+        else:
+            return None
+    return parser
+
 class Cmd(cmd.Cmd):
     caseInsensitive = True
     multilineCommands = []
@@ -143,7 +177,7 @@
                     break
             
     settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive']
-    terminators = ';\n'
+    terminators = [';','\n']
     _TO_PASTE_BUFFER = 1
     def do_cmdenvironment(self, args):
         self.stdout.write("""
@@ -152,7 +186,7 @@
         Settable parameters: %(settable)s
         """ % 
         { 'casesensitive': ('not ' and self.caseInsensitive) or '',
-          'terminators': ' '.join(self.terminators),
+          'terminators': ' '.join(str(self.terminators)),
           'settable': ' '.join(self.settable)
         })
         
@@ -173,7 +207,12 @@
         """Lists single-key shortcuts available."""
         result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
         self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
-
+        
+    commmand_terminator_finder = punctuationParser(terminators)
+    outputTo_finder = punctuationParser(['>'])
+    inputFrom_finder = punctuationParser(['<'])
+    pipe_finder = punctuationParser(['|'])
+    
     notAPipe = pyparsing.SkipTo('|')
     notAPipe.ignore(pyparsing.sglQuotedString)
     notAPipe.ignore(pyparsing.dblQuotedString)    
@@ -650,3 +689,7 @@
     def restore(self):
         for attrib in self.attribs:
             setattr(self.obj, attrib, getattr(self, attrib))        
+
+if __name__ == '__main__':
+    doctest.testmod()    
+            
\ No newline at end of file