diff options
Diffstat (limited to 'src/zope/i18n/locales')
-rw-r--r-- | src/zope/i18n/locales/__init__.py | 112 | ||||
-rw-r--r-- | src/zope/i18n/locales/fallbackcollator.py | 2 | ||||
-rw-r--r-- | src/zope/i18n/locales/inheritance.py | 24 | ||||
-rw-r--r-- | src/zope/i18n/locales/provider.py | 5 | ||||
-rw-r--r-- | src/zope/i18n/locales/tests/test_docstrings.py | 19 | ||||
-rw-r--r-- | src/zope/i18n/locales/tests/test_fallbackcollator.py | 13 | ||||
-rw-r--r-- | src/zope/i18n/locales/tests/test_locales.py | 51 | ||||
-rw-r--r-- | src/zope/i18n/locales/tests/test_xmlfactory.py | 11 | ||||
-rw-r--r-- | src/zope/i18n/locales/xmlfactory.py | 159 |
9 files changed, 239 insertions, 157 deletions
diff --git a/src/zope/i18n/locales/__init__.py b/src/zope/i18n/locales/__init__.py index 030c453..27de94c 100644 --- a/src/zope/i18n/locales/__init__.py +++ b/src/zope/i18n/locales/__init__.py @@ -28,13 +28,17 @@ from zope.i18n.interfaces.locales import ILocaleFormat, ILocaleFormatLength from zope.i18n.interfaces.locales import ILocaleOrientation from zope.i18n.interfaces.locales import ILocaleDayContext, ILocaleMonthContext from zope.i18n.format import NumberFormat, DateTimeFormat -from zope.i18n.locales.inheritance import \ - AttributeInheritance, InheritingDictionary, NoParentException +from zope.i18n.locales.inheritance import ( + AttributeInheritance, + InheritingDictionary, + NoParentException, +) from zope.i18n.locales.provider import LocaleProvider, LoadLocaleError # Setup the locale directory from zope import i18n + LOCALEDIR = os.path.join(os.path.dirname(i18n.__file__), "locales", "data") # Global LocaleProvider. We really just need this single one. @@ -63,15 +67,25 @@ FRIDAY = 5 SATURDAY = 6 SUNDAY = 7 -dayMapping = {'mon': 1, 'tue': 2, 'wed': 3, 'thu': 4, - 'fri': 5, 'sat': 6, 'sun': 7} +dayMapping = { + 'mon': 1, + 'tue': 2, + 'wed': 3, + 'thu': 4, + 'fri': 5, + 'sat': 6, + 'sun': 7, +} BC = 1 AD = 2 -calendarAliases = {'islamic': ('arabic',), - 'islamic-civil': ('civil-arabic',), - 'buddhist': ('thai-buddhist', )} +calendarAliases = { + 'islamic': ('arabic',), + 'islamic-civil': ('civil-arabic',), + 'buddhist': ('thai-buddhist',), +} + @implementer(ILocaleIdentity) class LocaleIdentity(object): @@ -102,7 +116,9 @@ class LocaleIdentity(object): <LocaleIdentity (en, None, US, POSIX)> """ - def __init__(self, language=None, script=None, territory=None, variant=None): + def __init__( + self, language=None, script=None, territory=None, variant=None + ): """Initialize object.""" self.language = language self.script = script @@ -110,10 +126,13 @@ class LocaleIdentity(object): self.variant = variant def __repr__(self): - """See zope.i18n.interfaces.ILocaleIdentity - """ - return "<LocaleIdentity (%s, %s, %s, %s)>" %( - self.language, self.script, self.territory, self.variant) + """See zope.i18n.interfaces.ILocaleIdentity""" + return "<LocaleIdentity (%s, %s, %s, %s)>" % ( + self.language, + self.script, + self.territory, + self.variant, + ) @implementer(ILocaleVersion) @@ -151,12 +170,17 @@ class LocaleVersion(object): self.notes = notes def __lt__(self, other): - return ((self.generationDate, self.number) < - (other.generationDate, other.number)) + return (self.generationDate, self.number) < ( + other.generationDate, + other.number, + ) def __eq__(self, other): - return ((self.generationDate, self.number) == - (other.generationDate, other.number)) + return (self.generationDate, self.number) == ( + other.generationDate, + other.number, + ) + @implementer(ILocaleDisplayNames) class LocaleDisplayNames(AttributeInheritance): @@ -233,7 +257,6 @@ class LocaleFormatLength(AttributeInheritance): """Specifies one of the format lengths of a specific quantity, like numbers, dates, times and datetimes.""" - def __init__(self, type=None): """Initialize the object.""" self.type = type @@ -242,7 +265,6 @@ class LocaleFormatLength(AttributeInheritance): @implementer(ILocaleMonthContext) class LocaleMonthContext(AttributeInheritance): - def __init__(self, type=None): """Initialize the object.""" self.type = type @@ -251,7 +273,6 @@ class LocaleMonthContext(AttributeInheritance): @implementer(ILocaleDayContext) class LocaleDayContext(AttributeInheritance): - def __init__(self, type=None): """Initialize the object.""" self.type = type @@ -343,7 +364,9 @@ class LocaleCalendar(AttributeInheritance): def getMonthNames(self): """See zope.i18n.interfaces.ILocaleCalendar""" - return [self.months.get(type, (None, None))[0] for type in range(1, 13)] + return [ + self.months.get(type, (None, None))[0] for type in range(1, 13) + ] def getMonthTypeFromName(self, name): """See zope.i18n.interfaces.ILocaleCalendar""" @@ -353,7 +376,9 @@ class LocaleCalendar(AttributeInheritance): def getMonthAbbreviations(self): """See zope.i18n.interfaces.ILocaleCalendar""" - return [self.months.get(type, (None, None))[1] for type in range(1, 13)] + return [ + self.months.get(type, (None, None))[1] for type in range(1, 13) + ] def getMonthTypeFromAbbreviation(self, abbr): """See zope.i18n.interfaces.ILocaleCalendar""" @@ -491,26 +516,34 @@ class LocaleDates(AttributeInheritance): """ - def getFormatter(self, category, length=None, name=None, - calendar=u"gregorian"): + def getFormatter( + self, category, length=None, name=None, calendar=u"gregorian" + ): """See zope.i18n.interfaces.locales.ILocaleDates""" if category not in (u"date", u"time", u"dateTime"): raise ValueError('Invalid category: %s' % category) - if calendar not in (u"gregorian", u"arabic", u"chinese", - u"civil-arabic", u"hebrew", u"japanese", - u"thai-buddhist"): + if calendar not in ( + u"gregorian", + u"arabic", + u"chinese", + u"civil-arabic", + u"hebrew", + u"japanese", + u"thai-buddhist", + ): raise ValueError('Invalid calendar: %s' % calendar) if length not in (u"short", u"medium", u"long", u"full", None): raise ValueError('Invalid format length: %s' % length) cal = self.calendars[calendar] - formats = getattr(cal, category+'Formats') + formats = getattr(cal, category + 'Formats') if length is None: length = getattr( cal, - 'default'+category[0].upper()+category[1:]+'Format', - list(formats.keys())[0]) + 'default' + category[0].upper() + category[1:] + 'Format', + list(formats.keys())[0], + ) # 'datetime' is always a bit special; we often do not have a length # specification, but we need it for looking up the date and time @@ -528,9 +561,11 @@ class LocaleDates(AttributeInheritance): if category == 'dateTime': date_pat = self.getFormatter( - 'date', length, name, calendar).getPattern() + 'date', length, name, calendar + ).getPattern() time_pat = self.getFormatter( - 'time', length, name, calendar).getPattern() + 'time', length, name, calendar + ).getPattern() pattern = pattern.replace('{1}', date_pat) pattern = pattern.replace('{0}', time_pat) @@ -636,7 +671,8 @@ class LocaleNumbers(AttributeInheritance): length = getattr( self, 'default' + category[0].upper() + category[1:] + 'Format', - list(formats.keys())[0]) + list(formats.keys())[0], + ) formatLength = formats[length] if name is None: @@ -649,8 +685,8 @@ class LocaleNumbers(AttributeInheritance): @implementer(ILocaleOrientation) class LocaleOrientation(AttributeInheritance): - """Implementation of ILocaleOrientation - """ + """Implementation of ILocaleOrientation""" + @implementer(ILocale) class Locale(AttributeInheritance): @@ -682,15 +718,15 @@ class Locale(AttributeInheritance): """ id = self.id - pieces = [x for x in - (id.language, id.script, id.territory, id.variant) - if x] + pieces = [ + x for x in (id.language, id.script, id.territory, id.variant) if x + ] id_string = '_'.join(pieces) # TODO: What about keys??? Where do I get this info from? # Notice that 'pieces' is always empty. pieces = [key + '=' + type for (key, type) in ()] assert not pieces - if pieces: # pragma: no cover + if pieces: # pragma: no cover id_string += '@' + ','.join(pieces) return id_string diff --git a/src/zope/i18n/locales/fallbackcollator.py b/src/zope/i18n/locales/fallbackcollator.py index fc76a58..f747b34 100644 --- a/src/zope/i18n/locales/fallbackcollator.py +++ b/src/zope/i18n/locales/fallbackcollator.py @@ -16,8 +16,8 @@ from unicodedata import normalize -class FallbackCollator: +class FallbackCollator: def __init__(self, locale): pass diff --git a/src/zope/i18n/locales/inheritance.py b/src/zope/i18n/locales/inheritance.py index 912d8c9..5cfe7e8 100644 --- a/src/zope/i18n/locales/inheritance.py +++ b/src/zope/i18n/locales/inheritance.py @@ -23,12 +23,17 @@ __docformat__ = 'restructuredtext' from zope.deprecation import deprecate from zope.interface import implementer -from zope.i18n.interfaces.locales import \ - ILocaleInheritance, IAttributeInheritance, IDictionaryInheritance +from zope.i18n.interfaces.locales import ( + ILocaleInheritance, + IAttributeInheritance, + IDictionaryInheritance, +) + class NoParentException(AttributeError): pass + @implementer(ILocaleInheritance) class Inheritance(object): """A simple base version of locale inheritance. @@ -37,7 +42,6 @@ class Inheritance(object): 'ILocaleInheritance' implementations. """ - # See zope.i18n.interfaces.locales.ILocaleInheritance __parent__ = None @@ -100,18 +104,15 @@ class AttributeInheritance(Inheritance): True """ - def __setattr__(self, name, value): """See zope.i18n.interfaces.locales.ILocaleInheritance""" # If we have a value that can also inherit data from other locales, we # set its parent and name, so that we know how to get to it. - if (ILocaleInheritance.providedBy(value) and - not name.startswith('__')): + if ILocaleInheritance.providedBy(value) and not name.startswith('__'): value.__parent__ = self value.__name__ = name super(AttributeInheritance, self).__setattr__(name, value) - def __getattr__(self, name): """See zope.i18n.interfaces.locales.ILocaleInheritance""" try: @@ -119,9 +120,10 @@ class AttributeInheritance(Inheritance): except NoParentException: # There was simply no parent anymore, so let's raise an error # for good - raise AttributeError("'%s' object (or any of its parents) has no " - "attribute '%s'" % (self.__class__.__name__, - name)) + raise AttributeError( + "'%s' object (or any of its parents) has no " + "attribute '%s'" % (self.__class__.__name__, name) + ) else: value = getattr(selfUp, name) # Since a locale hierarchy never changes after startup, we can @@ -134,7 +136,6 @@ class AttributeInheritance(Inheritance): return value - @implementer(IDictionaryInheritance) class InheritingDictionary(Inheritance, dict): """Implementation of a dictionary that can also inherit values. @@ -197,7 +198,6 @@ class InheritingDictionary(Inheritance, dict): `value` is a deprecated synonym for `values` """ - def __setitem__(self, name, value): """See zope.i18n.interfaces.locales.ILocaleInheritance""" if ILocaleInheritance.providedBy(value): diff --git a/src/zope/i18n/locales/provider.py b/src/zope/i18n/locales/provider.py index 3864b7a..5ed415f 100644 --- a/src/zope/i18n/locales/provider.py +++ b/src/zope/i18n/locales/provider.py @@ -20,6 +20,7 @@ import os from zope.interface import implementer from zope.i18n.interfaces.locales import ILocaleProvider + class LoadLocaleError(Exception): """This error is raised if a locale cannot be loaded.""" @@ -28,7 +29,6 @@ class LoadLocaleError(Exception): class LocaleProvider(object): """A locale provider that gets its data from the XML data.""" - def __init__(self, locale_dir): self._locales = {} self._locale_dir = locale_dir @@ -55,7 +55,8 @@ class LocaleProvider(object): path = os.path.join(self._locale_dir, filename) if not os.path.exists(path): raise LoadLocaleError( - 'The desired locale is not available.\nPath: %s' % path) + 'The desired locale is not available.\nPath: %s' % path + ) # Import here to avoid circular imports from zope.i18n.locales.xmlfactory import LocaleFactory diff --git a/src/zope/i18n/locales/tests/test_docstrings.py b/src/zope/i18n/locales/tests/test_docstrings.py index fa52078..c40b691 100644 --- a/src/zope/i18n/locales/tests/test_docstrings.py +++ b/src/zope/i18n/locales/tests/test_docstrings.py @@ -20,8 +20,8 @@ from zope.i18n.locales.inheritance import NoParentException from zope.i18n.testing import unicode_checker -class LocaleInheritanceStub(AttributeInheritance): +class LocaleInheritanceStub(AttributeInheritance): def __init__(self, nextLocale=None): self.__nextLocale__ = nextLocale @@ -32,11 +32,18 @@ class LocaleInheritanceStub(AttributeInheritance): def test_suite(): - return unittest.TestSuite(( - DocTestSuite('zope.i18n.locales', checker=unicode_checker), - DocTestSuite('zope.i18n.locales.inheritance', checker=unicode_checker), - DocTestSuite('zope.i18n.locales.xmlfactory', checker=unicode_checker), - )) + return unittest.TestSuite( + ( + DocTestSuite('zope.i18n.locales', checker=unicode_checker), + DocTestSuite( + 'zope.i18n.locales.inheritance', checker=unicode_checker + ), + DocTestSuite( + 'zope.i18n.locales.xmlfactory', checker=unicode_checker + ), + ) + ) + if __name__ == '__main__': unittest.main() diff --git a/src/zope/i18n/locales/tests/test_fallbackcollator.py b/src/zope/i18n/locales/tests/test_fallbackcollator.py index 7e10b50..4ecf34d 100644 --- a/src/zope/i18n/locales/tests/test_fallbackcollator.py +++ b/src/zope/i18n/locales/tests/test_fallbackcollator.py @@ -17,11 +17,16 @@ import doctest from zope.i18n.testing import unicode_checker + def test_suite(): - return unittest.TestSuite(( - doctest.DocFileSuite('../fallbackcollator.txt', checker=unicode_checker), - )) + return unittest.TestSuite( + ( + doctest.DocFileSuite( + '../fallbackcollator.txt', checker=unicode_checker + ), + ) + ) + if __name__ == '__main__': unittest.main(defaultTest='test_suite') - diff --git a/src/zope/i18n/locales/tests/test_locales.py b/src/zope/i18n/locales/tests/test_locales.py index 2b100b8..58cecda 100644 --- a/src/zope/i18n/locales/tests/test_locales.py +++ b/src/zope/i18n/locales/tests/test_locales.py @@ -22,8 +22,10 @@ from zope.i18n.locales import locales from zope.i18n.locales.provider import LocaleProvider, LoadLocaleError import zope.i18n + datadir = os.path.join(os.path.dirname(zope.i18n.__file__), 'locales', 'data') + class AbstractTestILocaleProviderMixin(object): """Test the functionality of an implmentation of the ILocaleProvider interface.""" @@ -60,14 +62,14 @@ class AbstractTestILocaleProviderMixin(object): class TestLocaleProvider(AbstractTestILocaleProviderMixin, TestCase): - def _makeNewProvider(self): return LocaleProvider(datadir) def test_loadLocale(self): self.locales.loadLocale(None, None, None) - self.assertEqual(list(self.locales._locales.keys()), - [(None, None, None)]) + self.assertEqual( + list(self.locales._locales.keys()), [(None, None, None)] + ) self.locales.loadLocale('en', None, None) self.assertIn(('en', None, None), self.locales._locales.keys()) @@ -94,27 +96,34 @@ class TestLocaleAndProvider(TestCase): def test_getTimeFormatter(self): formatter = self.locale.dates.getFormatter('time', 'medium') self.assertEqual(formatter.getPattern(), 'h:mm:ss a') - self.assertEqual(formatter.format(datetime.time(12, 30, 10)), - '12:30:10 PM') - self.assertEqual(formatter.parse('12:30:10 PM'), - datetime.time(12, 30, 10)) + self.assertEqual( + formatter.format(datetime.time(12, 30, 10)), '12:30:10 PM' + ) + self.assertEqual( + formatter.parse('12:30:10 PM'), datetime.time(12, 30, 10) + ) def test_getDateFormatter(self): formatter = self.locale.dates.getFormatter('date', 'medium') self.assertEqual(formatter.getPattern(), 'MMM d, yyyy') - self.assertEqual(formatter.format(datetime.date(2003, 1, 2)), - 'Jan 2, 2003') - self.assertEqual(formatter.parse('Jan 2, 2003'), - datetime.date(2003, 1, 2)) + self.assertEqual( + formatter.format(datetime.date(2003, 1, 2)), 'Jan 2, 2003' + ) + self.assertEqual( + formatter.parse('Jan 2, 2003'), datetime.date(2003, 1, 2) + ) def test_getDateTimeFormatter(self): formatter = self.locale.dates.getFormatter('dateTime', 'medium') self.assertEqual(formatter.getPattern(), 'MMM d, yyyy h:mm:ss a') self.assertEqual( formatter.format(datetime.datetime(2003, 1, 2, 12, 30)), - 'Jan 2, 2003 12:30:00 PM') - self.assertEqual(formatter.parse('Jan 2, 2003 12:30:00 PM'), - datetime.datetime(2003, 1, 2, 12, 30)) + 'Jan 2, 2003 12:30:00 PM', + ) + self.assertEqual( + formatter.parse('Jan 2, 2003 12:30:00 PM'), + datetime.datetime(2003, 1, 2, 12, 30), + ) def test_getNumberFormatter(self): formatter = self.locale.numbers.getFormatter('decimal') @@ -126,7 +135,6 @@ class TestLocaleAndProvider(TestCase): class TestGlobalLocaleProvider(TestCase): - def testLoading(self): locales.loadLocale(None, None, None) self.assertIn((None, None, None), locales._locales) @@ -143,6 +151,7 @@ class TestGlobalLocaleProvider(TestCase): self.assertEqual(locale.id.territory, 'GB') self.assertEqual(locale.id.variant, None) + class TestRootLocale(TestCase): """There were some complaints that the root locale does not work correctly, so make sure it does.""" @@ -153,10 +162,14 @@ class TestRootLocale(TestCase): def test_dateFormatter(self): formatter = self.locale.dates.getFormatter('date') self.assertEqual( - formatter.format(datetime.date(2004, 10, 31), 'E'), '1') + formatter.format(datetime.date(2004, 10, 31), 'E'), '1' + ) self.assertEqual( - formatter.format(datetime.date(2004, 10, 31), 'EE'), '01') + formatter.format(datetime.date(2004, 10, 31), 'EE'), '01' + ) self.assertEqual( - formatter.format(datetime.date(2004, 10, 31), 'EEE'), '1') + formatter.format(datetime.date(2004, 10, 31), 'EEE'), '1' + ) self.assertEqual( - formatter.format(datetime.date(2004, 10, 31), 'EEEE'), '1') + formatter.format(datetime.date(2004, 10, 31), 'EEEE'), '1' + ) diff --git a/src/zope/i18n/locales/tests/test_xmlfactory.py b/src/zope/i18n/locales/tests/test_xmlfactory.py index 792c96d..b1c09bd 100644 --- a/src/zope/i18n/locales/tests/test_xmlfactory.py +++ b/src/zope/i18n/locales/tests/test_xmlfactory.py @@ -19,6 +19,7 @@ from unittest import TestCase, TestSuite from zope.i18n.locales.xmlfactory import LocaleFactory import zope.i18n + class LocaleXMLFileTestCase(TestCase): """This test verifies that every locale XML file can be loaded.""" @@ -34,13 +35,13 @@ class LocaleXMLFileTestCase(TestCase): # necessary for the xml files to have all format definitions. ## Making sure all number format patterns parse - #for category in (u'decimal', u'scientific', u'percent', u'currency'): + # for category in (u'decimal', u'scientific', u'percent', u'currency'): # for length in getattr(locale.numbers, category+'Formats').values(): # for format in length.formats.values(): # self.assert_(parseNumberPattern(format.pattern) is not None) ## Making sure all datetime patterns parse - #for calendar in locale.dates.calendars.values(): + # for calendar in locale.dates.calendars.values(): # for category in ('date', 'time', 'dateTime'): # for length in getattr(calendar, category+'Formats').values(): # for format in length.formats.values(): @@ -48,11 +49,11 @@ class LocaleXMLFileTestCase(TestCase): # parseDateTimePattern(format.pattern) is not None) - def test_suite(): suite = TestSuite() - locale_dir = os.path.join(os.path.dirname(zope.i18n.__file__), - 'locales', 'data') + locale_dir = os.path.join( + os.path.dirname(zope.i18n.__file__), 'locales', 'data' + ) for path in os.listdir(locale_dir): if not path.endswith(".xml"): continue diff --git a/src/zope/i18n/locales/xmlfactory.py b/src/zope/i18n/locales/xmlfactory.py index 5b2dbf5..244d17e 100644 --- a/src/zope/i18n/locales/xmlfactory.py +++ b/src/zope/i18n/locales/xmlfactory.py @@ -41,7 +41,6 @@ class LocaleFactory(object): rc = rc + node.data return rc - def _extractVersion(self, identity_node): """Extract the Locale's version info based on data from the DOM tree. @@ -81,7 +80,6 @@ class LocaleFactory(object): return LocaleVersion(number, generationDate, notes) - def _extractIdentity(self): """Extract the Locale's identity object based on info from the DOM tree. @@ -119,7 +117,7 @@ class LocaleFactory(object): # Retrieve the language of the locale nodes = identity.getElementsByTagName('language') if nodes != []: - id.language = nodes[0].getAttribute('type') or None + id.language = nodes[0].getAttribute('type') or None # Retrieve the territory of the locale nodes = identity.getElementsByTagName('territory') if nodes != []: @@ -132,7 +130,6 @@ class LocaleFactory(object): id.version = self._extractVersion(identity) return id - def _extractTypes(self, names_node): """Extract all types from the names_node. @@ -179,7 +176,6 @@ class LocaleFactory(object): types[(type, key)] = self._getText(type_node.childNodes) return types - def _extractDisplayNames(self): """Extract all display names from the DOM tree. @@ -265,11 +261,13 @@ class LocaleFactory(object): if names_nodes == []: return displayNames - for group_tag, single_tag in (('languages', 'language'), - ('scripts', 'script'), - ('territories', 'territory'), - ('variants', 'variant'), - ('keys', 'key')): + for group_tag, single_tag in ( + ('languages', 'language'), + ('scripts', 'script'), + ('territories', 'territory'), + ('variants', 'variant'), + ('keys', 'key'), + ): group_nodes = names_nodes[0].getElementsByTagName(group_tag) if group_nodes == []: continue @@ -285,7 +283,6 @@ class LocaleFactory(object): displayNames.types = types return displayNames - def _extractMonths(self, months_node, calendar): """Extract all month entries from cal_node and store them in calendar. @@ -385,14 +382,16 @@ class LocaleFactory(object): defaultMonthContext_node = months_node.getElementsByTagName('default') if defaultMonthContext_node: - calendar.defaultMonthContext = defaultMonthContext_node[0].getAttribute('type') + calendar.defaultMonthContext = defaultMonthContext_node[ + 0 + ].getAttribute('type') monthContext_nodes = months_node.getElementsByTagName('monthContext') if not monthContext_nodes: return calendar.monthContexts = InheritingDictionary() - names_node = abbrs_node = None # BBB + names_node = abbrs_node = None # BBB for node in monthContext_nodes: context_type = node.getAttribute('type') @@ -438,9 +437,10 @@ class LocaleFactory(object): # Put the info together calendar.months = InheritingDictionary() for type in range(1, 13): - calendar.months[type] = (names.get(type, None), - abbrs.get(type, None)) - + calendar.months[type] = ( + names.get(type, None), + abbrs.get(type, None), + ) def _extractDays(self, days_node, calendar): """Extract all day entries from cal_node and store them in @@ -529,14 +529,16 @@ class LocaleFactory(object): defaultDayContext_node = days_node.getElementsByTagName('default') if defaultDayContext_node: - calendar.defaultDayContext = defaultDayContext_node[0].getAttribute('type') + calendar.defaultDayContext = defaultDayContext_node[ + 0 + ].getAttribute('type') dayContext_nodes = days_node.getElementsByTagName('dayContext') if not dayContext_nodes: return calendar.dayContexts = InheritingDictionary() - names_node = abbrs_node = None # BBB + names_node = abbrs_node = None # BBB for node in dayContext_nodes: context_type = node.getAttribute('type') @@ -581,9 +583,10 @@ class LocaleFactory(object): # Put the info together calendar.days = InheritingDictionary() for type in range(1, 13): - calendar.days[type] = (names.get(type, None), - abbrs.get(type, None)) - + calendar.days[type] = ( + names.get(type, None), + abbrs.get(type, None), + ) def _extractWeek(self, cal_node, calendar): """Extract all week entries from cal_node and store them in @@ -644,7 +647,6 @@ class LocaleFactory(object): time_args = map(int, node.getAttribute('time').split(':')) calendar.week['weekendEnd'] = (day, time(*time_args)) - def _extractEras(self, cal_node, calendar): """Extract all era entries from cal_node and store them in calendar. @@ -703,8 +705,10 @@ class LocaleFactory(object): calendar.eras = InheritingDictionary() for type in abbrs.keys(): - calendar.eras[type] = (names.get(type, None), abbrs.get(type, None)) - + calendar.eras[type] = ( + names.get(type, None), + abbrs.get(type, None), + ) def _extractFormats(self, formats_node, lengthNodeName, formatNodeName): """Extract all format entries from formats_node and return a @@ -765,14 +769,18 @@ class LocaleFactory(object): if length_node.getElementsByTagName(formatNodeName): length.formats = InheritingDictionary() - for format_node in length_node.getElementsByTagName(formatNodeName): + for format_node in length_node.getElementsByTagName( + formatNodeName + ): format = LocaleFormat() format.type = format_node.getAttribute('type') or None pattern_node = format_node.getElementsByTagName('pattern')[0] format.pattern = self._getText(pattern_node.childNodes) name_nodes = format_node.getElementsByTagName('displayName') if name_nodes: - format.displayName = self._getText(name_nodes[0].childNodes) + format.displayName = self._getText( + name_nodes[0].childNodes + ) length.formats[format.type] = format lengths[length.type] = length @@ -905,17 +913,21 @@ class LocaleFactory(object): self._extractEras(cal_node, calendar) for formatsName, lengthName, formatName in ( - ('dateFormats', 'dateFormatLength', 'dateFormat'), - ('timeFormats', 'timeFormatLength', 'timeFormat'), - ('dateTimeFormats', 'dateTimeFormatLength', 'dateTimeFormat')): + ('dateFormats', 'dateFormatLength', 'dateFormat'), + ('timeFormats', 'timeFormatLength', 'timeFormat'), + ('dateTimeFormats', 'dateTimeFormatLength', 'dateTimeFormat'), + ): formats_nodes = cal_node.getElementsByTagName(formatsName) if formats_nodes: default, formats = self._extractFormats( - formats_nodes[0], lengthName, formatName) - setattr(calendar, - 'default'+formatName[0].upper()+formatName[1:], - default) + formats_nodes[0], lengthName, formatName + ) + setattr( + calendar, + 'default' + formatName[0].upper() + formatName[1:], + default, + ) setattr(calendar, formatsName, formats) calendars[calendar.type] = calendar @@ -925,7 +937,6 @@ class LocaleFactory(object): return calendars - def _extractTimeZones(self, dates_node): """Extract all timezone information for the locale from the DOM tree. @@ -1009,7 +1020,6 @@ class LocaleFactory(object): return zones - def _extractDates(self): """Extract all date information from the DOM tree""" dates_nodes = self._data.getElementsByTagName('dates') @@ -1025,7 +1035,6 @@ class LocaleFactory(object): dates.timezones = timezones return dates - def _extractSymbols(self, numbers_node): """Extract all week entries from cal_node and store them in calendar. @@ -1071,17 +1080,26 @@ class LocaleFactory(object): return symbols = InheritingDictionary() - for name in (u"decimal", u"group", u"list", u"percentSign", - u"nativeZeroDigit", u"patternDigit", u"plusSign", - u"minusSign", u"exponential", u"perMille", - u"infinity", u"nan"): + for name in ( + u"decimal", + u"group", + u"list", + u"percentSign", + u"nativeZeroDigit", + u"patternDigit", + u"plusSign", + u"minusSign", + u"exponential", + u"perMille", + u"infinity", + u"nan", + ): nodes = symbols_nodes[0].getElementsByTagName(name) if nodes: symbols[name] = self._getText(nodes[0].childNodes) return symbols - def _extractNumberFormats(self, numbers_node, numbers): """Extract all number formats from the numbers_node and save the data in numbers. @@ -1162,19 +1180,19 @@ class LocaleFactory(object): """ for category in ('decimal', 'scientific', 'percent', 'currency'): - formatsName = category+'Formats' - lengthName = category+'FormatLength' - formatName = category+'Format' - defaultName = 'default'+formatName[0].upper()+formatName[1:] + formatsName = category + 'Formats' + lengthName = category + 'FormatLength' + formatName = category + 'Format' + defaultName = 'default' + formatName[0].upper() + formatName[1:] formats_nodes = numbers_node.getElementsByTagName(formatsName) if formats_nodes: default, formats = self._extractFormats( - formats_nodes[0], lengthName, formatName) + formats_nodes[0], lengthName, formatName + ) setattr(numbers, defaultName, default) setattr(numbers, formatsName, formats) - def _extractCurrencies(self, numbers_node): """Extract all currency definitions and their information from the Locale's DOM tree. @@ -1231,8 +1249,9 @@ class LocaleFactory(object): nodes = curr_node.getElementsByTagName('symbol') if nodes: currency.symbol = self._getText(nodes[0].childNodes) - currency.symbolChoice = \ - nodes[0].getAttribute('choice') == u"true" + currency.symbolChoice = ( + nodes[0].getAttribute('choice') == u"true" + ) nodes = curr_node.getElementsByTagName('displayName') if nodes: @@ -1242,7 +1261,6 @@ class LocaleFactory(object): return currencies - def _extractNumbers(self): """Extract all number information from the DOM tree""" numbers_nodes = self._data.getElementsByTagName('numbers') @@ -1259,7 +1277,6 @@ class LocaleFactory(object): numbers.currencies = currencies return numbers - def _extractDelimiters(self): """Extract all delimiter entries from the DOM tree. @@ -1307,33 +1324,36 @@ class LocaleFactory(object): return delimiters = InheritingDictionary() - for name in (u'quotationStart', u"quotationEnd", - u"alternateQuotationStart", u"alternateQuotationEnd"): + for name in ( + u'quotationStart', + u"quotationEnd", + u"alternateQuotationStart", + u"alternateQuotationEnd", + ): nodes = delimiters_nodes[0].getElementsByTagName(name) if nodes: delimiters[name] = self._getText(nodes[0].childNodes) return delimiters - def _extractOrientation(self): """Extract orientation information. - >>> factory = LocaleFactory(None) - >>> from xml.dom.minidom import parseString - >>> xml = u''' - ... <ldml> - ... <layout> - ... <orientation lines="bottom-to-top" characters="right-to-left" /> - ... </layout> - ... </ldml>''' - >>> dom = parseString(xml) - >>> factory._data = parseString(xml).documentElement - >>> orientation = factory._extractOrientation() - >>> orientation.lines - u'bottom-to-top' - >>> orientation.characters - u'right-to-left' + >>> factory = LocaleFactory(None) + >>> from xml.dom.minidom import parseString + >>> xml = u''' + ... <ldml> + ... <layout> + ... <orientation lines="bottom-to-top" characters="right-to-left" /> + ... </layout> + ... </ldml>''' + >>> dom = parseString(xml) + >>> factory._data = parseString(xml).documentElement + >>> orientation = factory._extractOrientation() + >>> orientation.lines + u'bottom-to-top' + >>> orientation.characters + u'right-to-left' """ orientation_nodes = self._data.getElementsByTagName('orientation') if not orientation_nodes: @@ -1345,7 +1365,6 @@ class LocaleFactory(object): setattr(orientation, name, value) return orientation - def __call__(self): """Create the Locale.""" locale = Locale(self._extractIdentity()) |