Apply a common style to the main()s of locale database programs

Include documentation in both, using common phrasing. Take sys.argv as
a parameter, along with sys.stdout and sys.stderr, so that we can
invoke them from python when importing into a python session to debug
or test. Supply the script name to the argument parser as prog, so it
can correctly report it and forward the rest of argv to parse_args().
Remove comments anticipating one of the several calendars we don't yet
support; the existing entries suffice to make clear what shall be
needed when we get round to adding more.

Change-Id: I2cebd385679e3c84d4ccf899e60091ac823ad10d
Reviewed-by: Mate Barany <mate.barany@qt.io>
This commit is contained in:
Edward Welbourne 2024-03-07 18:04:46 +01:00
parent 165f638783
commit f83206229e
2 changed files with 30 additions and 9 deletions

View File

@ -37,17 +37,25 @@ All the scripts mentioned support --help to tell you how to use them.
""" """
from pathlib import Path from pathlib import Path
import sys
import argparse import argparse
from cldr import CldrReader from cldr import CldrReader
from qlocalexml import QLocaleXmlWriter from qlocalexml import QLocaleXmlWriter
def main(out, err): def main(argv, out, err):
all_calendars = ['gregorian', 'persian', 'islamic'] # 'hebrew' """Generate a QLocaleXML file from CLDR data.
Takes sys.argv, sys.stdout, sys.stderr (or equivalents) as
arguments. In argv[1:], it expects the root of the CLDR data
directory as first parameter and the name of the file in which to
save QLocaleXML data as second parameter. It accepts a --calendars
option to select which calendars to support (all available by
default)."""
all_calendars = ['gregorian', 'persian', 'islamic']
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog=Path(argv[0]).name,
description='Generate QLocaleXML from CLDR data.', description='Generate QLocaleXML from CLDR data.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('cldr_path', help='path to the root of the CLDR tree') parser.add_argument('cldr_path', help='path to the root of the CLDR tree')
@ -57,7 +65,7 @@ def main(out, err):
nargs='+', metavar='CALENDAR', nargs='+', metavar='CALENDAR',
choices=all_calendars, default=all_calendars) choices=all_calendars, default=all_calendars)
args = parser.parse_args() args = parser.parse_args(argv[1:])
root = Path(args.cldr_path) root = Path(args.cldr_path)
root_xml_path = 'common/main/root.xml' root_xml_path = 'common/main/root.xml'
@ -90,4 +98,5 @@ def main(out, err):
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main(sys.stdout, sys.stderr)) import sys
sys.exit(main(sys.argv, sys.stdout, sys.stderr))

View File

@ -521,17 +521,29 @@ class LocaleHeaderWriter (SourceFileEditor):
out('\n };\n') out('\n };\n')
def main(out, err): def main(argv, out, err):
"""Updates QLocale's CLDR data from a QLocaleXML file.
Takes sys.argv, sys.stdout, sys.stderr (or equivalents) as
arguments. In argv[1:] it expects the QLocaleXML file as first
parameter and the ISO 639-3 data table as second
parameter. Accepts the root of the qtbase checkout as third
parameter (default is inferred from this script's path) and a
--calendars option to select which calendars to support (all
available by default).
Updates various src/corelib/t*/q*_data_p.h files within the qtbase
checkout to contain data extracted from the QLocaleXML file."""
calendars_map = { calendars_map = {
# CLDR name: Qt file name fragment # CLDR name: Qt file name fragment
'gregorian': 'roman', 'gregorian': 'roman',
'persian': 'jalali', 'persian': 'jalali',
'islamic': 'hijri', 'islamic': 'hijri',
# 'hebrew': 'hebrew'
} }
all_calendars = list(calendars_map.keys()) all_calendars = list(calendars_map.keys())
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog=Path(argv[0]).name,
description='Generate C++ code from CLDR data in QLocaleXML form.', description='Generate C++ code from CLDR data in QLocaleXML form.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('input_file', help='input XML file name', parser.add_argument('input_file', help='input XML file name',
@ -543,7 +555,7 @@ def main(out, err):
parser.add_argument('--calendars', help='select calendars to emit data for', parser.add_argument('--calendars', help='select calendars to emit data for',
nargs='+', metavar='CALENDAR', nargs='+', metavar='CALENDAR',
choices=all_calendars, default=all_calendars) choices=all_calendars, default=all_calendars)
args = parser.parse_args() args = parser.parse_args(argv[1:])
qlocalexml = args.input_file qlocalexml = args.input_file
qtsrcdir = Path(args.qtbase_path) qtsrcdir = Path(args.qtbase_path)
@ -624,4 +636,4 @@ def main(out, err):
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys
sys.exit(main(sys.stdout, sys.stderr)) sys.exit(main(sys.argv, sys.stdout, sys.stderr))