-
Notifications
You must be signed in to change notification settings - Fork 424
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
Simplify multiple calls to str.startswith / str.endswith #1760
Conversation
@@ -361,9 +356,9 @@ def load_addon_file(path, callback=None): | |||
return False | |||
fptr.close() | |||
# file_obj is either Zipfile or TarFile | |||
if path.endswith(".zip") or path.endswith(".ZIP"): | |||
if path.endswith((".zip", ".ZIP")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if path.endswith((".zip", ".ZIP")): | |
if path.lower().endswith((".zip")): |
would this be ever clearer and handle mixed case (.Zip etc.) correctly as well?
or path.startswith("ftp://") | ||
or path.startswith("file://") | ||
): | ||
if path.startswith(("http://", "https://", "ftp://", "file://")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if path.startswith(("http://", "https://", "ftp://", "file://")): | |
if path.lower().startswith(("http://", "https://", "ftp://", "file://")): |
The protocol in a URI is case-insensitive. See https://www.rfc-editor.org/rfc/rfc3986#section-3.1
file_obj = Zipfile(buffer) | ||
elif path.endswith(".tar.gz") or path.endswith(".tgz"): | ||
elif path.endswith((".tar.gz", ".tgz")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elif path.endswith((".tar.gz", ".tgz")): | |
elif path.lower().endswith((".tar.gz", ".tgz")): |
or url.endswith(".tar.gz") | ||
or url.endswith(".tgz") | ||
): | ||
if url.endswith((".zip", ".ZIP", ".tar.gz", ".tgz")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if url.endswith((".zip", ".ZIP", ".tar.gz", ".tgz")): | |
if url.lower().endswith((".zip", ".ZIP", ".tar.gz", ".tgz")): |
if not ( | ||
uri.startswith("http://") or uri.startswith("https://") | ||
): | ||
if not uri.startswith(("http://", "https://")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not uri.startswith(("http://", "https://")): | |
if not uri.lower().startswith(("http://", "https://")): |
url = self.secure_mode | ||
uri = url + "%(website)s" % {"website": uri} | ||
|
||
# FTP server address | ||
elif _type == UrlType.WEB_FTP: | ||
if not (uri.startswith("ftp://") or uri.startswith("ftps://")): | ||
if not uri.startswith(("ftp://", "ftps://")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not uri.startswith(("ftp://", "ftps://")): | |
if not uri.lower().startswith(("ftp://", "ftps://")): |
@@ -280,8 +280,8 @@ def month_navigation(self, nr_up, year, currentsection): | |||
url_fname = url_fname.lower() | |||
url = url_fname | |||
add_subdirs = False | |||
if not (url.startswith("http:") or url.startswith("/")): | |||
add_subdirs = not any(url.endswith(ext) for ext in _WEB_EXT) | |||
if not url.startswith(("http:", "/")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not url.startswith(("http:", "/")): | |
if not url.lower().startswith(("http:", "/")): |
@@ -708,7 +708,7 @@ def _has_webpage_extension(url): | |||
|
|||
@param: url -- filename to be checked | |||
""" | |||
return any(url.endswith(ext) for ext in _WEB_EXT) | |||
return url.endswith(_WEB_EXT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return url.endswith(_WEB_EXT) | |
return url.lower().endswith(_WEB_EXT) |
@@ -709,8 +709,8 @@ def month_navigation(self, nr_up, year, currentsection): | |||
url_fname = url_fname.lower() | |||
url = url_fname | |||
add_subdirs = False | |||
if not (url.startswith("http:") or url.startswith("/")): | |||
add_subdirs = not any(url.endswith(ext) for ext in _WEB_EXT) | |||
if not url.startswith(("http:", "/")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not url.startswith(("http:", "/")): | |
if not url.lower().startswith(("http:", "/")): |
@@ -2238,7 +2238,7 @@ def _has_webpage_extension(url): | |||
|
|||
url = filename to be checked | |||
""" | |||
return any(url.endswith(ext) for ext in _WEB_EXT) | |||
return url.endswith(_WEB_EXT) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return url.endswith(_WEB_EXT) | |
return url.lower().endswith(_WEB_EXT) |
@QuLogic My suggestions to improve handling of case go beyond your original fix. Let me know if you would prefer me to submit a separate PR with these changes. |
These methods have supported tuples to mean "any of the prefixes / suffixes" since Python 2.5, which can greatly simplify a bunch of `or`'d conditions.
fd8fc0e
to
c3c30f5
Compare
These methods have supported tuples to mean "any of the prefixes / suffixes" since Python 2.5, which can greatly simplify a bunch of
or
'd conditions.