QLocaleXML: Improve documentation, tidy up a bit

Omit parentheses round what python will form into a tuple anyway.
Include trailing commas on last entries of tuples so adding future
entries don't drag the existing line into their diffs.
Let the writer's tag-opener handle attributes, if supplied.
Clean up spacing in some doc-strings.
This is all preparation for further changes, to limit their diffs.

Change-Id: I989ae28bbd235b2af9c1d72467d4741c4f1f20ae
Reviewed-by: Mate Barany <mate.barany@qt.io>
This commit is contained in:
Edward Welbourne 2024-04-23 15:50:59 +02:00
parent 9534341654
commit 72a7dddc25

View File

@ -110,7 +110,7 @@ class QLocaleXmlReader (object):
def msLandIanas(self): def msLandIanas(self):
kid = self.__firstChildText kid = self.__firstChildText
for elt in self.__eachEltInGroup(self.root, 'windowsZone', 'msLandZones'): for elt in self.__eachEltInGroup(self.root, 'windowsZone', 'msLandZones'):
yield (kid(elt, 'msid'), kid(elt, 'territorycode'), kid(elt, 'ianaids')) yield kid(elt, 'msid'), kid(elt, 'territorycode'), kid(elt, 'ianaids')
def languageIndices(self, locales): def languageIndices(self, locales):
index = 0 index = 0
@ -251,17 +251,16 @@ class Spacer (object):
First argument, indent, is either None (its default, for First argument, indent, is either None (its default, for
'minifying'), an ingeter (number of spaces) or the unit of 'minifying'), an ingeter (number of spaces) or the unit of
text that is to be used for each indentation level (e.g. '\t' text that is to be used for each indentation level (e.g. '\t'
to use tabs). If indent is None, no indentation is added, nor to use tabs). If indent is None, no indentation is added, nor
are line-breaks; otherwise, self(text), for non-empty text, are line-breaks; otherwise, self(text), for non-empty text,
shall end with a newline and begin with indentation. shall end with a newline and begin with indentation.
Second argument, initial, is the initial indentation; it is Second argument, initial, is the initial indentation; it is
ignored if indent is None. Indentation increases after each ignored if indent is None. Indentation increases after each
call to self(text) in which text starts with a tag and doesn't call to self(text) in which text starts with a tag and doesn't
include its end-tag; indentation decreases if text starts with include its end-tag; indentation decreases if text starts with
an end-tag. The text is not parsed any more carefully than an end-tag. The text is not parsed any more carefully than
just described. just described."""
"""
if indent is None: if indent is None:
self.__call = lambda x: x self.__call = lambda x: x
else: else:
@ -287,6 +286,10 @@ class Spacer (object):
return self.__call(line) return self.__call(line)
class QLocaleXmlWriter (object): class QLocaleXmlWriter (object):
"""Save the full set of locale data to a QLocaleXML file.
The output saved by this should conform to qlocalexml.rnc's
schema."""
def __init__(self, save = None, space = Spacer(4)): def __init__(self, save = None, space = Spacer(4)):
"""Set up to write digested CLDR data as QLocale XML. """Set up to write digested CLDR data as QLocale XML.
@ -442,7 +445,10 @@ class QLocaleXmlWriter (object):
self.__scripts.discard(locale.script_code) self.__scripts.discard(locale.script_code)
self.__territories.discard(locale.territory_code) self.__territories.discard(locale.territory_code)
def __openTag(self, tag): def __openTag(self, tag, **attrs):
if attrs:
text = ', '.join(f'{k}="{v}"' for k, v in attrs.items())
tag = f'{tag} {text}'
self.__write(f'<{tag}>') self.__write(f'<{tag}>')
def __closeTag(self, tag): def __closeTag(self, tag):
self.__write(f'</{tag}>') self.__write(f'</{tag}>')
@ -489,7 +495,8 @@ class Locale (object):
"longTimeFormat", "shortTimeFormat", "longTimeFormat", "shortTimeFormat",
'byte_unit', 'byte_si_quantified', 'byte_iec_quantified', 'byte_unit', 'byte_si_quantified', 'byte_iec_quantified',
"currencyIsoCode", "currencySymbol", "currencyDisplayName", "currencyIsoCode", "currencySymbol", "currencyDisplayName",
"currencyFormat", "currencyNegativeFormat") "currencyFormat", "currencyNegativeFormat",
)
# Day-of-Week numbering used by Qt: # Day-of-Week numbering used by Qt:
__qDoW = {"mon": 1, "tue": 2, "wed": 3, "thu": 4, "fri": 5, "sat": 6, "sun": 7} __qDoW = {"mon": 1, "tue": 2, "wed": 3, "thu": 4, "fri": 5, "sat": 6, "sun": 7}
@ -498,12 +505,15 @@ class Locale (object):
def fromXmlData(cls, lookup, calendars=('gregorian',)): def fromXmlData(cls, lookup, calendars=('gregorian',)):
"""Constructor from the contents of XML elements. """Constructor from the contents of XML elements.
Single parameter, lookup, is called with the names of XML First parameter, lookup, is called with the names of XML elements that
elements that should contain the relevant data, within a CLDR should contain the relevant data, within a QLocaleXML locale element
locale element (within a localeList element); these names are (within a localeList element); these names mostly match the attributes
used for the attributes of the object constructed. Attribute of the object constructed. Its return must be the full text of the
values are obtained by suitably digesting the returned element first child DOM node element with the given name. Attribute values are
texts.\n""" obtained by suitably digesting the returned element texts.
Optional second parameter, calendars, is a sequence of calendars for
which data is to be retrieved."""
data = {} data = {}
for k in cls.__asint: for k in cls.__asint:
data[k] = int(lookup(k)) data[k] = int(lookup(k))
@ -554,7 +564,7 @@ class Locale (object):
'longDateFormat', 'shortDateFormat', 'longDateFormat', 'shortDateFormat',
'longTimeFormat', 'shortTimeFormat', 'longTimeFormat', 'shortTimeFormat',
'currencyIsoCode', 'currencySymbol', 'currencyDisplayName', 'currencyIsoCode', 'currencySymbol', 'currencyDisplayName',
'currencyFormat', 'currencyNegativeFormat' 'currencyFormat', 'currencyNegativeFormat',
) + tuple(self.propsMonthDay('days')) + tuple( ) + tuple(self.propsMonthDay('days')) + tuple(
'_'.join((k, cal)) '_'.join((k, cal))
for k in self.propsMonthDay('months') for k in self.propsMonthDay('months')