Merge lp:~blackfrog/pastebinit/clipboard into lp:pastebinit

Proposed by BlackFrog
Status: Work in progress
Proposed branch: lp:~blackfrog/pastebinit/clipboard
Merge into: lp:pastebinit
Diff against target: 80 lines (+55/-3)
1 file modified
pastebinit (+55/-3)
To merge this branch: bzr merge lp:~blackfrog/pastebinit/clipboard
Reviewer Review Type Date Requested Status
Stéphane Graber Needs Fixing
Review via email: mp+51404@code.launchpad.net

Description of the change

This branch added a feature that will copy the url of the paste to the system clipboard. The feature has beed tested on Mac OS X system. I have included code for a Linux & Windows system. But I cannot test/debug those system.

To post a comment you must log in.
Revision history for this message
Stéphane Graber (stgraber) wrote :

Not merging it for this release, I'd need to have testers on the various supported platform and would like this feature to be optional.

I for one don't like apps messing with my clipboard and I don't think showing an error message on most system is the way to go.

review: Needs Fixing

Unmerged revisions

115. By BlackFrog

Added code to copy the url to the system clipboard

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pastebinit'
2--- pastebinit 2011-01-30 15:20:00 +0000
3+++ pastebinit 2011-02-26 05:46:14 +0000
4@@ -160,6 +160,52 @@
5 def getFirstNodeText(nodes, title):
6 return getText(getFirstNode(nodes, title).childNodes)
7
8+ def copyToClipboard(url):
9+ try:
10+ import platform, subprocess
11+ if platform.system() == 'Darwin':
12+ # echo `url` | pbcopy
13+ subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE).communicate(url)
14+
15+ elif platform.system() == "Windows":
16+ # I don't have windows to test the following code
17+ # I found at http://stackoverflow.com/questions/3827511/copying-and-pasting-from-to-clipboard-with-python-win32
18+ from win32clipboard import win32clipboard
19+ import win32gui, win32con
20+ OpenClipboard()
21+ EmptyClipboard()
22+ SetClipboard(win32con.CF_TEXT, url) #set clipboard data
23+ # if the above code doesn't work at all
24+ # try this method: http://mail.python.org/pipermail/python-win32/2005-April/003186.html
25+
26+ elif platform.system() == "Linux":
27+ # TODO: need to make sure xclip is install before using
28+ # because I don't know how to test if xclip is available on the system
29+ # I am going to wrap Popen call in try block
30+ try:
31+ subprocess.Popen(['xclip'], stdin=subprocess.PIPE).communicate(url)
32+ copySuccess = True
33+ except:
34+ copySuccess = False
35+ # Check if xclip was success, if not try gtk
36+ if copySuccess == False:
37+ # Found this code for using gtk on Ubuntu
38+ # http://ubuntuforums.org/showthread.php?t=1295070
39+ try:
40+ import pygtk
41+ pygtk.require('2.0')
42+ import gtk
43+ clipboard = gtk.clipboard_get()
44+ clipboard.set_text("%s" % (url))
45+ clipboard.store()
46+ except ImportError, e:
47+ pass # module doesn't exist
48+ except: #catch *all* exceptions
49+ # for debugging un-comment the lines below to see what the exception errors
50+ # e = sys.exc_info()[1]
51+ # print e
52+ print "Couldn't copy URL to clipboard, you will have to copy manually."
53+
54 # Display usage instructions
55 def Usage():
56 print "pastebinit v" + version
57@@ -331,14 +377,20 @@
58 page = url_opener.open(website, params) #Send the informations and be redirected to the final page
59
60 try:
61+ pasteURL = "" # the url of where we can file the paste
62 if reLink: #Check if we have to apply a regexp
63 website = website.replace(tmp_page, "")
64 if reLink == '(.*)':
65- print page.read().strip()
66+ pasteURL = page.read().strip()
67 else:
68- print website + re.split(reLink, page.read())[1] #Print the result of the Regexp
69+ pasteURL = website + re.split(reLink, page.read())[1] #Print the result of the Regexp
70 else:
71- print page.url #Get the final page and show the ur
72+ pasteURL = page.url #Get the final page and show the url
73+
74+ # copy the url to the clipboard
75+ copyToClipboard(pasteURL)
76+ print pasteURL # output paste url to stdout
77+
78 except KeyboardInterrupt:
79 sys.exit(_("KeyboardInterrupt caught."))
80 except:

Subscribers

People subscribed via source and target branches

to all changes: