Skip to content

Commit 971d166

Browse files
committed
Use new JSON interface of bpaste.net and remove the old XML-RPC interface
This change adds a dependency on requests. Signed-off-by: Sebastian Ramacher <sebastian@ramacher.at>
1 parent f3f2a96 commit 971d166

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

bpython/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ def loadini(struct, configfile):
6868
'tab_length': 4,
6969
'pastebin_confirm': True,
7070
'pastebin_private': False,
71-
'pastebin_url': 'http://bpaste.net/xmlrpc/',
71+
'pastebin_url': 'https://bpaste.net/json/new',
7272
'pastebin_private': True,
73-
'pastebin_show_url': 'http://bpaste.net/show/$paste_id/',
73+
'pastebin_show_url': 'https://bpaste.net/show/$paste_id',
74+
'pastebin_removal_url': 'https://bpaste.net/remove/$removal_id',
75+
'pastebin_expiry': '1week',
7476
'pastebin_helper': '',
7577
'save_append_py': False,
7678
'editor': os.environ.get('VISUAL', os.environ.get('EDITOR', 'vi'))
@@ -149,6 +151,8 @@ def loadini(struct, configfile):
149151
struct.pastebin_url = config.get('general', 'pastebin_url')
150152
struct.pastebin_private = config.get('general', 'pastebin_private')
151153
struct.pastebin_show_url = config.get('general', 'pastebin_show_url')
154+
struct.pastebin_removal_url = config.get('general', 'pastebin_removal_url')
155+
struct.pastebin_expiry = config.get('general', 'pastebin_expiry')
152156
struct.pastebin_helper = config.get('general', 'pastebin_helper')
153157

154158
struct.cli_suggestion_width = config.getfloat('cli',

bpython/repl.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import inspect
2929
import os
3030
import pydoc
31+
import requests
3132
import shlex
3233
import subprocess
3334
import sys
@@ -41,8 +42,7 @@
4142
from socket import error as SocketError
4243
from string import Template
4344
from urllib import quote as urlquote
44-
from urlparse import urlparse
45-
from xmlrpclib import ServerProxy, Error as XMLRPCError
45+
from urlparse import urlparse, urljoin
4646

4747
from pygments.token import Token
4848

@@ -771,32 +771,41 @@ def do_pastebin(self, s):
771771
if self.config.pastebin_helper:
772772
return self.do_pastebin_helper(s)
773773
else:
774-
return self.do_pastebin_xmlrpc(s)
774+
return self.do_pastebin_json(s)
775775

776-
def do_pastebin_xmlrpc(self, s):
777-
"""Upload to pastebin via XML-RPC."""
778-
try:
779-
pasteservice = ServerProxy(self.config.pastebin_url)
780-
except IOError, e:
781-
self.interact.notify(_("Pastebin error for URL '%s': %s") %
782-
(self.config.pastebin_url, str(e)))
783-
return
776+
def do_pastebin_json(self, s):
777+
"""Upload to pastebin via json interface."""
778+
779+
url = urljoin(self.config.pastebin_url, '/json/new')
780+
payload = {
781+
'code': s,
782+
'lexer': 'pycon',
783+
'expiry': self.config.pastebin_expiry
784+
}
784785

785786
self.interact.notify(_('Posting data to pastebin...'))
786787
try:
787-
paste_id = pasteservice.pastes.newPaste('pycon', s, '', '', '',
788-
self.config.pastebin_private)
789-
except (SocketError, XMLRPCError), e:
790-
self.interact.notify(_('Upload failed: %s') % (str(e), ) )
791-
return
788+
response = requests.post(url, data=payload, verify=True)
789+
response.raise_for_status()
790+
except requests.exceptions.RequestException as exc:
791+
self.interact.notify(_('Upload failed: %s') % (str(exc), ))
792+
return
792793

793794
self.prev_pastebin_content = s
795+
data = response.json()
794796

795797
paste_url_template = Template(self.config.pastebin_show_url)
796-
paste_id = urlquote(paste_id)
798+
paste_id = urlquote(data['paste_id'])
797799
paste_url = paste_url_template.safe_substitute(paste_id=paste_id)
800+
801+
removal_url_template = Template(self.config.pastebin_removal_url)
802+
removal_id = urlquote(data['removal_id'])
803+
removal_url = removal_url_template.safe_substitute(removal_id=removal_id)
804+
798805
self.prev_pastebin_url = paste_url
799-
self.interact.notify(_('Pastebin URL: %s') % (paste_url, ), 10)
806+
self.interact.notify(_('Pastebin URL: %s - Removal URL: %s') %
807+
(paste_url, removal_url))
808+
800809
return paste_url
801810

802811
def do_pastebin_helper(self, s):

doc/sphinx/source/configuration-options.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ Soft tab size (default 4, see pep-8)
4444
pastebin_url
4545
^^^^^^^^^^^^
4646
The pastebin url to post to (without a trailing slash). This pastebin has to be
47-
a pastebin which uses LodgeIt. Examples are: http://paste.pocoo.org/xmlrpc/ and
48-
http://bpaste.net/xmlrpc/ (default: http://bpaste.net/xmlrpc/)
47+
a pastebin which uses provides a similar interface to ``bpaste.net``'s JSON
48+
interface. (default: https://bpaste.net/json/new)
4949

5050
pastebin_private
5151
^^^^^^^^^^^^^^^^
@@ -57,9 +57,19 @@ Default: True).
5757
pastebin_show_url
5858
^^^^^^^^^^^^^^^^^
5959
The url under which the new paste can be reached. ``$paste_id`` will be replaced
60-
by the ID of the new paste. Examples are: http://bpaste.net/show/$paste_id/ and
61-
http://paste.pocoo.org/show/$paste_id/ (default:
62-
http://bpaste.net/show/$paste_id/)
60+
by the ID of the new paste. (default: https://bpaste.net/show/$paste_id/)
61+
62+
pastebin_removal_url
63+
^^^^^^^^^^^^^^^^^^^^
64+
The url under which a paste can be removed. ``$removal_id`` will be replaced
65+
by the removal ID of the paste. (default: https://bpaste.net/remova/$removal_id/)
66+
67+
.. versionadded:: 0.14
68+
69+
pastebin_expiry
70+
^^^^^^^^^^^^^^^
71+
Time duration after which a paste should expire. Valid values are ``1day``,
72+
``1week`` and ``1month``. (default: ``1week``)
6373

6474
pastebin_helper
6575
^^^^^^^^^^^^^^^

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def initialize_options(self):
180180
long_description = """bpython is a fancy interface to the Python
181181
interpreter for Unix-like operating systems.""",
182182
install_requires = [
183-
'pygments'
183+
'pygments',
184+
'requests'
184185
],
185186
extras_require = extras_require,
186187
tests_require = ['mock'],

0 commit comments

Comments
 (0)