Mercurial > babel > mirror
annotate babel/localedata.py @ 567:c81a11cb1476 trunk
add a compat module to shield the code from changes in different versions of Python
author | fschwarz |
---|---|
date | Mon, 26 Sep 2011 09:42:43 +0000 |
parents | 1de26da5aa25 |
children |
rev | line source |
---|---|
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
2 # |
530 | 3 # Copyright (C) 2007-2011 Edgewall Software |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
5 # |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
8 # are also available at http://babel.edgewall.org/wiki/License. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
9 # |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://babel.edgewall.org/log/. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
13 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
14 """Low-level locale data access. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
15 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
16 :note: The `Locale` class, which uses this module under the hood, provides a |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
17 more convenient interface for accessing the locale data. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
18 """ |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
19 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
20 import os |
549
1de26da5aa25
use cPickle instead of pickle for better performance (fixes #225)
fschwarz
parents:
546
diff
changeset
|
21 import cPickle as pickle |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
22 from UserDict import DictMixin |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
23 |
567
c81a11cb1476
add a compat module to shield the code from changes in different versions of Python
fschwarz
parents:
549
diff
changeset
|
24 from babel.compat import threading |
c81a11cb1476
add a compat module to shield the code from changes in different versions of Python
fschwarz
parents:
549
diff
changeset
|
25 |
546
fd3bda2d3d34
rename babel.localedata.list() to ease Python 3 transition (fixes #250)
fschwarz
parents:
530
diff
changeset
|
26 __all__ = ['exists', 'locale_identifiers', 'load'] |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
27 __docformat__ = 'restructuredtext en' |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
28 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
29 _cache = {} |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
30 _cache_lock = threading.RLock() |
41
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
31 _dirname = os.path.join(os.path.dirname(__file__), 'localedata') |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
32 |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
33 |
41
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
34 def exists(name): |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
35 """Check whether locale data is available for the given locale. |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
36 |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
37 :param name: the locale identifier string |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
38 :return: `True` if the locale data exists, `False` otherwise |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
39 :rtype: `bool` |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
40 """ |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
41 if name in _cache: |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
42 return True |
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
43 return os.path.exists(os.path.join(_dirname, '%s.dat' % name)) |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
44 |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
45 |
546
fd3bda2d3d34
rename babel.localedata.list() to ease Python 3 transition (fixes #250)
fschwarz
parents:
530
diff
changeset
|
46 def locale_identifiers(): |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
47 """Return a list of all locale identifiers for which locale data is |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
48 available. |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
49 |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
50 :return: a list of locale identifiers (strings) |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
51 :rtype: `list` |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
52 :since: version 0.8.1 |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
53 """ |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
54 return [stem for stem, extension in [ |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
55 os.path.splitext(filename) for filename in os.listdir(_dirname) |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
56 ] if extension == '.dat' and stem != 'root'] |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
99
diff
changeset
|
57 |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
58 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
59 def load(name, merge_inherited=True): |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
60 """Load the locale data for the given locale. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
61 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
62 The locale data is a dictionary that contains much of the data defined by |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
63 the Common Locale Data Repository (CLDR). This data is stored as a |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
64 collection of pickle files inside the ``babel`` package. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
65 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
66 >>> d = load('en_US') |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
67 >>> d['languages']['sv'] |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
68 u'Swedish' |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
69 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
70 Note that the results are cached, and subsequent requests for the same |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
71 locale return the same dictionary: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
72 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
73 >>> d1 = load('en_US') |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
74 >>> d2 = load('en_US') |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
75 >>> d1 is d2 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
76 True |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
77 |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
78 :param name: the locale identifier string (or "root") |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
79 :param merge_inherited: whether the inherited data should be merged into |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
80 the data of the requested locale |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
81 :return: the locale data |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
82 :rtype: `dict` |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
83 :raise `IOError`: if no locale data file is found for the given locale |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
84 identifer, or one of the locales it inherits from |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
85 """ |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
86 _cache_lock.acquire() |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
87 try: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
88 data = _cache.get(name) |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
89 if not data: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
90 # Load inherited data |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
91 if name == 'root' or not merge_inherited: |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
92 data = {} |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
93 else: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
94 parts = name.split('_') |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
95 if len(parts) == 1: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
96 parent = 'root' |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
97 else: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
98 parent = '_'.join(parts[:-1]) |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
99 data = load(parent).copy() |
41
e46b7cd193ee
Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
cmlenz
parents:
26
diff
changeset
|
100 filename = os.path.join(_dirname, '%s.dat' % name) |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
101 fileobj = open(filename, 'rb') |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
102 try: |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
103 if name != 'root' and merge_inherited: |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
104 merge(data, pickle.load(fileobj)) |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
105 else: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
106 data = pickle.load(fileobj) |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
107 _cache[name] = data |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
108 finally: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
109 fileobj.close() |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
110 return data |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
111 finally: |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
112 _cache_lock.release() |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
113 |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
114 |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
115 def merge(dict1, dict2): |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
116 """Merge the data from `dict2` into the `dict1` dictionary, making copies |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
117 of nested dictionaries. |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
118 |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
119 >>> d = {1: 'foo', 3: 'baz'} |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
120 >>> merge(d, {1: 'Foo', 2: 'Bar'}) |
440
2e8fa2145a13
remove sorted and don't assume dict ordering (Python 2.3 & Jython compat)
pjenvey
parents:
399
diff
changeset
|
121 >>> items = d.items(); items.sort(); items |
2e8fa2145a13
remove sorted and don't assume dict ordering (Python 2.3 & Jython compat)
pjenvey
parents:
399
diff
changeset
|
122 [(1, 'Foo'), (2, 'Bar'), (3, 'baz')] |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
123 |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
124 :param dict1: the dictionary to merge into |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
125 :param dict2: the dictionary containing the data that should be merged |
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
126 """ |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
127 for key, val2 in dict2.items(): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
128 if val2 is not None: |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
129 val1 = dict1.get(key) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
130 if isinstance(val2, dict): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
131 if val1 is None: |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
132 val1 = {} |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
133 if isinstance(val1, Alias): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
134 val1 = (val1, val2) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
135 elif isinstance(val1, tuple): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
136 alias, others = val1 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
137 others = others.copy() |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
138 merge(others, val2) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
139 val1 = (alias, others) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
140 else: |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
141 val1 = val1.copy() |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
142 merge(val1, val2) |
26
6041782ea677
* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime.
cmlenz
parents:
diff
changeset
|
143 else: |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
144 val1 = val2 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
145 dict1[key] = val1 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
146 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
147 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
148 class Alias(object): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
149 """Representation of an alias in the locale data. |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
150 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
151 An alias is a value that refers to some other part of the locale data, |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
152 as specified by the `keys`. |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
153 """ |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
154 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
155 def __init__(self, keys): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
156 self.keys = tuple(keys) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
157 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
158 def __repr__(self): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
159 return '<%s %r>' % (type(self).__name__, self.keys) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
160 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
161 def resolve(self, data): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
162 """Resolve the alias based on the given data. |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
163 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
164 This is done recursively, so if one alias resolves to a second alias, |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
165 that second alias will also be resolved. |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
166 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
167 :param data: the locale data |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
168 :type data: `dict` |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
169 """ |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
170 base = data |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
171 for key in self.keys: |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
172 data = data[key] |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
173 if isinstance(data, Alias): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
174 data = data.resolve(base) |
399
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
175 elif isinstance(data, tuple): |
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
176 alias, others = data |
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
177 data = alias.resolve(base) |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
178 return data |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
179 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
180 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
181 class LocaleDataDict(DictMixin, dict): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
182 """Dictionary wrapper that automatically resolves aliases to the actual |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
183 values. |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
184 """ |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
185 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
186 def __init__(self, data, base=None): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
187 dict.__init__(self, data) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
188 if base is None: |
399
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
189 base = data |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
190 self.base = base |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
191 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
192 def __getitem__(self, key): |
399
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
193 orig = val = dict.__getitem__(self, key) |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
194 if isinstance(val, Alias): # resolve an alias |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
195 val = val.resolve(self.base) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
196 if isinstance(val, tuple): # Merge a partial dict with an alias |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
197 alias, others = val |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
198 val = alias.resolve(self.base).copy() |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
199 merge(val, others) |
399
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
200 if type(val) is dict: # Return a nested alias-resolving dict |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
201 val = LocaleDataDict(val, base=self.base) |
399
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
202 if val is not orig: |
953a531232ca
Fix for memory leak reported in #128. Thanks to Manlio Perillo for reporting the problem.
cmlenz
parents:
375
diff
changeset
|
203 self[key] = val |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
204 return val |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
205 |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
206 def copy(self): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
233
diff
changeset
|
207 return LocaleDataDict(dict.copy(self), base=self.base) |