Created
July 19, 2019 10:07
-
-
Save terapyon/395f51ad18313ab34c082e3061d31226 to your computer and use it in GitHub Desktop.
Plone po file to csv file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
po file to CSV file for untranselated | |
""" | |
from collections import defaultdict | |
import csv | |
EMPTY_ONLY = True | |
CSV_HEADERS = ("uses", "default", "msgid", "fuzzy", "msgstr") | |
def po2block(filename): | |
"""po filename to msg block list (generator) | |
""" | |
with open(filename, "r") as f: | |
block_number = 0 | |
block = defaultdict(list) | |
for i, line in enumerate(f): | |
# if i > 50: | |
# break | |
if not line.strip(): | |
if block_number: | |
yield block | |
block_number += 1 | |
block = defaultdict(list) | |
continue | |
elif block_number == 0: | |
continue | |
if line.startswith("#. Default: "): | |
block["default"].append(line.split("#. Default: ", 1)[1].strip()) | |
elif line.startswith("#: "): | |
block["uses"].append(line.split("#: ", 1)[1].strip()) | |
elif line.startswith("#, fuzzy"): | |
block["fuzzy"].append("1") | |
elif line.startswith("msgid "): | |
block["msgid"].append(line.split("msgid ", 1)[1].strip()[1:-1]) | |
elif line.startswith("msgstr "): | |
block["msgstr"].append(line.split("msgstr ", 1)[1].strip()[1:-1]) | |
def _block_line(block): | |
for key in CSV_HEADERS: | |
# print(key, block.get(key)) | |
yield " ".join(block.get(key, [])) | |
def block2lines(blocks, output): | |
"""msg blocks to csv lines | |
""" | |
with open(output, "w", newline="") as f: | |
writer = csv.writer(f, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL) | |
for block in blocks: | |
if not EMPTY_ONLY: | |
writer.writerow(_block_line(block)) | |
elif ( | |
block.get("fuzzy", [""])[0] == "1" or block.get("msgstr", [""])[0] == "" | |
): | |
writer.writerow(_block_line(block)) | |
def main(input, output): | |
blocks = po2block(input) | |
block2lines(blocks, output) | |
if __name__ == "__main__": | |
import sys | |
args = sys.argv | |
if len(args) < 3: | |
print("Nedd args for po filename and output filename") | |
else: | |
main(args[1], args[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment