##// END OF EJS Templates
Backport PR #13056: add expiry days option to pastebin magic and change http protocol to https
Blazej Michalik -
Show More
@@ -0,0 +1,7 b''
1 Pastebin magic expiry days option
2 =================================
3
4 The Pastebin magic now has ``-e`` option to determine
5 the number of days for paste expiration. For example
6 the paste that created with ``%pastebin -e 20 1`` magic will
7 be available for next 20 days.
@@ -248,7 +248,7 b' class CodeMagics(Magics):'
248 """Upload code to dpaste.com, returning the URL.
248 """Upload code to dpaste.com, returning the URL.
249
249
250 Usage:\\
250 Usage:\\
251 %pastebin [-d "Custom description"] 1-7
251 %pastebin [-d "Custom description"][-e 24] 1-7
252
252
253 The argument can be an input history range, a filename, or the name of a
253 The argument can be an input history range, a filename, or the name of a
254 string or macro.
254 string or macro.
@@ -257,8 +257,10 b' class CodeMagics(Magics):'
257
257
258 -d: Pass a custom description. The default will say
258 -d: Pass a custom description. The default will say
259 "Pasted from IPython".
259 "Pasted from IPython".
260 -e: Pass number of days for the link to be expired.
261 The default will be 7 days.
260 """
262 """
261 opts, args = self.parse_options(parameter_s, 'd:')
263 opts, args = self.parse_options(parameter_s, "d:e:")
262
264
263 try:
265 try:
264 code = self.shell.find_user_code(args)
266 code = self.shell.find_user_code(args)
@@ -266,16 +268,27 b' class CodeMagics(Magics):'
266 print(e.args[0])
268 print(e.args[0])
267 return
269 return
268
270
271 expiry_days = 7
272 try:
273 expiry_days = int(opts.get("e", 7))
274 except ValueError as e:
275 print(e.args[0].capitalize())
276 return
277 if expiry_days < 1 or expiry_days > 365:
278 print("Expiry days should be in range of 1 to 365")
279 return
280
269 post_data = urlencode(
281 post_data = urlencode(
270 {
282 {
271 "title": opts.get("d", "Pasted from IPython"),
283 "title": opts.get("d", "Pasted from IPython"),
272 "syntax": "python",
284 "syntax": "python",
273 "content": code,
285 "content": code,
286 "expiry_days": expiry_days,
274 }
287 }
275 ).encode("utf-8")
288 ).encode("utf-8")
276
289
277 request = Request(
290 request = Request(
278 "http://dpaste.com/api/v2/",
291 "https://dpaste.com/api/v2/",
279 headers={"User-Agent": "IPython v{}".format(version)},
292 headers={"User-Agent": "IPython v{}".format(version)},
280 )
293 )
281 response = urlopen(request, post_data)
294 response = urlopen(request, post_data)
General Comments 0
You need to be logged in to leave comments. Login now