i18n Python scripts: improve fg-copy-weather-scenarios-to-default-locale
- Use the new flightgear.meta.strutils.simplify() function. - Automatically add a comment to the generated file to indicate that it was automatically generated and shouldn't be manually modified, etc. - Improve error handling: abort with a clear error message if any of the 'id', 'name' and 'desc' children of a 'scenario' element is problematic (empty or missing, for instance). - Rename make_xml_leaf() to makeXmlLeaf() + other minor renaming.
This commit is contained in:
parent
52e84741d0
commit
d259ec0b76
1 changed files with 53 additions and 18 deletions
|
@ -20,10 +20,12 @@
|
|||
import argparse
|
||||
import locale
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import sys
|
||||
import textwrap
|
||||
import lxml.etree as ET
|
||||
|
||||
import flightgear.meta.strutils as strutils
|
||||
from catalog import sgprops
|
||||
|
||||
PROGNAME = os.path.basename(sys.argv[0])
|
||||
|
@ -50,34 +52,67 @@ Copy weather scenario descriptions to the default translation XML""",
|
|||
|
||||
return parser.parse_args(namespace=params)
|
||||
|
||||
# create an xml node with text content
|
||||
def make_xml_leaf(name, text):
|
||||
|
||||
def insertInitialComment(root_elt, rel_input_path):
|
||||
"""Insert an XML comment before element 'root_elt'."""
|
||||
s = textwrap.dedent("""\
|
||||
This file was automatically generated from {input_file} using the
|
||||
{progname} script from FGMeta. Modifications should be done either in
|
||||
{input_file} or in that script.""".format(
|
||||
progname=PROGNAME,
|
||||
input_file=os.path.join("$FG_ROOT", rel_input_path)))
|
||||
filled_paragraph = textwrap.fill(s, width=79)
|
||||
comment_pseudo_element = ET.Comment(
|
||||
" !!! Don't modify this file manually. !!!\n" + filled_paragraph + " ")
|
||||
root_elt.addprevious(comment_pseudo_element)
|
||||
|
||||
|
||||
def stringifyChildValue(node, child):
|
||||
# The 'or ""' is needed because an empty node is returned as None!
|
||||
return strutils.simplify(node.getValue(child, "") or "")
|
||||
|
||||
|
||||
def makeXmlLeaf(name, text):
|
||||
"""Create an XML element with text contents."""
|
||||
leaf = ET.Element(name)
|
||||
leaf.text = '' if text is None else str(text)
|
||||
return leaf
|
||||
|
||||
simplify_cre = re.compile(r"\s+")
|
||||
def simplifyString(input):
|
||||
return simplify_cre.sub(" ", input.strip())
|
||||
|
||||
def copyWeatherScenarios(fgdata):
|
||||
environment_node = sgprops.readProps(os.path.join(fgdata, "Environment", "environment.xml"))
|
||||
rel_input_path = os.path.join("Environment", "environment.xml")
|
||||
environment_node = sgprops.readProps(os.path.join(fgdata, rel_input_path))
|
||||
scenarios = environment_node.getChild('weather-scenarios')
|
||||
|
||||
result = ET.Element("PropertyList")
|
||||
|
||||
for s in scenarios.getChildren("scenario"):
|
||||
scenarioId = s.getValue("id", None)
|
||||
name = simplifyString(s.getValue("name", None))
|
||||
desc = simplifyString(s.getValue("description", None))
|
||||
root = ET.Element("PropertyList")
|
||||
insertInitialComment(root, rel_input_path)
|
||||
|
||||
result.append(make_xml_leaf(scenarioId + "-name", name))
|
||||
result.append(make_xml_leaf(scenarioId + "-desc", desc))
|
||||
for scen_idx, scen_node in enumerate(scenarios.getChildren("scenario")):
|
||||
scenarioId = scen_node.getValue("id", None)
|
||||
if (not scenarioId) or scenarioId != strutils.simplify(scenarioId):
|
||||
sys.exit(
|
||||
"{prg}: 'scenario' element number {i} has a missing, empty "
|
||||
"or suspiciously-formatted 'id' child; aborting.".format(
|
||||
prg=PROGNAME, i=scen_idx+1))
|
||||
|
||||
default_trans_file = os.path.join(fgdata, "Translations", "default", "weather-scenarios.xml")
|
||||
name = stringifyChildValue(scen_node, "name")
|
||||
desc = stringifyChildValue(scen_node, "description")
|
||||
|
||||
doc = ET.ElementTree(result)
|
||||
doc.write(default_trans_file, encoding='utf-8', xml_declaration=True, pretty_print=True)
|
||||
if not (name and desc):
|
||||
sys.exit(
|
||||
"{prg}: scenario '{scen}' has an empty or missing name or "
|
||||
"description after string simplification; aborting.".format(
|
||||
prg=PROGNAME, scen=scenarioId))
|
||||
|
||||
root.append(makeXmlLeaf(scenarioId + "-name", name))
|
||||
root.append(makeXmlLeaf(scenarioId + "-desc", desc))
|
||||
|
||||
default_trans_file = os.path.join(fgdata, "Translations", "default",
|
||||
"weather-scenarios.xml")
|
||||
|
||||
doc = ET.ElementTree(root)
|
||||
doc.write(default_trans_file, encoding='utf-8',
|
||||
xml_declaration=True, pretty_print=True)
|
||||
|
||||
|
||||
def main():
|
||||
|
|
Loading…
Reference in a new issue