Simplify code by making use of early returns

This commit is contained in:
Dunedan 2024-09-07 06:38:52 +02:00
parent f4c40b740c
commit eeb502c115
Signed by untrusted user: Dunedan
GPG Key ID: 885B16854284E0B2
2 changed files with 58 additions and 57 deletions

View File

@ -98,21 +98,19 @@ class Extractor:
break
else:
for filemask in self.include_masks:
if pathmatch(filemask, filename):
filepath = os.path.join(directory_absolute_path, filename)
for (
message,
plural,
context,
position,
comments,
) in self.extract_from_file(filepath):
if empty_string_pattern.match(message):
continue
if not pathmatch(filemask, filename):
continue
if " " in filename or "\t" in filename:
filename = "\u2068" + filename + "\u2069"
yield message, plural, context, (filename, position), comments
filepath = os.path.join(directory_absolute_path, filename)
for message, plural, context, position, comments in self.extract_from_file(
filepath
):
if empty_string_pattern.match(message):
continue
if " " in filename or "\t" in filename:
filename = "\u2068" + filename + "\u2069"
yield message, plural, context, (filename, position), comments
def extract_from_file(self, filepath):
"""Extract messages from a specific file.
@ -457,35 +455,35 @@ class XmlExtractor(Extractor):
for keyword in self.keywords:
for element in xml_document.iter(keyword):
lineno = element.sourceline
if element.text is not None:
comments = []
if "extractJson" in self.keywords[keyword]:
json_extractor = self.get_json_extractor()
json_extractor.set_options(self.keywords[keyword]["extractJson"])
for message, context in json_extractor.extract_from_string(
element.text
):
yield message, None, context, lineno, comments
if element.text is None:
continue
comments = []
if "extractJson" in self.keywords[keyword]:
json_extractor = self.get_json_extractor()
json_extractor.set_options(self.keywords[keyword]["extractJson"])
for message, context in json_extractor.extract_from_string(element.text):
yield message, None, context, lineno, comments
else:
context = None
if "context" in element.attrib:
context = str(element.get("context"))
elif "tagAsContext" in self.keywords[keyword]:
context = keyword
elif "customContext" in self.keywords[keyword]:
context = self.keywords[keyword]["customContext"]
if "comment" in element.attrib:
comment = element.get("comment")
comment = " ".join(
comment.split()
) # Remove tabs, line breaks and unecessary spaces.
comments.append(comment)
if "splitOnWhitespace" in self.keywords[keyword]:
for split_text in element.text.split():
# split on whitespace is used for token lists, there, a
# leading '-' means the token has to be removed, so it's not
# to be processed here either
if split_text[0] != "-":
yield str(split_text), None, context, lineno, comments
else:
context = None
if "context" in element.attrib:
context = str(element.get("context"))
elif "tagAsContext" in self.keywords[keyword]:
context = keyword
elif "customContext" in self.keywords[keyword]:
context = self.keywords[keyword]["customContext"]
if "comment" in element.attrib:
comment = element.get("comment")
comment = " ".join(
comment.split()
) # Remove tabs, line breaks and unecessary spaces.
comments.append(comment)
if "splitOnWhitespace" in self.keywords[keyword]:
for split_text in element.text.split():
# split on whitespace is used for token lists, there, a
# leading '-' means the token has to be removed, so it's not
# to be processed here either
if split_text[0] != "-":
yield str(split_text), None, context, lineno, comments
else:
yield str(element.text), None, context, lineno, comments
yield str(element.text), None, context, lineno, comments

View File

@ -35,18 +35,21 @@ def warn_about_untouched_mods():
mods_root_folder = os.path.join(PROJECT_ROOT_DIRECTORY, "binaries", "data", "mods")
untouched_mods = {}
for mod_folder in os.listdir(mods_root_folder):
if mod_folder[0] != "_" and mod_folder[0] != ".":
if not os.path.exists(os.path.join(mods_root_folder, mod_folder, L10N_FOLDER_NAME)):
untouched_mods[mod_folder] = (
f"There is no '{L10N_FOLDER_NAME}' folder in the root folder of this mod."
)
elif not os.path.exists(
os.path.join(mods_root_folder, mod_folder, L10N_FOLDER_NAME, messages_filename)
):
untouched_mods[mod_folder] = (
f"There is no '{messages_filename}' file within the '{L10N_FOLDER_NAME}' "
f"folder in the root folder of this mod."
)
if mod_folder.startswith(("_", ".")):
continue
if not os.path.exists(os.path.join(mods_root_folder, mod_folder, L10N_FOLDER_NAME)):
untouched_mods[mod_folder] = (
f"There is no '{L10N_FOLDER_NAME}' folder in the root folder of this mod."
)
elif not os.path.exists(
os.path.join(mods_root_folder, mod_folder, L10N_FOLDER_NAME, messages_filename)
):
untouched_mods[mod_folder] = (
f"There is no '{messages_filename}' file within the '{L10N_FOLDER_NAME}' "
f"folder in the root folder of this mod."
)
if untouched_mods:
print("Warning: No messages were extracted from the following mods:")
for mod in untouched_mods: