Skip to content

Commit 63b07a2

Browse files
committed
Add a pastebin_helper config option to name an executable for pastebin upload.
If pastebin_helper is set it takes precedence over the pastebin configured for XML-RPC upload. The data is delivered to the executable via STDIN, and it is expected to return an URL on STDOUT.
1 parent a5f3ba1 commit 63b07a2

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

bpython/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def loadini(struct, configfile):
7070
'pastebin_url': 'http://bpaste.net/xmlrpc/',
7171
'pastebin_private': True,
7272
'pastebin_show_url': 'http://bpaste.net/show/$paste_id/',
73+
'pastebin_helper': '',
7374
},
7475
'keyboard': {
7576
'clear_line': 'C-u',
@@ -137,6 +138,7 @@ def loadini(struct, configfile):
137138
struct.pastebin_url = config.get('general', 'pastebin_url')
138139
struct.pastebin_private = config.get('general', 'pastebin_private')
139140
struct.pastebin_show_url = config.get('general', 'pastebin_show_url')
141+
struct.pastebin_helper = config.get('general', 'pastebin_helper')
140142

141143
struct.cli_suggestion_width = config.getfloat('cli',
142144
'suggestion_width')

bpython/repl.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import pydoc
3030
import re
3131
import rlcompleter
32+
import subprocess
3233
import sys
3334
import textwrap
3435
import traceback
@@ -770,10 +771,13 @@ def pastebin(self, s=None):
770771
not self.interact.confirm("Pastebin buffer? (y/N) ")):
771772
self.interact.notify("Pastebin aborted")
772773
return
773-
return self.do_pastebin(s)
774+
if self.config.pastebin_helper:
775+
return self.do_pastebin_helper(s)
776+
else:
777+
return self.do_pastebin_xmlrpc(s)
774778

775-
def do_pastebin(self, s):
776-
"""Actually perform the upload."""
779+
def do_pastebin_xmlrpc(self, s):
780+
"""Upload to pastebin via XML-RPC."""
777781
try:
778782
pasteservice = ServerProxy(self.config.pastebin_url)
779783
except IOError, e:
@@ -803,6 +807,40 @@ def do_pastebin(self, s):
803807
self.interact.notify('Pastebin URL: %s' % (paste_url, ), 10)
804808
return paste_url
805809

810+
def do_pastebin_helper(self, s):
811+
"""Call out to helper program for pastebin upload."""
812+
if s == self.prev_pastebin_content:
813+
self.interact.notify('Duplicate pastebin. Previous URL: ' +
814+
self.prev_pastebin_url)
815+
return self.prev_pastebin_url
816+
817+
self.interact.notify('Posting data to pastebin...')
818+
819+
try:
820+
helper = subprocess.Popen('', executable=self.config.pastebin_helper,
821+
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
822+
helper.stdin.write(s.encode())
823+
paste_url = helper.communicate()[0].decode().strip()
824+
except OSError, e:
825+
if e.errno == 2:
826+
self.interact.notify('Upload failed: Helper program not found.')
827+
else:
828+
self.interact.notify('Upload failed: Helper program could not be run.')
829+
return
830+
831+
if helper.returncode != 0:
832+
self.interact.notify('Upload failed: Helper program returned non-zero exit status %s.' %
833+
(helper.returncode, ))
834+
return
835+
836+
if not paste_url:
837+
self.interact.notify('Upload failed: No output from helper program.')
838+
return
839+
840+
self.prev_pastebin_content = s
841+
self.interact.notify('Pastebin URL: %s' % (paste_url, ), 10)
842+
return paste_url
843+
806844
def push(self, s, insert_into_history=True):
807845
"""Push a line of code onto the buffer so it can process it all
808846
at once when a code block ends"""

0 commit comments

Comments
 (0)