changeset 29:c4bd5f1a6968

paste buffer working on linux and windows
author catherine@cordelia
date Mon, 19 May 2008 09:35:13 -0400
parents 28b3fb301d3d
children 786b3ea3e440
files cmd2.py
diffstat 1 files changed, 48 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Sun May 18 13:34:38 2008 -0400
+++ b/cmd2.py	Mon May 19 09:35:13 2008 -0400
@@ -20,10 +20,6 @@
 """
 import cmd, re, os, sys, optparse, subprocess, tempfile
 from optparse import make_option
-try:
-    import win32clipboard
-except:
-    pass
 
 class OptionParser(optparse.OptionParser):
     def exit(self, status=0, msg=None):
@@ -74,40 +70,56 @@
     def __init__(self):
         Exception.__init__(self, self.errmsg)
 
-if sys.platform[:3] == 'win':
-    def getPasteBuffer():
-        try:
+'''check here if functions exist; otherwise, stub out'''
+pastebufferr = """Redirecting to or from paste buffer requires %s
+to be installed on operating system.
+%s"""
+if subprocess.mswindows:
+    try:
+        import win32clipboard
+        def getPasteBuffer():
             win32clipboard.OpenClipboard(0)
-        except NameError:
-            raise PasteBufferError
-        try:
-            result = win32clipboard.GetClipboardData()
-        except TypeError:
-            result = ''  #non-text
-        win32clipboard.CloseClipboard()
-        return result
-    def writeToPasteBuffer(txt):
-        try:
+            try:
+                result = win32clipboard.GetClipboardData()
+            except TypeError:
+                result = ''  #non-text
+            win32clipboard.CloseClipboard()
+            return result            
+        def writeToPasteBuffer(txt):
             win32clipboard.OpenClipboard(0)
-        except NameError:
-            raise PasteBufferError
-        win32clipboard.EmptyClipboard()
-        win32clipboard.SetClipboardText(txt)
-        win32clipboard.CloseClipboard()
+            win32clipboard.EmptyClipboard()
+            win32clipboard.SetClipboardText(txt)
+            win32clipboard.CloseClipboard()        
+    except ImportError:
+        def getPasteBuffer():
+            raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/')
+        setPasteBuffer = getPasteBuffer
 else:
-    def getPasteBuffer():
-        try:
-            xclipproc = subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
-        except subprocess.CalledProcessError:
-            raise PasteBufferError
-        return xclipproc.stdout.read()
-    def writeToPasteBuffer(txt):
-        try:
-            xclipproc = subprocess.check_call('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
-        except subprocess.CalledProcessError:
-            raise PasteBufferError
-        xclipporc.stdin.write(txt)
-        
+    can_clip = False
+    try:
+        subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+        can_clip = True
+    except AttributeError:  # check_call not defined, Python < 2.5
+        teststring = 'Testing for presence of xclip.'
+        xclipproc = subprocess.check_call('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+        xclipproc.stdin.write(teststring)
+        if xclipproc.stdout.read() == teststring:
+            can_clip = True
+    except (subprocess.CalledProcessError, OSError):
+        pass
+    if can_clip:    
+        def getPasteBuffer():
+            xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+            return xclipproc.stdout.read()
+        def writeToPasteBuffer(txt):
+            xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+            xclipproc.stdin.write(txt)
+            xclipproc.stdin.close()
+    else:
+        def getPasteBuffer():
+            raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
+        setPasteBuffer = getPasteBuffer
+                
 class Cmd(cmd.Cmd):
     caseInsensitive = True
     multilineCommands = []
@@ -221,7 +233,7 @@
                         self.stdout.write(clipcontents)
                 else:
                     statement = '%s %s' % (statement, clipcontents)
-            except PasteBufferError, e:
+            except OSError, e:
                 print e
                 return 0
         elif redirect: