changeset 23:37bff80ef816

refactoring, no failure mode yet
author catherine@localhost
date Fri, 16 May 2008 17:17:26 -0400
parents 354a6b713d36
children f9196cf5ef51
files cmd2.py
diffstat 1 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Fri May 16 16:28:25 2008 -0400
+++ b/cmd2.py	Fri May 16 17:17:26 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
+import cmd, re, os, sys, optparse, subprocess, tempfile
 from optparse import make_option
 
 class OptionParser(optparse.OptionParser):
@@ -75,6 +75,7 @@
             
     settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive']
     terminators = ';\n'
+    _TO_PASTE_BUFFER = 1
     def do_cmdenvironment(self, args):
         self.stdout.write("""
         Commands are %(casesensitive)scase-sensitive.
@@ -116,7 +117,7 @@
             if not self.legalFileName.search(redirect):
                 return statement, None
         else:
-            redirect = 1
+            redirect = self._TO_PASTE_BUFFER
         return newStatement, redirect
     
     def extractCommand(self, statement):
@@ -141,6 +142,13 @@
             return newStatement, redirect, 'r'
         return statement, '', ''
         
+    def pasteBufferProcess(self, mode='read'):
+        if mode == 'read':
+            command = 'xclip -o -sel clip'
+        else:
+            command = 'xclip -sel clip'
+        return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+        
     def onecmd(self, line):
         """Interpret the argument as though it had been typed in response
         to the prompt.
@@ -157,14 +165,14 @@
             statement = self.finishStatement(statement)
         statekeeper = None
         statement, redirect, mode = self.parseRedirectors(statement)
-        if redirect == 1:
+        if redirect == self._TO_PASTE_BUFFER:
             if mode in ('a', 'r'):
-                clipcontents = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE).stdout.read()
+                clipcontents = self.pasteBufferProcess('read').stdout.read()
             if mode in ('w', 'a'):
                 statekeeper = Statekeeper(self, ('stdout',))
-                self.stdout = subprocess.Popen('xclip -sel clip', shell=True, stdin=subprocess.PIPE).stdin
+                self.stdout = self.pasteBufferProcess('write').stdin
                 if mode == 'a':
-                    self.stdout.write(clipcontents + '\n')
+                    self.stdout.write(clipcontents)
             else:
                 statement = '%s %s' % (statement, clipcontents)
         elif redirect: