2020-06-18 00:03:19 +08:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (C) 2020 James Turner
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import locale
|
|
|
|
import os
|
2020-06-18 17:16:06 +08:00
|
|
|
import re
|
2020-06-18 17:26:54 +08:00
|
|
|
import sys
|
|
|
|
import textwrap
|
2020-06-18 00:03:19 +08:00
|
|
|
import lxml.etree as ET
|
|
|
|
|
2020-06-18 17:26:54 +08:00
|
|
|
import flightgear.meta.strutils as strutils
|
2020-06-19 20:19:41 +08:00
|
|
|
from flightgear.meta import sgprops
|
2020-06-18 00:03:19 +08:00
|
|
|
|
|
|
|
PROGNAME = os.path.basename(sys.argv[0])
|
|
|
|
|
|
|
|
|
|
|
|
def processCommandLine():
|
|
|
|
params = argparse.Namespace()
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
usage="""\
|
|
|
|
%(prog)s [OPTION ...] FGDATA
|
|
|
|
Copy weather scenario descriptions to the default translation XML""",
|
|
|
|
description="""\
|
|
|
|
""",
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
# I want --help but not -h (it might be useful for something else)
|
|
|
|
add_help=False)
|
|
|
|
|
|
|
|
parser.add_argument("fgdata", metavar="FGDATA",
|
|
|
|
help="""\
|
|
|
|
location of FGData""")
|
|
|
|
parser.add_argument("--help", action="help",
|
|
|
|
help="display this message and exit")
|
|
|
|
|
|
|
|
return parser.parse_args(namespace=params)
|
|
|
|
|
2020-06-18 17:26:54 +08:00
|
|
|
|
|
|
|
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."""
|
2020-06-18 00:03:19 +08:00
|
|
|
leaf = ET.Element(name)
|
2020-06-18 17:16:06 +08:00
|
|
|
leaf.text = '' if text is None else str(text)
|
2020-06-18 00:03:19 +08:00
|
|
|
return leaf
|
|
|
|
|
2020-06-18 17:16:06 +08:00
|
|
|
|
2020-06-18 00:03:19 +08:00
|
|
|
def copyWeatherScenarios(fgdata):
|
2020-06-18 17:26:54 +08:00
|
|
|
rel_input_path = os.path.join("Environment", "environment.xml")
|
|
|
|
environment_node = sgprops.readProps(os.path.join(fgdata, rel_input_path))
|
2020-06-18 00:03:19 +08:00
|
|
|
scenarios = environment_node.getChild('weather-scenarios')
|
|
|
|
|
2020-06-18 17:26:54 +08:00
|
|
|
root = ET.Element("PropertyList")
|
|
|
|
insertInitialComment(root, rel_input_path)
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
name = stringifyChildValue(scen_node, "name")
|
|
|
|
desc = stringifyChildValue(scen_node, "description")
|
|
|
|
|
|
|
|
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))
|
2020-06-18 00:03:19 +08:00
|
|
|
|
2020-06-18 17:26:54 +08:00
|
|
|
root.append(makeXmlLeaf(scenarioId + "-name", name))
|
|
|
|
root.append(makeXmlLeaf(scenarioId + "-desc", desc))
|
2020-06-18 00:03:19 +08:00
|
|
|
|
2020-06-18 17:26:54 +08:00
|
|
|
default_trans_file = os.path.join(fgdata, "Translations", "default",
|
|
|
|
"weather-scenarios.xml")
|
2020-06-18 00:03:19 +08:00
|
|
|
|
2020-06-18 17:26:54 +08:00
|
|
|
doc = ET.ElementTree(root)
|
|
|
|
doc.write(default_trans_file, encoding='utf-8',
|
|
|
|
xml_declaration=True, pretty_print=True)
|
2020-06-18 00:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global params
|
|
|
|
|
|
|
|
locale.setlocale(locale.LC_ALL, '')
|
|
|
|
params = processCommandLine()
|
|
|
|
|
|
|
|
copyWeatherScenarios(params.fgdata)
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": main()
|