diff cmd2.py @ 406:cc5c68e15a83

merged
author Catherine Devlin <catherine.devlin@gmail.com>
date Sun, 07 Nov 2010 09:20:33 -0500
parents 3bef4253cf1b be18c88a0fc8
children f5aa16a22b52
line wrap: on
line diff
--- a/cmd2.py	Sat Nov 06 07:56:07 2010 -0400
+++ b/cmd2.py	Sun Nov 07 09:20:33 2010 -0500
@@ -46,8 +46,8 @@
     raw_input = input
 else:
     import pyparsing
-    
-__version__ = '0.6.0'
+
+__version__ = '0.6.2'
 
 class OptionParser(optparse.OptionParser):
     def exit(self, status=0, msg=None):
@@ -205,11 +205,11 @@
             return xclipproc.stdout.read()
         def write_to_paste_buffer(txt):
             xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
-            xclipproc.stdin.write(txt)
+            xclipproc.stdin.write(txt.encode())
             xclipproc.stdin.close()
             # but we want it in both the "primary" and "mouse" clipboards
             xclipproc = subprocess.Popen('xclip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
-            xclipproc.stdin.write(txt)
+            xclipproc.stdin.write(txt.encode())
             xclipproc.stdin.close()
     else:
         def get_paste_buffer(*args):
@@ -399,6 +399,7 @@
         ''')
     
     def poutput(self, msg):
+        '''Convenient shortcut for self.stdout.write(); adds newline if necessary.'''
         if msg:
             self.stdout.write(msg)
             if msg[-1] != '\n':
@@ -701,7 +702,9 @@
     
     def preparse(self, raw, **kwargs):
         return raw
-    
+    def postparse(self, parseResult):
+        return parseResult
+   
     def parsed(self, raw, **kwargs):
         if isinstance(raw, ParsedString):
             p = raw
@@ -717,6 +720,7 @@
             result = self.parser.parseString(s)
             result['raw'] = raw            
             result['command'] = result.multilineCommand or result.command        
+            result = self.postparse(result)
             p = ParsedString(result.args)
             p.parsed = result
             p.parser = self.parsed
@@ -767,6 +771,7 @@
         finally:
             return self.postparsing_postcmd(stop)        
     def complete_statement(self, line):
+        """Keep accepting lines of input until the command is complete."""
         if (not line) or (
             not pyparsing.Or(self.commentGrammars).
                 setParseAction(lambda x: '').transformString(line)):
@@ -787,6 +792,9 @@
             self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
             sys.stdout = self.stdout = self.redirect.stdin
         elif statement.parsed.output:
+            if (not statement.parsed.outputTo) and (not can_clip):
+                self.perror('Cannot redirect to paste buffer; install ``xclip`` and re-run to enable')
+                return
             self.kept_state = Statekeeper(self, ('stdout',))            
             self.kept_sys = Statekeeper(sys, ('stdout',))
             if statement.parsed.outputTo:
@@ -795,7 +803,7 @@
                     mode = 'a'
                 sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode)                            
             else:
-                sys.stdout = self.stdout = tempfile.TemporaryFile()
+                sys.stdout = self.stdout = tempfile.TemporaryFile(mode="w+")
                 if statement.parsed.output == '>>':
                     self.stdout.write(get_paste_buffer())
                     
@@ -1382,7 +1390,7 @@
     if typ == bool:
         try:
             return bool(int(new))
-        except ValueError, TypeError:
+        except (ValueError, TypeError):
             pass
         try:
             new = new.lower()    
@@ -1453,7 +1461,7 @@
                 self.transcripts[fname] = iter(tfile.readlines())
                 tfile.close()
         if not len(self.transcripts):
-            raise StandardError, "No test files found - nothing to test."
+            raise (StandardError,), "No test files found - nothing to test."
     def setUp(self):
         if self.CmdApp:
             self.outputTrap = OutputTrap()
@@ -1474,14 +1482,17 @@
         lineNum = 0
         try:
             line = transcript.next()
+            lineNum += 1
             while True:
                 while not line.startswith(self.cmdapp.prompt):
                     line = transcript.next()
+                    lineNum += 1
                 command = [line[len(self.cmdapp.prompt):]]
                 line = transcript.next()
                 while line.startswith(self.cmdapp.continuation_prompt):
                     command.append(line[len(self.cmdapp.continuation_prompt):])
                     line = transcript.next()
+                    lineNum += 1
                 command = ''.join(command)               
                 stop = self.cmdapp.onecmd_plus_hooks(command)
                 #TODO: should act on ``stop``
@@ -1495,6 +1506,7 @@
                 while not line.startswith(self.cmdapp.prompt):
                     expected.append(line)
                     line = transcript.next()
+                    lineNum += 1
                 expected = ''.join(expected)
                 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
                     (fname, lineNum, command, expected, result)      
@@ -1504,7 +1516,8 @@
                 result = self.anyWhitespace.sub('', result)
                 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
         except StopIteration:
-            pass
+            message = 'Last %d lines never seen, beginning with\n%s' % (len(expected), expected[0])
+            self.assert_(len(expected) < 3, message)
     def tearDown(self):
         if self.CmdApp:
             self.outputTrap.tearDown()