Skip to content

Commit 4795738

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 ce19978 commit 4795738

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
@@ -53,9 +53,11 @@ def loadini(struct, configfile):
5353
'tab_length': 4,
5454
'pastebin_confirm': True,
5555
'pastebin_private': False,
56-
'pastebin_url': 'http://bpaste.net/xmlrpc/',
56+
'pastebin_url': 'https://bpaste.net/json/new',
5757
'pastebin_private': True,
58-
'pastebin_show_url': 'http://bpaste.net/show/$paste_id/',
58+
'pastebin_show_url': 'https://bpaste.net/show/$paste_id',
59+
'pastebin_removal_url': 'https://bpaste.net/remove/$removal_id',
60+
'pastebin_expiry': '1week',
5961
'pastebin_helper': '',
6062
'save_append_py': False,
6163
'editor': os.environ.get('VISUAL', os.environ.get('EDITOR', 'vi'))
@@ -144,6 +146,8 @@ def loadini(struct, configfile):
144146
struct.pastebin_url = config.get('general', 'pastebin_url')
145147
struct.pastebin_private = config.get('general', 'pastebin_private')
146148
struct.pastebin_show_url = config.get('general', 'pastebin_show_url')
149+
struct.pastebin_removal_url = config.get('general', 'pastebin_removal_url')
150+
struct.pastebin_expiry = config.get('general', 'pastebin_expiry')
147151
struct.pastebin_helper = config.get('general', 'pastebin_helper')
148152

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

bpython/repl.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import logging
3030
import os
3131
import pydoc
32+
import requests
3233
import shlex
3334
import subprocess
3435
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

@@ -783,32 +783,41 @@ def do_pastebin(self, s):
783783
if self.config.pastebin_helper:
784784
return self.do_pastebin_helper(s)
785785
else:
786-
return self.do_pastebin_xmlrpc(s)
786+
return self.do_pastebin_json(s)
787787

788-
def do_pastebin_xmlrpc(self, s):
789-
"""Upload to pastebin via XML-RPC."""
790-
try:
791-
pasteservice = ServerProxy(self.config.pastebin_url)
792-
except IOError, e:
793-
self.interact.notify(_("Pastebin error for URL '%s': %s") %
794-
(self.config.pastebin_url, str(e)))
795-
return
788+
def do_pastebin_json(self, s):
789+
"""Upload to pastebin via json interface."""
790+
791+
url = urljoin(self.config.pastebin_url, '/json/new')
792+
payload = {
793+
'code': s,
794+
'lexer': 'pycon',
795+
'expiry': self.config.pastebin_expiry
796+
}
796797

797798
self.interact.notify(_('Posting data to pastebin...'))
798799
try:
799-
paste_id = pasteservice.pastes.newPaste('pycon', s, '', '', '',
800-
self.config.pastebin_private)
801-
except (SocketError, XMLRPCError), e:
802-
self.interact.notify(_('Upload failed: %s') % (str(e), ) )
803-
return
800+
response = requests.post(url, data=payload, verify=True)
801+
response.raise_for_status()
802+
except requests.exceptions.RequestException as exc:
803+
self.interact.notify(_('Upload failed: %s') % (str(exc), ))
804+
return
804805

805806
self.prev_pastebin_content = s
807+
data = response.json()
806808

807809
paste_url_template = Template(self.config.pastebin_show_url)
808-
paste_id = urlquote(paste_id)
810+
paste_id = urlquote(data['paste_id'])
809811
paste_url = paste_url_template.safe_substitute(paste_id=paste_id)
812+
813+
removal_url_template = Template(self.config.pastebin_removal_url)
814+
removal_id = urlquote(data['removal_id'])
815+
removal_url = removal_url_template.safe_substitute(removal_id=removal_id)
816+
810817
self.prev_pastebin_url = paste_url
811-
self.interact.notify(_('Pastebin URL: %s') % (paste_url, ), 10)
818+
self.interact.notify(_('Pastebin URL: %s - Removal URL: %s') %
819+
(paste_url, removal_url))
820+
812821
return paste_url
813822

814823
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
@@ -177,7 +177,8 @@ def initialize_options(self):
177177
long_description = """bpython is a fancy interface to the Python
178178
interpreter for Unix-like operating systems.""",
179179
install_requires = [
180-
'pygments'
180+
'pygments',
181+
'requests'
181182
],
182183
extras_require = extras_require,
183184
tests_require = ['mock'],

0 commit comments

Comments
 (0)