Use startswith for more pythonic code.

Swap out while True with for line in f.readlines. This will now termitate without having to catch a StopIteration error.

CURA-9141
This commit is contained in:
j.delarago 2022-05-13 15:08:11 +02:00 committed by Ghostkeeper
parent b04f3bd587
commit 63e6b7704c
No known key found for this signature in database
GPG key ID: 68F39EA88EEED5FF

View file

@ -28,33 +28,31 @@ def parsePOFile(filename: str) -> List[Msg]:
messages = [] messages = []
with open(filename) as f: with open(filename) as f:
iterator = iter(f.readlines()) iterator = iter(f.readlines())
while True: for line in iterator:
try: if line.startswith("msgctxt"):
line = next(iterator) # Start of a translation item block
if line[0:7] == "msgctxt": msg = Msg()
# Start of a translation item block msg.msgctxt = line
msg = Msg()
msg.msgctxt = line
while True: while True:
line = next(iterator) line = next(iterator)
if line[0:5] == "msgid": if line.startswith("msgid"):
msg.msgid = line msg.msgid = line
break break
while True: while True:
# msgstr can be split over multiple lines # msgstr can be split over multiple lines
line = next(iterator) line = next(iterator)
if line == "\n": if line == "\n":
break break
if line[0:6] == "msgstr": if line.startswith("msgstr"):
msg.msgstr = line msg.msgstr = line
else: else:
msg.msgstr += line msg.msgstr += line
messages.append(msg) messages.append(msg)
except StopIteration:
return messages return messages
def getDifferentMessages(messages_original: List[Msg], messages_new: List[Msg]) -> List[Msg]: def getDifferentMessages(messages_original: List[Msg], messages_new: List[Msg]) -> List[Msg]:
@ -75,32 +73,27 @@ def updatePOFile(input_filename: str, output_filename: str, messages: List[Msg])
# Takes a list of changed messages and writes a copy of input file with updated message strings # Takes a list of changed messages and writes a copy of input file with updated message strings
with open(input_filename, "r") as input_file, open(output_filename, "w") as output_file: with open(input_filename, "r") as input_file, open(output_filename, "w") as output_file:
iterator = iter(input_file.readlines()) iterator = iter(input_file.readlines())
while True: for line in iterator:
try: output_file.write(line)
line = next(iterator) if line.startswith("msgctxt"):
output_file.write(line) # Start of translation block
if line[0: 7] == "msgctxt": msgctxt = line
# Start of translation block
msgctxt = line
msgid = next(iterator) msgid = next(iterator)
output_file.write(msgid) output_file.write(msgid)
# Check for updated version of msgstr # Check for updated version of msgstr
message = list(filter(lambda m: m.msgctxt == msgctxt and m.msgid == msgid, messages)) message = list(filter(lambda m: m.msgctxt == msgctxt and m.msgid == msgid, messages))
if message and message[0]: if message and message[0]:
# Write update translation # Write update translation
output_file.write(message[0].msgstr) output_file.write(message[0].msgstr)
# Skip lines until next translation. This should skip multiline msgstr # Skip lines until next translation. This should skip multiline msgstr
while True: while True:
line = next(iterator) line = next(iterator)
if line == "\n": if line == "\n":
output_file.write(line) output_file.write(line)
break break
except StopIteration:
return
if __name__ == "__main__": if __name__ == "__main__":