From d6d28235b0be17b7755c3baf0cec519e805d2437 Mon Sep 17 00:00:00 2001 From: alejo Date: Sat, 16 Apr 2022 01:01:44 -0400 Subject: [PATCH 1/3] Added logic to trim away leading/trailing whitespaces in user-inputted urls. Tested against current gpodder-installe executable for correct/ desired behaviour. simplified receive_clipboard_text method of class gPodderAddPodcast in addpodcast.py resolves #520 --- src/gpodder/gtkui/interface/addpodcast.py | 9 +++++---- src/gpodder/util.py | 10 +++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/gpodder/gtkui/interface/addpodcast.py b/src/gpodder/gtkui/interface/addpodcast.py index a094a80a9..ab20debe6 100644 --- a/src/gpodder/gtkui/interface/addpodcast.py +++ b/src/gpodder/gtkui/interface/addpodcast.py @@ -49,9 +49,10 @@ def new(self): clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) def receive_clipboard_text(clipboard, text, second_try): - # Heuristic: If there is a space in the clipboard - # text, assume it's some arbitrary text, and no URL - if text is not None and ' ' not in text: + # Heuristic: If space is present in clipboard text + # normalize_feed_url will either fix to valid url or + # return None if URL cannot be validated + if text is not None: url = util.normalize_feed_url(text) if url is not None: self.entry_url.set_text(url) @@ -72,7 +73,7 @@ def on_btn_paste_clicked(self, widget): def receive_clipboard_text(self, clipboard, text, data=None): if text is not None: - self.entry_url.set_text(text) + self.entry_url.set_text(text).strip() else: self.show_message(_('Nothing to paste.'), _('Clipboard is empty')) diff --git a/src/gpodder/util.py b/src/gpodder/util.py index 9523e45d8..70fe7a6ff 100644 --- a/src/gpodder/util.py +++ b/src/gpodder/util.py @@ -247,6 +247,14 @@ def normalize_feed_url(url): """ if not url or len(url) < 8: return None + + # Removes leading and/or trailing whitespaces - if url contains whitespaces + # in between after str.strip() -> conclude invalid url & return None + url = url.strip() + if ' ' in url: + return None + + # This is a list of prefixes that you can use to minimize the amount of # keystrokes that you have to use. @@ -267,7 +275,7 @@ def normalize_feed_url(url): # Assume HTTP for URLs without scheme if '://' not in url: - url = 'http://' + url + url = f'http://{url}' scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url) From 4c3d747a426badf678d082cf9ae38eff13becfd0 Mon Sep 17 00:00:00 2001 From: alejo Date: Sat, 16 Apr 2022 02:11:32 -0400 Subject: [PATCH 2/3] Linted, updated deprecated logger method warn to -> warning --- src/gpodder/gtkui/interface/addpodcast.py | 2 +- src/gpodder/util.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/gpodder/gtkui/interface/addpodcast.py b/src/gpodder/gtkui/interface/addpodcast.py index ab20debe6..97bef3f98 100644 --- a/src/gpodder/gtkui/interface/addpodcast.py +++ b/src/gpodder/gtkui/interface/addpodcast.py @@ -51,7 +51,7 @@ def new(self): def receive_clipboard_text(clipboard, text, second_try): # Heuristic: If space is present in clipboard text # normalize_feed_url will either fix to valid url or - # return None if URL cannot be validated + # return None if URL cannot be validated if text is not None: url = util.normalize_feed_url(text) if url is not None: diff --git a/src/gpodder/util.py b/src/gpodder/util.py index 70fe7a6ff..30edb4cab 100644 --- a/src/gpodder/util.py +++ b/src/gpodder/util.py @@ -73,14 +73,16 @@ try: import html5lib except ImportError: - logger.warn('html5lib not found, falling back to HTMLParser') + error_message = "html5lib was not found, fall-back to HTMLParser" + logger.warning(f'{error_message}', stack_info=True) html5lib = None if gpodder.ui.win32: try: import gpodder.utilwin32ctypes as win32file except ImportError: - logger.warn('Running on Win32 but utilwin32ctypes can\'t be loaded.') + error_message = 'Running on Win32: utilwin32ctypes cannot be loaded' + logger.warning(f'{error_message}', stack_info=True) win32file = None _ = gpodder.gettext @@ -247,14 +249,12 @@ def normalize_feed_url(url): """ if not url or len(url) < 8: return None - - # Removes leading and/or trailing whitespaces - if url contains whitespaces - # in between after str.strip() -> conclude invalid url & return None + + # Removes leading and/or trailing whitespaces - if url contains whitespaces + # in between after str.strip() -> conclude invalid url & return None url = url.strip() if ' ' in url: return None - - # This is a list of prefixes that you can use to minimize the amount of # keystrokes that you have to use. @@ -275,7 +275,7 @@ def normalize_feed_url(url): # Assume HTTP for URLs without scheme if '://' not in url: - url = f'http://{url}' + url = f'http://{url}' scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url) From 2e4dee0ff73dd41a55218cf7076228c1a93d7c91 Mon Sep 17 00:00:00 2001 From: alejo Date: Sun, 17 Apr 2022 00:04:24 -0400 Subject: [PATCH 3/3] removed f-strings to support 3.5 and changed logger.warning as per requested changes :) --- src/gpodder/util.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/gpodder/util.py b/src/gpodder/util.py index 30edb4cab..edc48b757 100644 --- a/src/gpodder/util.py +++ b/src/gpodder/util.py @@ -73,16 +73,14 @@ try: import html5lib except ImportError: - error_message = "html5lib was not found, fall-back to HTMLParser" - logger.warning(f'{error_message}', stack_info=True) + logger.warning("html5lib was not found, fall-back to HTMLParser") html5lib = None if gpodder.ui.win32: try: import gpodder.utilwin32ctypes as win32file except ImportError: - error_message = 'Running on Win32: utilwin32ctypes cannot be loaded' - logger.warning(f'{error_message}', stack_info=True) + logger.warning('Running on Win32: utilwin32ctypes cannot be loaded') win32file = None _ = gpodder.gettext @@ -275,7 +273,7 @@ def normalize_feed_url(url): # Assume HTTP for URLs without scheme if '://' not in url: - url = f'http://{url}' + url = 'http://' + url scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)