Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add expiry days option to pastebin magic and change http protocol to https #13056

Merged
merged 8 commits into from Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions IPython/core/magics/code.py
Expand Up @@ -249,7 +249,7 @@ def pastebin(self, parameter_s=''):
"""Upload code to dpaste.com, returning the URL.

Usage:\\
%pastebin [-d "Custom description"] 1-7
%pastebin [-d "Custom description"][-e 24] 1-7

The argument can be an input history range, a filename, or the name of a
string or macro.
Expand All @@ -258,25 +258,38 @@ def pastebin(self, parameter_s=''):

-d: Pass a custom description. The default will say
"Pasted from IPython".
-e: Pass number of days for the link to be expired.
The default will be 7 days.
"""
opts, args = self.parse_options(parameter_s, 'd:')
opts, args = self.parse_options(parameter_s, "d:e:")

try:
code = self.shell.find_user_code(args)
except (ValueError, TypeError) as e:
print(e.args[0])
return

expiry_days = 7
MrMino marked this conversation as resolved.
Show resolved Hide resolved
try:
expiry_days = int(opts.get("e", 7))
except ValueError as e:
print(e.args[0].capitalize())
return
if expiry_days < 1 or expiry_days > 365:
print("Expiry days should be in range of 1 to 365")
return

post_data = urlencode(
{
"title": opts.get("d", "Pasted from IPython"),
"syntax": "python",
"content": code,
"expiry_days": expiry_days,
}
).encode("utf-8")

request = Request(
"http://dpaste.com/api/v2/",
"https://dpaste.com/api/v2/",
headers={"User-Agent": "IPython v{}".format(version)},
)
response = urlopen(request, post_data)
Expand Down
7 changes: 7 additions & 0 deletions docs/source/whatsnew/pr/pastebin-expiry-days.rst
@@ -0,0 +1,7 @@
Pastebin magic expiry days option
=================================

The Pastebin magic now has ``-e`` option to determine
the number of days for paste expiration. For example
the paste that created with ``%pastebin -e 20 1`` magic will
be available for next 20 days.