142
|
1 # -*- coding: utf-8 -*-
|
|
2 #
|
|
3 # Copyright (C) 2007 Edgewall Software
|
|
4 # All rights reserved.
|
|
5 #
|
|
6 # This software is licensed as described in the file COPYING, which
|
|
7 # you should have received as part of this distribution. The terms
|
|
8 # are also available at http://babel.edgewall.org/wiki/License.
|
|
9 #
|
|
10 # This software consists of voluntary contributions made by many
|
|
11 # individuals. For the exact contribution history, see the revision
|
|
12 # history and logs, available at http://babel.edgewall.org/log/.
|
|
13
|
|
14 """Low-level locale data access.
|
|
15
|
|
16 :note: The `Locale` class, which uses this module under the hood, provides a
|
|
17 more convenient interface for accessing the locale data.
|
|
18 """
|
|
19
|
|
20 import os
|
|
21 import pickle
|
|
22 try:
|
|
23 import threading
|
|
24 except ImportError:
|
|
25 import dummy_threading as threading
|
|
26
|
|
27 __all__ = ['exists', 'load']
|
|
28 __docformat__ = 'restructuredtext en'
|
|
29
|
|
30 _cache = {}
|
|
31 _cache_lock = threading.RLock()
|
|
32 _dirname = os.path.join(os.path.dirname(__file__), 'localedata')
|
|
33
|
|
34 def exists(name):
|
|
35 """Check whether locale data is available for the given locale.
|
|
36
|
|
37 :param name: the locale identifier string
|
|
38 :return: `True` if the locale data exists, `False` otherwise
|
|
39 :rtype: `bool`
|
|
40 """
|
|
41 if name in _cache:
|
|
42 return True
|
|
43 return os.path.exists(os.path.join(_dirname, '%s.dat' % name))
|
|
44
|
|
45 def load(name):
|
|
46 """Load the locale data for the given locale.
|
|
47
|
|
48 The locale data is a dictionary that contains much of the data defined by
|
|
49 the Common Locale Data Repository (CLDR). This data is stored as a
|
|
50 collection of pickle files inside the ``babel`` package.
|
|
51
|
|
52 >>> d = load('en_US')
|
|
53 >>> d['languages']['sv']
|
|
54 u'Swedish'
|
|
55
|
|
56 Note that the results are cached, and subsequent requests for the same
|
|
57 locale return the same dictionary:
|
|
58
|
|
59 >>> d1 = load('en_US')
|
|
60 >>> d2 = load('en_US')
|
|
61 >>> d1 is d2
|
|
62 True
|
|
63
|
|
64 :param name: the locale identifier string (or "root")
|
|
65 :return: the locale data
|
|
66 :rtype: `dict`
|
|
67 :raise `IOError`: if no locale data file is found for the given locale
|
|
68 identifer, or one of the locales it inherits from
|
|
69 """
|
|
70 _cache_lock.acquire()
|
|
71 try:
|
|
72 data = _cache.get(name)
|
|
73 if not data:
|
|
74 # Load inherited data
|
|
75 if name == 'root':
|
|
76 data = {}
|
|
77 else:
|
|
78 parts = name.split('_')
|
|
79 if len(parts) == 1:
|
|
80 parent = 'root'
|
|
81 else:
|
|
82 parent = '_'.join(parts[:-1])
|
|
83 data = load(parent).copy()
|
|
84 filename = os.path.join(_dirname, '%s.dat' % name)
|
|
85 fileobj = open(filename, 'rb')
|
|
86 try:
|
|
87 if name != 'root':
|
|
88 merge(data, pickle.load(fileobj))
|
|
89 else:
|
|
90 data = pickle.load(fileobj)
|
|
91 _cache[name] = data
|
|
92 finally:
|
|
93 fileobj.close()
|
|
94 return data
|
|
95 finally:
|
|
96 _cache_lock.release()
|
|
97
|
|
98 def merge(dict1, dict2):
|
|
99 """Merge the data from `dict2` into the `dict1` dictionary, making copies
|
|
100 of nested dictionaries.
|
|
101
|
|
102 :param dict1: the dictionary to merge into
|
|
103 :param dict2: the dictionary containing the data that should be merged
|
|
104 """
|
|
105 for key, value in dict2.items():
|
|
106 if value:
|
|
107 if type(value) is dict:
|
|
108 dict1[key] = dict1.get(key, {}).copy()
|
|
109 merge(dict1[key], value)
|
|
110 else:
|
|
111 dict1[key] = value
|