comparison cmd2.py @ 29:c4bd5f1a6968

paste buffer working on linux and windows
author catherine@cordelia
date Mon, 19 May 2008 09:35:13 -0400
parents 28b3fb301d3d
children dd64c0e3dbe0
comparison
equal deleted inserted replaced
28:28b3fb301d3d 29:c4bd5f1a6968
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 21 import cmd, re, os, sys, optparse, subprocess, tempfile
22 from optparse import make_option 22 from optparse import make_option
23 try:
24 import win32clipboard
25 except:
26 pass
27 23
28 class OptionParser(optparse.OptionParser): 24 class OptionParser(optparse.OptionParser):
29 def exit(self, status=0, msg=None): 25 def exit(self, status=0, msg=None):
30 if msg: 26 if msg:
31 print msg 27 print msg
72 to be installed on operating system. 68 to be installed on operating system.
73 On Debian/Ubuntu, 'sudo apt-get install xclip' will install it.""" 69 On Debian/Ubuntu, 'sudo apt-get install xclip' will install it."""
74 def __init__(self): 70 def __init__(self):
75 Exception.__init__(self, self.errmsg) 71 Exception.__init__(self, self.errmsg)
76 72
77 if sys.platform[:3] == 'win': 73 '''check here if functions exist; otherwise, stub out'''
78 def getPasteBuffer(): 74 pastebufferr = """Redirecting to or from paste buffer requires %s
79 try: 75 to be installed on operating system.
76 %s"""
77 if subprocess.mswindows:
78 try:
79 import win32clipboard
80 def getPasteBuffer():
80 win32clipboard.OpenClipboard(0) 81 win32clipboard.OpenClipboard(0)
81 except NameError: 82 try:
82 raise PasteBufferError 83 result = win32clipboard.GetClipboardData()
83 try: 84 except TypeError:
84 result = win32clipboard.GetClipboardData() 85 result = '' #non-text
85 except TypeError: 86 win32clipboard.CloseClipboard()
86 result = '' #non-text 87 return result
87 win32clipboard.CloseClipboard() 88 def writeToPasteBuffer(txt):
88 return result
89 def writeToPasteBuffer(txt):
90 try:
91 win32clipboard.OpenClipboard(0) 89 win32clipboard.OpenClipboard(0)
92 except NameError: 90 win32clipboard.EmptyClipboard()
93 raise PasteBufferError 91 win32clipboard.SetClipboardText(txt)
94 win32clipboard.EmptyClipboard() 92 win32clipboard.CloseClipboard()
95 win32clipboard.SetClipboardText(txt) 93 except ImportError:
96 win32clipboard.CloseClipboard() 94 def getPasteBuffer():
95 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/')
96 setPasteBuffer = getPasteBuffer
97 else: 97 else:
98 def getPasteBuffer(): 98 can_clip = False
99 try: 99 try:
100 xclipproc = subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 100 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
101 except subprocess.CalledProcessError: 101 can_clip = True
102 raise PasteBufferError 102 except AttributeError: # check_call not defined, Python < 2.5
103 return xclipproc.stdout.read() 103 teststring = 'Testing for presence of xclip.'
104 def writeToPasteBuffer(txt): 104 xclipproc = subprocess.check_call('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
105 try: 105 xclipproc.stdin.write(teststring)
106 xclipproc = subprocess.check_call('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 106 if xclipproc.stdout.read() == teststring:
107 except subprocess.CalledProcessError: 107 can_clip = True
108 raise PasteBufferError 108 except (subprocess.CalledProcessError, OSError):
109 xclipporc.stdin.write(txt) 109 pass
110 110 if can_clip:
111 def getPasteBuffer():
112 xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
113 return xclipproc.stdout.read()
114 def writeToPasteBuffer(txt):
115 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
116 xclipproc.stdin.write(txt)
117 xclipproc.stdin.close()
118 else:
119 def getPasteBuffer():
120 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
121 setPasteBuffer = getPasteBuffer
122
111 class Cmd(cmd.Cmd): 123 class Cmd(cmd.Cmd):
112 caseInsensitive = True 124 caseInsensitive = True
113 multilineCommands = [] 125 multilineCommands = []
114 continuationPrompt = '> ' 126 continuationPrompt = '> '
115 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} 127 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
219 self.stdout = tempfile.TemporaryFile() 231 self.stdout = tempfile.TemporaryFile()
220 if mode == 'a': 232 if mode == 'a':
221 self.stdout.write(clipcontents) 233 self.stdout.write(clipcontents)
222 else: 234 else:
223 statement = '%s %s' % (statement, clipcontents) 235 statement = '%s %s' % (statement, clipcontents)
224 except PasteBufferError, e: 236 except OSError, e:
225 print e 237 print e
226 return 0 238 return 0
227 elif redirect: 239 elif redirect:
228 if mode in ('w','a'): 240 if mode in ('w','a'):
229 statekeeper = Statekeeper(self, ('stdout',)) 241 statekeeper = Statekeeper(self, ('stdout',))