refactor: Add a maxsplit limit to string splits

This commit is contained in:
Gavin D'souza 2023-01-24 17:45:55 +05:30 committed by gavin
parent 72ae670b88
commit d357af1533
38 changed files with 69 additions and 57 deletions

View file

@ -1590,7 +1590,7 @@ def read_file(path, raise_not_found=False):
def get_attr(method_string: str) -> Any:
"""Get python method object from its name."""
app_name = method_string.split(".")[0]
app_name = method_string.split(".", 1)[0]
if (
not local.flags.in_uninstall
and not local.flags.in_install

View file

@ -55,7 +55,9 @@ class HTTPRequest:
def set_request_ip(self):
if frappe.get_request_header("X-Forwarded-For"):
frappe.local.request_ip = (frappe.get_request_header("X-Forwarded-For").split(",")[0]).strip()
frappe.local.request_ip = (
frappe.get_request_header("X-Forwarded-For").split(",", 1)[0]
).strip()
elif frappe.get_request_header("REMOTE_ADDR"):
frappe.local.request_ip = frappe.get_request_header("REMOTE_ADDR")

View file

@ -562,7 +562,7 @@ def _psql():
def jupyter(context):
"""Start an interactive jupyter notebook"""
installed_packages = (
r.split("==")[0]
r.split("==", 1)[0]
for r in subprocess.check_output([sys.executable, "-m", "pip", "freeze"], encoding="utf8")
)
@ -1001,7 +1001,7 @@ def request(context, args=None, path=None):
frappe.local.form_dict = frappe._dict()
if args.startswith("/api/method"):
frappe.local.form_dict.cmd = args.split("?")[0].split("/")[-1]
frappe.local.form_dict.cmd = args.split("?", 1)[0].split("/")[-1]
elif path:
with open(os.path.join("..", path)) as f:
args = json.loads(f.read())

View file

@ -499,7 +499,7 @@ def parse_email(communication, email_strings):
if email_string:
for email in email_string.split(","):
if delimiter in email:
email = email.split("@")[0]
email = email.split("@", 1)[0]
email_local_parts = email.split(delimiter)
if not len(email_local_parts) == 3:
continue
@ -521,7 +521,7 @@ def get_email_without_link(email):
try:
_email = email.split("@")
email_id = _email[0].split("+")[0]
email_id = _email[0].split("+", 1)[0]
email_host = _email[1]
except IndexError:
return email

View file

@ -885,7 +885,7 @@ def validate_series(dt, autoname=None, name=None):
if not autoname and dt.get("fields", {"fieldname": "naming_series"}):
dt.autoname = "naming_series:"
elif dt.autoname and dt.autoname.startswith("naming_series:"):
fieldname = dt.autoname.split("naming_series:")[0] or "naming_series"
fieldname = dt.autoname.split("naming_series:", 1)[0] or "naming_series"
if not dt.get("fields", {"fieldname": fieldname}):
frappe.throw(
_("Fieldname called {0} must exist to enable autonaming").format(frappe.bold(fieldname)),
@ -913,7 +913,7 @@ def validate_series(dt, autoname=None, name=None):
and (not autoname.startswith("format:"))
):
prefix = autoname.split(".")[0]
prefix = autoname.split(".", 1)[0]
doctype = frappe.qb.DocType("DocType")
used_in = (
frappe.qb.from_(doctype)
@ -1348,7 +1348,7 @@ def validate_fields(meta):
if meta.sort_field:
sort_fields = [meta.sort_field]
if "," in meta.sort_field:
sort_fields = [d.split()[0] for d in meta.sort_field.split(",")]
sort_fields = [d.split(maxsplit=1)[0] for d in meta.sort_field.split(",")]
for fieldname in sort_fields:
if fieldname not in (fieldname_list + list(default_fields) + list(child_table_fields)):

View file

@ -225,7 +225,7 @@ def extract_images_from_html(doc: "Document", content: str, is_private: bool = F
def _save_file(match):
data = match.group(1).split("data:")[1]
headers, content = data.split(",")
mtype = headers.split(";")[0]
mtype = headers.split(";", 1)[0]
if isinstance(content, str):
content = content.encode("utf-8")
@ -237,7 +237,7 @@ def extract_images_from_html(doc: "Document", content: str, is_private: bool = F
if "filename=" in headers:
filename = headers.split("filename=")[-1]
filename = safe_decode(filename).split(";")[0]
filename = safe_decode(filename).split(";", 1)[0]
else:
filename = get_random_filename(content_type=mtype)

View file

@ -26,7 +26,7 @@ class PackageImport(Document):
attachment = attachment[0]
# get package_name from file (package_name-0.0.0.tar.gz)
package_name = attachment.file_name.split(".")[0].rsplit("-", 1)[0]
package_name = attachment.file_name.split(".", 1)[0].rsplit("-", 1)[0]
if not os.path.exists(frappe.get_site_path("packages")):
os.makedirs(frappe.get_site_path("packages"))

View file

@ -277,7 +277,7 @@ def create_user(email, *roles):
user = frappe.new_doc("User")
user.email = email
user.first_name = email.split("@")[0]
user.first_name = email.split("@", 1)[0]
if not roles:
roles = ("System Manager",)

View file

@ -19,7 +19,7 @@ def get_mariadb_version(version_string: str = ""):
# MariaDB classifies their versions as Major (1st and 2nd number), and Minor (3rd number)
# Example: Version 10.3.13 is Major Version = 10.3, Minor Version = 13
version_string = version_string or get_mariadb_variables().get("version")
version = version_string.split("-")[0]
version = version_string.split("-", 1)[0]
return version.rsplit(".", 1)

View file

@ -306,8 +306,8 @@ def get_events(start, end, user=None, for_reminder=False, filters=None) -> list[
)
# process recurring events
start = start.split(" ")[0]
end = end.split(" ")[0]
start = start.split(" ", 1)[0]
end = end.split(" ", 1)[0]
add_events = []
remove_events = []
@ -315,7 +315,7 @@ def get_events(start, end, user=None, for_reminder=False, filters=None) -> list[
new_event = e.copy()
enddate = (
add_days(date, int(date_diff(e.ends_on.split(" ")[0], e.starts_on.split(" ")[0])))
add_days(date, int(date_diff(e.ends_on.split(" ", 1)[0], e.starts_on.split(" ", 1)[0])))
if (e.starts_on and e.ends_on)
else date
)
@ -337,8 +337,8 @@ def get_events(start, end, user=None, for_reminder=False, filters=None) -> list[
repeat = "3000-01-01" if cstr(e.repeat_till) == "" else e.repeat_till
if e.repeat_on == "Yearly":
start_year = cint(start.split("-")[0])
end_year = cint(end.split("-")[0])
start_year = cint(start.split("-", 1)[0])
end_year = cint(end.split("-", 1)[0])
# creates a string with date (27) and month (07) eg: 07-27
event_start = "-".join(event_start.split("-")[1:])
@ -357,7 +357,13 @@ def get_events(start, end, user=None, for_reminder=False, filters=None) -> list[
if e.repeat_on == "Monthly":
# creates a string with date (27) and month (07) and year (2019) eg: 2019-07-27
date = start.split("-")[0] + "-" + start.split("-")[1] + "-" + event_start.split("-")[2]
date = (
start.split("-", maxsplit=1)[0]
+ "-"
+ start.split("-", maxsplit=2)[1]
+ "-"
+ event_start.split("-", maxsplit=3)[2]
)
# last day of month issue, start from prev month!
try:

View file

@ -134,7 +134,7 @@ class FormMeta(Meta):
for fname in os.listdir(path):
if fname.endswith(".html"):
with open(os.path.join(path, fname), encoding="utf-8") as f:
templates[fname.split(".")[0]] = scrub_html_template(f.read())
templates[fname.split(".", 1)[0]] = scrub_html_template(f.read())
self.set("__templates", templates or None)
@ -249,7 +249,7 @@ class FormMeta(Meta):
def load_templates(self):
if not self.custom:
module = load_doctype_module(self.name)
app = module.__name__.split(".")[0]
app = module.__name__.split(".", 1)[0]
templates = {}
if hasattr(module, "form_grid_templates"):
for key, path in module.form_grid_templates.items():

View file

@ -428,7 +428,7 @@ def add_total_row(result, columns, meta=None, is_tree=False, parent_field=None):
if isinstance(columns[0], str):
first_col = columns[0].split(":")
if len(first_col) > 1:
first_col_fieldtype = first_col[1].split("/")[0]
first_col_fieldtype = first_col[1].split("/", 1)[0]
else:
first_col_fieldtype = columns[0].get("fieldtype")

View file

@ -185,7 +185,7 @@ def extract_fieldname(field):
fieldname = field
for sep in (" as ", " AS "):
if sep in fieldname:
fieldname = fieldname.split(sep)[0]
fieldname = fieldname.split(sep, 1)[0]
# certain functions allowed, extract the fieldname from the function
if fieldname.startswith("count(") or fieldname.startswith("sum(") or fieldname.startswith("avg("):
@ -456,13 +456,13 @@ def handle_duration_fieldtype_values(doctype, data, fields):
def parse_field(field: str) -> tuple[str | None, str]:
"""Parse a field into parenttype and fieldname."""
key = field.split(" as ")[0]
key = field.split(" as ", 1)[0]
if key.startswith(("count(", "sum(", "avg(")):
raise ValueError
if "." in key:
return key.split(".")[0][4:-1], key.split(".")[1].strip("`")
return key.split(".", 1)[0][4:-1], key.split(".", 2)[1].strip("`")
return None, key.strip("`")

View file

@ -76,7 +76,7 @@ def search_widget(
standard_queries = frappe.get_hooks().standard_queries or {}
if query and query.split()[0].lower() != "select":
if query and query.split(maxsplit=1)[0].lower() != "select":
# by method
try:
is_whitelisted(frappe.get_attr(query))

View file

@ -288,7 +288,11 @@ class FrappeClient:
if doctype != "User" and not frappe.db.exists("User", doc.get("owner")):
frappe.get_doc(
{"doctype": "User", "email": doc.get("owner"), "first_name": doc.get("owner").split("@")[0]}
{
"doctype": "User",
"email": doc.get("owner"),
"first_name": doc.get("owner").split("@", 1)[0],
}
).insert()
if update:

View file

@ -242,7 +242,7 @@ def parse_app_name(name: str) -> str:
_repo = name.split(":")[1].rsplit("/", 1)[1]
else:
_repo = name.rsplit("/", 2)[2]
repo = _repo.split(".")[0]
repo = _repo.split(".", 1)[0]
else:
_, repo, _ = fetch_details_from_tag(name)
return repo
@ -785,7 +785,7 @@ def is_downgrade(sql_file_path, verbose=False):
for app in all_apps:
app_name = app[0]
app_version = app[1].split(" ")[0]
app_version = app[1].split(" ", 1)[0]
if app_name == "frappe":
try:

View file

@ -115,7 +115,7 @@ def get_static_default_value(df, doctype_user_permissions, allowed_records):
return df.default
elif df.fieldtype == "Select" and df.options and df.options not in ("[Select]", "Loading..."):
return df.options.split("\n")[0]
return df.options.split("\n", 1)[0]
def validate_value_via_user_permissions(

View file

@ -435,7 +435,7 @@ class DatabaseQuery:
if not ("tab" in field and "." in field) or any(x for x in sql_functions if x in field):
continue
table_name = field.split(".")[0]
table_name = field.split(".", 1)[0]
if table_name.lower().startswith("group_concat("):
table_name = table_name[13:]
@ -897,7 +897,7 @@ class DatabaseQuery:
# will covert to
# `tabItem`.`idx` desc, `tabItem`.`modified` desc
args.order_by = ", ".join(
f"`tab{self.doctype}`.`{f.split()[0].strip()}` {f.split()[1].strip()}"
f"`tab{self.doctype}`.`{f.split(maxsplit=1)[0].strip()}` {f.split(maxsplit=2)[1].strip()}"
for f in meta.sort_field.split(",")
)
else:
@ -1029,7 +1029,7 @@ def get_order_by(doctype, meta):
# will covert to
# `tabItem`.`idx` desc, `tabItem`.`modified` desc
order_by = ", ".join(
f"`tab{doctype}`.`{f.split()[0].strip()}` {f.split()[1].strip()}"
f"`tab{doctype}`.`{f.split(maxsplit=1)[0].strip()}` {f.split(maxsplit=2)[1].strip()}"
for f in meta.sort_field.split(",")
)

View file

@ -176,7 +176,7 @@ def update_naming_series(doc):
if doc.meta.autoname.startswith("naming_series:") and getattr(doc, "naming_series", None):
revert_series_if_last(doc.naming_series, doc.name, doc)
elif doc.meta.autoname.split(":")[0] not in ("Prompt", "field", "hash", "autoincrement"):
elif doc.meta.autoname.split(":", 1)[0] not in ("Prompt", "field", "hash", "autoincrement"):
revert_series_if_last(doc.meta.autoname, doc.name, doc)

View file

@ -27,7 +27,7 @@ def rename_field(doctype, old_fieldname, new_fieldname):
frappe.db.sql(
"""update `tab%s` set parentfield=%s
where parentfield=%s"""
% (new_field.options.split("\n")[0], "%s", "%s"),
% (new_field.options.split("\n", 1)[0], "%s", "%s"),
(new_fieldname, old_fieldname),
)

View file

@ -252,7 +252,7 @@ def load_code_properties(doc, path):
if hasattr(doc, "get_code_fields"):
dirname, filename = os.path.split(path)
for key, extn in doc.get_code_fields().items():
codefile = os.path.join(dirname, filename.split(".")[0] + "." + extn)
codefile = os.path.join(dirname, filename.split(".", 1)[0] + "." + extn)
if os.path.exists(codefile):
with open(codefile) as txtfile:
doc.set(key, txtfile.read())

View file

@ -152,7 +152,7 @@ def run_single(patchmodule=None, method=None, methodargs=None, force=False):
return True
def execute_patch(patchmodule, method=None, methodargs=None):
def execute_patch(patchmodule: str, method=None, methodargs=None):
"""execute the patch"""
_patch_mode(True)
@ -162,7 +162,7 @@ def execute_patch(patchmodule, method=None, methodargs=None):
docstring = ""
else:
has_patch_file = True
patch = f"{patchmodule.split()[0]}.execute"
patch = f"{patchmodule.split(maxsplit=1)[0]}.execute"
_patch = frappe.get_attr(patch)
docstring = _patch.__doc__ or ""

View file

@ -123,7 +123,7 @@ def get_static_pages_from_all_apps():
files_to_index = glob(path_to_index + "/**/*.html", recursive=True)
files_to_index.extend(glob(path_to_index + "/**/*.md", recursive=True))
for file in files_to_index:
route = os.path.relpath(file, path_to_index).split(".")[0]
route = os.path.relpath(file, path_to_index).split(".", maxsplit=1)[0]
if route.endswith("index"):
route = route.rsplit("index", 1)[0]
routes_to_index.append(route)

View file

@ -330,7 +330,7 @@ class TestCommands(BaseTestCommands):
# test 2: bare functionality for single site
self.execute("bench --site {site} list-apps")
self.assertEqual(self.returncode, 0)
list_apps = {_x.split()[0] for _x in self.stdout.split("\n")}
list_apps = {_x.split(maxsplit=1)[0] for _x in self.stdout.split("\n")}
doctype = frappe.get_single("Installed Applications").installed_applications
if doctype:
installed_apps = {x.app_name for x in doctype}

View file

@ -59,7 +59,7 @@ class TestPatches(FrappeTestCase):
else:
if patchmodule.startswith("finally:"):
patchmodule = patchmodule.split("finally:")[-1]
self.assertTrue(frappe.get_attr(patchmodule.split()[0] + ".execute"))
self.assertTrue(frappe.get_attr(patchmodule.split(maxsplit=1)[0] + ".execute"))
frappe.flags.in_install = False
@ -149,7 +149,7 @@ def check_patch_files(app):
patch_dir = Path(frappe.get_app_path(app)) / "patches"
app_patches = [p.split()[0] for p in patch_handler.get_patches_from_app(app)]
app_patches = [p.split(maxsplit=1)[0] for p in patch_handler.get_patches_from_app(app)]
missing_patches = []

View file

@ -54,7 +54,7 @@ class TestSearch(FrappeTestCase):
user.update(
{
"email": email,
"first_name": email.split("@")[0],
"first_name": email.split("@", 1)[0],
"enabled": False,
"allowed_in_mentions": True,
}

View file

@ -314,7 +314,7 @@ def get_translations_from_apps(lang, apps=None):
path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv")
translations.update(get_translation_dict_from_file(path, lang, app) or {})
if "-" in lang:
parent = lang.split("-")[0]
parent = lang.split("-", 1)[0]
parent_translations = get_translations_from_apps(parent)
parent_translations.update(translations)
return parent_translations

View file

@ -509,7 +509,7 @@ def decode_dict(d, encoding="utf-8"):
@functools.lru_cache
def get_site_name(hostname):
return hostname.split(":")[0]
return hostname.split(":", 1)[0]
def get_disk_usage():

View file

@ -265,7 +265,7 @@ class BackupGenerator:
def backup_time(file_path):
file_name = file_path.split(os.sep)[-1]
file_timestamp = file_name.split("-")[0]
file_timestamp = file_name.split("-", 1)[0]
return timegm(datetime.strptime(file_timestamp, "%Y%m%d_%H%M%S").utctimetuple())
def get_latest(file_pattern):

View file

@ -177,7 +177,7 @@ def check_for_update():
# Get local instance's current version or the app
branch_version = (
apps[app]["branch_version"].split(" ")[0] if apps[app].get("branch_version", "") else ""
apps[app]["branch_version"].split(" ", 1)[0] if apps[app].get("branch_version", "") else ""
)
instance_version = Version(branch_version or apps[app].get("version"))
# Compare and popup update message

View file

@ -1191,7 +1191,7 @@ def fmt_money(
if flt(amount) < 0:
minus = "-"
amount = cstr(abs(flt(amount))).split(".")[0]
amount = cstr(abs(flt(amount))).split(".", 1)[0]
if len(amount) > 3:
parts.append(amount[-3:])
@ -1348,7 +1348,7 @@ def is_image(filepath: str) -> bool:
from mimetypes import guess_type
# filepath can be https://example.com/bed.jpg?v=129
filepath = (filepath or "").split("?")[0]
filepath = (filepath or "").split("?", 1)[0]
return (guess_type(filepath)[0] or "").startswith("image/")

View file

@ -51,7 +51,7 @@ def parse_date(date):
if " " in date:
# as date-timestamp, remove the time part
date = date.split(" ")[0]
date = date.split(" ", 1)[0]
# why the sorting? checking should be done in a predictable order
check_formats = [None] + sorted(

View file

@ -68,7 +68,7 @@ def get_snapshot(exception, context=10):
s = {
"pyver": "Python {version:s}: {executable:s} (prefix: {prefix:s})".format(
version=sys.version.split()[0], executable=sys.executable, prefix=sys.prefix
version=sys.version.split(maxsplit=1)[0], executable=sys.executable, prefix=sys.prefix
),
"timestamp": cstr(datetime.datetime.now()),
"traceback": traceback.format_exc(),

View file

@ -307,7 +307,7 @@ def get_routes_to_index():
filepath = os.path.join(dirpath, f)
route = os.path.relpath(filepath, base)
route = route.split(".")[0]
route = route.split(".", 1)[0]
if route.endswith("index"):
route = route.rsplit("index", 1)[0]

View file

@ -13,7 +13,7 @@ class Blogger(Document):
if self.user and not frappe.db.exists("User", self.user):
# for data import
frappe.get_doc(
{"doctype": "User", "email": self.user, "first_name": self.user.split("@")[0]}
{"doctype": "User", "email": self.user, "first_name": self.user.split("@", 1)[0]}
).insert()
def on_update(self):

View file

@ -61,7 +61,7 @@ def create_user_if_not_exists(email, first_name=None):
"user_type": "Website User",
"email": email,
"send_welcome_email": 0,
"first_name": first_name or email.split("@")[0],
"first_name": first_name or email.split("@", 1)[0],
"birth_date": frappe.utils.now_datetime(),
}
).insert(ignore_permissions=True)

View file

@ -153,7 +153,7 @@ class WebPage(WebsiteGenerator):
def check_for_redirect(self, context):
if "<!-- redirect:" in context.main_section:
frappe.local.flags.redirect_location = (
context.main_section.split("<!-- redirect:")[1].split("-->")[0].strip()
context.main_section.split("<!-- redirect:", 2)[1].split("-->", 1)[0].strip()
)
raise frappe.Redirect

View file

@ -18,7 +18,7 @@ def make_view_log(path, referrer=None, browser=None, version=None, url=None, use
user_agent = request_dict.get("environ", {}).get("HTTP_USER_AGENT")
if referrer:
referrer = referrer.split("?")[0]
referrer = referrer.split("?", 1)[0]
is_unique = True
if referrer.startswith(url):