From 694d3c63052f9db3870fade2c34432bc916589d1 Mon Sep 17 00:00:00 2001 From: Eyenpi Date: Tue, 13 Jul 2021 15:20:14 +0430 Subject: [PATCH 1/8] add expiry days feature to pastebin magic --- IPython/core/magics/code.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index e5b2ce17635..962f70bd745 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -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. @@ -258,8 +258,10 @@ 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) @@ -267,11 +269,23 @@ def pastebin(self, parameter_s=''): print(e.args[0]) return + expiry_days = 7 + if "e" in opts: + try: + expiry_days = int(opts.get("e", 7)) + except ValueError as e: + print(e.args[0]) + 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") From b0bc122821f75638b5ca5898585d81bfba96a44c Mon Sep 17 00:00:00 2001 From: Eyenpi Date: Tue, 13 Jul 2021 15:23:35 +0430 Subject: [PATCH 2/8] change dpaste api protocol from http to https --- IPython/core/magics/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 962f70bd745..4d330273938 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -290,7 +290,7 @@ def pastebin(self, parameter_s=''): ).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) From 276557e49bd4e77e1db0b99534591226fe7da724 Mon Sep 17 00:00:00 2001 From: Eyenpi Date: Tue, 13 Jul 2021 16:12:48 +0430 Subject: [PATCH 3/8] fix the lint with darker check --- IPython/core/magics/code.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 4d330273938..9a90e942a53 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -261,7 +261,7 @@ def pastebin(self, parameter_s=''): -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:e:') + opts, args = self.parse_options(parameter_s, "d:e:") try: code = self.shell.find_user_code(args) @@ -285,7 +285,7 @@ def pastebin(self, parameter_s=''): "title": opts.get("d", "Pasted from IPython"), "syntax": "python", "content": code, - "expiry_days": expiry_days + "expiry_days": expiry_days, } ).encode("utf-8") From 2c9ca78dd2906b3b24e9298de03301f4e06eefc7 Mon Sep 17 00:00:00 2001 From: Ali Nabipour <60272711+eyenpi@users.noreply.github.com> Date: Wed, 14 Jul 2021 14:08:38 +0430 Subject: [PATCH 4/8] capitalize error for consistency with L280 Co-authored-by: Blazej Michalik <6691643+MrMino@users.noreply.github.com> --- IPython/core/magics/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 9a90e942a53..7b766183b13 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -274,7 +274,7 @@ def pastebin(self, parameter_s=''): try: expiry_days = int(opts.get("e", 7)) except ValueError as e: - print(e.args[0]) + 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.") From 75f84eb8e464af30660a9b7759865d4107dc6958 Mon Sep 17 00:00:00 2001 From: Ali Nabipour <60272711+eyenpi@users.noreply.github.com> Date: Wed, 14 Jul 2021 14:09:44 +0430 Subject: [PATCH 5/8] drop the dot for consistency with L277 Co-authored-by: Blazej Michalik <6691643+MrMino@users.noreply.github.com> --- IPython/core/magics/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 7b766183b13..23709d04849 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -277,7 +277,7 @@ def pastebin(self, parameter_s=''): 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.") + print("Expiry days should be in range of 1 to 365") return post_data = urlencode( From 3708b30a78d636795c063f720ff1f1e9fe3f2b06 Mon Sep 17 00:00:00 2001 From: Eyenpi Date: Wed, 14 Jul 2021 14:22:18 +0430 Subject: [PATCH 6/8] remove the unneeded if statment --- IPython/core/magics/code.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 23709d04849..82ce71b2bf3 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -270,15 +270,14 @@ def pastebin(self, parameter_s=''): return expiry_days = 7 - if "e" in opts: - 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 + 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( { From e4f95eba4e759e859ae999f740ea929ab936bb64 Mon Sep 17 00:00:00 2001 From: Eyenpi Date: Wed, 14 Jul 2021 14:38:11 +0430 Subject: [PATCH 7/8] add the what's new entry for pastebin option --- docs/source/whatsnew/pr/pastebin-expiry-days.rst | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 docs/source/whatsnew/pr/pastebin-expiry-days.rst diff --git a/docs/source/whatsnew/pr/pastebin-expiry-days.rst b/docs/source/whatsnew/pr/pastebin-expiry-days.rst new file mode 100644 index 00000000000..cb86b390bac --- /dev/null +++ b/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. From 5b907230d0efddbfb232f71dc3a45c46139fc23b Mon Sep 17 00:00:00 2001 From: Blazej Michalik <6691643+MrMino@users.noreply.github.com> Date: Wed, 14 Jul 2021 14:37:14 +0200 Subject: [PATCH 8/8] Fix RST warning in docs "Title underline too short." --- docs/source/whatsnew/pr/pastebin-expiry-days.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/whatsnew/pr/pastebin-expiry-days.rst b/docs/source/whatsnew/pr/pastebin-expiry-days.rst index cb86b390bac..68faa7851f8 100644 --- a/docs/source/whatsnew/pr/pastebin-expiry-days.rst +++ b/docs/source/whatsnew/pr/pastebin-expiry-days.rst @@ -1,5 +1,5 @@ Pastebin magic expiry days option -=================== +================================= The Pastebin magic now has ``-e`` option to determine the number of days for paste expiration. For example