-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -333,12 +333,7 @@ def load_addon_file(path, callback=None): | |||||
""" | ||||||
import tarfile | ||||||
|
||||||
if ( | ||||||
path.startswith("http://") | ||||||
or path.startswith("https://") | ||||||
or path.startswith("ftp://") | ||||||
or path.startswith("file://") | ||||||
): | ||||||
if path.startswith(("http://", "https://", "ftp://", "file://")): | ||||||
try: | ||||||
fptr = urlopen_maybe_no_check_cert(path) | ||||||
except: | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
would this be ever clearer and handle mixed case (.Zip etc.) correctly as well? |
||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
try: | ||||||
file_obj = tarfile.open(None, fileobj=buffer) | ||||||
except: | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1254,7 +1254,7 @@ def __refresh_addon_list(self, obj): | |||||
pm.set_pass(total=len(lines), header=_("Reading gramps-project.org...")) | ||||||
for line in lines: | ||||||
pm.step() | ||||||
if line.startswith("|-") or line.startswith("|}"): | ||||||
if line.startswith(("|-", "|}")): | ||||||
if row != []: | ||||||
rows.append(row) | ||||||
state = "row" | ||||||
|
@@ -1308,12 +1308,7 @@ def __refresh_addon_list(self, obj): | |||||
url = download[1:-1] | ||||||
if " " in url: | ||||||
url, text = url.split(" ", 1) | ||||||
if ( | ||||||
url.endswith(".zip") | ||||||
or url.endswith(".ZIP") | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# Then this is ok: | ||||||
self.addon_model.append( | ||||||
row=[ | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2746,15 +2746,13 @@ def display_url_list(self, urllist=None): | |||||
|
||||||
# Web Site address | ||||||
elif _type == UrlType.WEB_HOME: | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
uri = "ftp://%(ftpsite)s" % {"ftpsite": uri} | ||||||
|
||||||
descr = Html("p", html_escape(descr)) + ( | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -57,7 +57,7 @@ | |||||
|
||||||
import gramps.plugins.lib.libholiday as libholiday | ||||||
from gramps.plugins.webreport.basepage import BasePage | ||||||
from gramps.plugins.webreport.common import do_we_have_holidays, _WEB_EXT | ||||||
from gramps.plugins.webreport.common import do_we_have_holidays, _has_webpage_extension | ||||||
from gramps.plugins.lib.libhtml import Html # , xml_lang | ||||||
from gramps.gui.pluginmanager import GuiPluginManager | ||||||
|
||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
add_subdirs = not _has_webpage_extension(url) | ||||||
|
||||||
# whether to add subdirs or not??? | ||||||
if add_subdirs: | ||||||
|
@@ -1348,15 +1348,6 @@ def get_first_day_of_month(year, month): | |||||
return current_date, current_ord, monthinfo | ||||||
|
||||||
|
||||||
def _has_webpage_extension(url): | ||||||
""" | ||||||
determine if a filename has an extension or not... | ||||||
|
||||||
url = filename to be checked | ||||||
""" | ||||||
return any(url.endswith(ext) for ext in _WEB_EXT) | ||||||
|
||||||
|
||||||
def get_day_list(event_date, holiday_list, bday_anniv_list, rlocale=glocale): | ||||||
""" | ||||||
Will fill day_list and return it to its caller: calendar_build() | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -78,7 +78,7 @@ | |||||
# define clear blank line for proper styling | ||||||
FULLCLEAR = Html("div", class_="fullclear", inline=True) | ||||||
# define all possible web page filename extensions | ||||||
_WEB_EXT = [".html", ".htm", ".shtml", ".php", ".cgi"] | ||||||
_WEB_EXT = (".html", ".htm", ".shtml", ".php", ".cgi") | ||||||
# used to select secured web site or not | ||||||
HTTP = "http://" | ||||||
HTTPS = "https://" | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
def add_birthdate(dbase, ppl_handle_list, rlocale): | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -98,7 +98,7 @@ | |||||
FULLCLEAR = Html("div", class_="fullclear", inline=True) | ||||||
|
||||||
# Web page filename extensions | ||||||
_WEB_EXT = [".html", ".htm", ".shtml", ".php", ".php3", ".cgi"] | ||||||
_WEB_EXT = (".html", ".htm", ".shtml", ".php", ".php3", ".cgi") | ||||||
|
||||||
# Calendar stylesheet names | ||||||
_CALENDARSCREEN = "calendar-screen.css" | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
add_subdirs = not _has_webpage_extension(url) | ||||||
|
||||||
# whether to add subdirs or not??? | ||||||
if add_subdirs: | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
def get_day_list(event_date, holiday_list, bday_anniv_list, rlocale=glocale): | ||||||
|
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.
The protocol in a URI is case-insensitive. See https://www.rfc-editor.org/rfc/rfc3986#section-3.1