Skip to content

Commit

Permalink
Report unused enum members after CLDR data scan
Browse files Browse the repository at this point in the history
We should at least know when members of QLocale's enums aren't adding
any value, and it may make sense to deprecate the unused ones.

Change-Id: Icf202f81d2a35904c13ccdc202d41985bcb3f2e6
Reviewed-by: Cristian Maureira-Fredes <[email protected]>
  • Loading branch information
ediosyncratic committed Jun 7, 2021
1 parent cce3445 commit 1a49d7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion util/locale_database/cldr2qlocalexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def main(args, out, err):
writer.likelySubTags(reader.likelySubTags())
writer.locales(reader.readLocales(calendars), calendars)

writer.close()
writer.close(err.write)
return 0

if __name__ == '__main__':
Expand Down
33 changes: 30 additions & 3 deletions util/locale_database/qlocalexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ def enumData(self):
self.__enumTable('language', language_map)
self.__enumTable('script', script_map)
self.__enumTable('territory', territory_map)
# Prepare to detect any unused codes (see __writeLocale(), close()):
self.__languages = set(p[1] for p in language_map.values()
if not p[1].isspace())
self.__scripts = set(p[1] for p in script_map.values()
if p[1] != 'ZZ')
self.__territories = set(p[1] for p in territory_map.values()
if p[1] != 'Zzzz')

def likelySubTags(self, entries):
self.__openTag('likelySubtags')
Expand All @@ -351,13 +358,13 @@ def likelySubTags(self, entries):
def locales(self, locales, calendars):
self.__openTag('localeList')
self.__openTag('locale')
Locale.C(calendars).toXml(self.inTag, calendars)
self.__writeLocale(Locale.C(calendars), calendars)
self.__closeTag('locale')
keys = locales.keys()
keys.sort()
for key in keys:
self.__openTag('locale')
locales[key].toXml(self.inTag, calendars)
self.__writeLocale(locales[key], calendars)
self.__closeTag('locale')
self.__closeTag('localeList')

Expand All @@ -367,11 +374,25 @@ def version(self, cldrVersion):
def inTag(self, tag, text):
self.__write('<{0}>{1}</{0}>'.format(tag, text))

def close(self):
def close(self, grumble):
"""Finish writing and grumble any issues discovered."""
if self.__rawOutput != self.__complain:
self.__write('</localeDatabase>')
self.__rawOutput = self.__complain

if self.__languages or self.__scripts or self.territories:
grumble('Some enum members are unused, corresponding to these tags:\n')
import textwrap
def kvetch(kind, seq, g = grumble, w = textwrap.wrap):
g('\n\t'.join(w(' {}: '.format(kind) + ', '.join(seq), width=80)) + '\n')
if self.__languages:
kvetch('Languages', self.__languages)
if self.__scripts:
kvetch('Scripts', self.__scripts)
if self.__territories:
kvetch('Territories', self.__territories)
grumble('It may make sense to deprecate them.')

# Implementation details
@staticmethod
def __printit(text):
Expand All @@ -398,6 +419,12 @@ def __likelySubTag(self, tag, likely):
# self.inTag('variant', likely[3])
self.__closeTag(tag)

def __writeLocale(self, locale, calendars):
locale.toXml(self.inTag, calendars)
self.__languages.discard(locale.language_code)
self.__scripts.discard(locale.script_code)
self.__territories.discard(locale.territory_code)

def __openTag(self, tag):
self.__write('<{}>'.format(tag))
def __closeTag(self, tag):
Expand Down

0 comments on commit 1a49d7d

Please sign in to comment.