Created
December 7, 2011 21:05
-
-
Save Repentinus/1444654 to your computer and use it in GitHub Desktop.
Check and sort a translation of the PDFreaders petition.
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
#! /usr/bin/env python | |
# Copyright (C) 2011 Heiki "Repentinus" Ojasild. | |
# Copying and distribution of this file, with or without modification, | |
# are permitted in any medium without royalty provided the copyright | |
# notice and this notice are preserved. This file is offered as-is, | |
# without any warranty. | |
# This is used to check that the translator has not introduced an error to | |
# the petition country list. It also sorts the countries alphabetically. | |
# Usage: script petition.??.xhtml petition.en.xhtml locale | |
# The sorted output will be written into petition.XX.xhtml | |
from functools import cmp_to_key | |
from lxml import etree | |
import locale | |
import sys | |
if len(sys.argv) != 4: | |
print("This script takes three command line arguments.") | |
exit() | |
parser = etree.XMLParser(remove_blank_text=True) | |
try: | |
translation = etree.parse(sys.argv[1], parser) | |
original = etree.parse(sys.argv[2], parser) | |
except: | |
print("An error has occurred while processing input.") | |
exit() | |
for i in translation.iter(): | |
if i.text: | |
i.text = ' '.join(i.text.split()) | |
authoritative = [] | |
for i in original.iter("select"): | |
if i.get("id") == "country": | |
for j in i.iter("option"): | |
if j.get("value"): | |
authoritative.append(j.get("value")) | |
else: | |
authoritative.append("" + j.text) | |
break | |
neu = [] | |
def mcmp(a, b): | |
return locale.strcoll(a[0], b[0]) | |
for i in translation.iter("select"): | |
if i.get("id") == "country": | |
for j in i.iter("option"): | |
if j.get("value") in authoritative: | |
neu.append((j.text, j)) | |
i.remove(j) | |
else: | |
print(j.get("value") + " is missing from the authoritative list.") | |
locale.setlocale(locale.LC_ALL, sys.argv[3]) | |
neu = sorted(neu, key=cmp_to_key(mcmp)) | |
for j in neu: | |
i.append(j[1]) | |
translation.write("petition.XX.xhtml", encoding="UTF-8", xml_declaration=True, method="xml", pretty_print=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment