]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Pull in some supplemental data from the CLDR, for things like the first day of the...
authorChristopher Lenz <cmlenz@gmail.com>
Wed, 30 May 2007 17:39:43 +0000 (17:39 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Wed, 30 May 2007 17:39:43 +0000 (17:39 +0000)
babel/core.py
babel/dates.py
scripts/dump_data.py [new file with mode: 0755]
scripts/import_cldr.py

index 607ea70eac2d0771abd6e1ba0d23788e937d0f31..5b9913add04541acb564fde154267635ef4f2fae 100644 (file)
@@ -157,6 +157,8 @@ class Locale(object):
         :type: `unicode`
         """)
 
+    #{ General Locale Display Names
+
     def languages(self):
         return self._data['languages']
     languages = property(languages, doc="""\
@@ -204,6 +206,8 @@ class Locale(object):
         :type: `dict`
         """)
 
+    #{ Number Formatting
+
     def number_symbols(self):
         return self._data['number_symbols']
     number_symbols = property(number_symbols, doc="""\
@@ -215,6 +219,8 @@ class Locale(object):
         :type: `dict`
         """)
 
+    #{ Calendar Information and Date Formatting
+
     def periods(self):
         return self._data['periods']
     periods = property(periods, doc="""\
@@ -272,6 +278,53 @@ class Locale(object):
         :type: `dict`
         """)
 
+    def first_week_day(self):
+        return self._data['week_data']['first_day']
+    first_week_day = property(first_week_day, doc="""\
+        The first day of a week.
+        
+        >>> Locale('de', 'DE').first_week_day
+        1
+        >>> Locale('en', 'US').first_week_day
+        7
+        
+        :type: `int`
+        """)
+
+    def weekend_start(self):
+        return self._data['week_data']['weekend_start']
+    weekend_start = property(weekend_start, doc="""\
+        The day the weekend starts.
+        
+        >>> Locale('de', 'DE').weekend_start
+        6
+        
+        :type: `int`
+        """)
+
+    def weekend_end(self):
+        return self._data['week_data']['weekend_end']
+    weekend_end = property(weekend_end, doc="""\
+        The day the weekend ends.
+        
+        >>> Locale('de', 'DE').weekend_end
+        7
+        
+        :type: `int`
+        """)
+
+    def min_week_days(self):
+        return self._data['week_data']['min_days']
+    min_week_days = property(min_week_days, doc="""\
+        The minimum number of days in a week so that the week is counted as the
+        first week of a year or month.
+        
+        >>> Locale('de', 'DE').min_week_days
+        4
+        
+        :type: `int`
+        """)
+
     def date_formats(self):
         return self._data['date_formats']
     date_formats = property(date_formats, doc="""\
index 4c6406df1924ba609eb9f7a4fab9f8a995c10340..1c8b4fa6ddce6632da5defa0e52ed97969d44c85 100644 (file)
@@ -251,6 +251,8 @@ class DateTimeFormat(object):
             return self.format(self.value.day, len(name))
         elif name[0] == 'E':
             return self.format_weekday(len(name))
+        elif name[0] == 'e':
+            return self.format_weekday(len(name), add_firstday=True)
         elif name[0] == 'c':
             return self.format_weekday(len(name), context='stand-alone')
         elif name[0] == 'a':
@@ -283,9 +285,11 @@ class DateTimeFormat(object):
         width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num]
         return get_month_names(width, context, self.locale)[self.value.month]
 
-    def format_weekday(self, num, context='format'):
+    def format_weekday(self, num, add_firstday=False, context='format'):
         width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[max(3, num)]
         weekday = self.value.weekday() + 1
+        if add_firstday:
+            weekday += self.locale.first_week_day
         return get_day_names(width, context, self.locale)[weekday]
 
     def format_period(self):
diff --git a/scripts/dump_data.py b/scripts/dump_data.py
new file mode 100755 (executable)
index 0000000..453f639
--- /dev/null
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2007 Edgewall Software
+# All rights reserved.
+#
+# This software is licensed as described in the file COPYING, which
+# you should have received as part of this distribution. The terms
+# are also available at http://babel.edgewall.org/wiki/License.
+#
+# This software consists of voluntary contributions made by many
+# individuals. For the exact contribution history, see the revision
+# history and logs, available at http://babel.edgewall.org/log/.
+
+from pprint import pprint
+import sys
+from babel import Locale
+
+locale = Locale(sys.argv[1])
+pprint(locale._data)
index a13de627aacc54ba4e5176bd302cb874d7624437..5bb852df3ff76f02e197ca27ca735e6f96c2e65a 100755 (executable)
@@ -24,6 +24,15 @@ except ImportError:
 
 from babel.dates import parse_pattern
 
+weekdays = {'mon': 1, 'tue': 2, 'wed': 3, 'thu': 4, 'fri': 5, 'sat': 6,
+            'sun': 7}
+
+try:
+    any
+except NameError:
+    def any(iterable):
+        return filter(None, list(iterable))
+
 def _parent(locale):
     parts = locale.split('_')
     if len(parts) == 1:
@@ -48,6 +57,25 @@ def main():
     destdir = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])),
                            '..', 'babel', 'localedata')
 
+    sup = parse(os.path.join(srcdir, 'supplemental', 'supplementalData.xml'))
+
+    # build a territory containment mapping for inheritance
+    regions = {}
+    for elem in sup.findall('//territoryContainment/group'):
+        regions[elem.attrib['type']] = elem.attrib['contains'].split()
+    from pprint import pprint
+
+    # Resolve territory containment
+    territory_containment = {}
+    region_items = regions.items()
+    region_items.sort()
+    for group, territory_list in region_items:
+        for territory in territory_list:
+            containers = territory_containment.setdefault(territory, set([]))
+            if group in territory_containment:
+                containers |= territory_containment[group]
+            containers.add(group)
+
     filenames = os.listdir(os.path.join(srcdir, 'main'))
     filenames.remove('root.xml')
     filenames.sort(lambda a,b: len(a)-len(b))
@@ -66,6 +94,20 @@ def main():
             data.update(copy.deepcopy(dicts[_parent(stem)]))
         tree = parse(os.path.join(srcdir, 'main', filename))
 
+        language = None
+        elem = tree.find('//identity/language')
+        if elem is not None:
+            language = elem.attrib['type']
+        print>>sys.stderr, '  Language:  %r' % language
+
+        territory = None
+        elem = tree.find('//identity/territory')
+        if elem is not None:
+            territory = elem.attrib['type']
+        print>>sys.stderr, '  Territory: %r' % territory
+        regions = territory_containment.get(territory, [])
+        print>>sys.stderr, '  Regions:    %r' % regions
+
         # <localeDisplayNames>
 
         territories = data.setdefault('territories', {})
@@ -94,6 +136,29 @@ def main():
 
         # <dates>
 
+        week_data = data.setdefault('week_data', {})
+        supelem = sup.find('//weekData')
+
+        for elem in supelem.findall('minDays'):
+            territories = elem.attrib['territories'].split()
+            if territory in territories or any([r in territories for r in regions]):
+                week_data['min_days'] = int(elem.attrib['count'])
+
+        for elem in supelem.findall('firstDay'):
+            territories = elem.attrib['territories'].split()
+            if territory in territories or any([r in territories for r in regions]):
+                week_data['first_day'] = weekdays[elem.attrib['day']]
+
+        for elem in supelem.findall('weekendStart'):
+            territories = elem.attrib['territories'].split()
+            if territory in territories or any([r in territories for r in regions]):
+                week_data['weekend_start'] = weekdays[elem.attrib['day']]
+
+        for elem in supelem.findall('weekendEnd'):
+            territories = elem.attrib['territories'].split()
+            if territory in territories or any([r in territories for r in regions]):
+                week_data['weekend_end'] = weekdays[elem.attrib['day']]
+
         time_zones = data.setdefault('time_zones', {})
         for elem in tree.findall('//timeZoneNames/zone'):
             time_zones[elem.tag] = unicode(elem.findtext('displayName'))
@@ -119,8 +184,7 @@ def main():
                 for width in ctxt.findall('dayWidth'):
                     widths = ctxts.setdefault(width.attrib['type'], {})
                     for elem in width.findall('day'):
-                        dtype = {'mon': 1, 'tue': 2, 'wed': 3, 'thu': 4,
-                                 'fri': 5, 'sat': 6, 'sun': 7}[elem.attrib['type']]
+                        dtype = weekdays[elem.attrib['type']]
                         if 'draft' in elem.attrib and dtype in widths:
                             continue
                         widths[dtype] = unicode(elem.text)