localedata.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. """Low-level locale data access.
  14. :note: The `Locale` class, which uses this module under the hood, provides a
  15. more convenient interface for accessing the locale data.
  16. """
  17. import os
  18. import pickle
  19. try:
  20. import threading
  21. except ImportError:
  22. import dummy_threading as threading
  23. from UserDict import DictMixin
  24. __all__ = ['exists', 'list', 'load']
  25. __docformat__ = 'restructuredtext en'
  26. _cache = {}
  27. _cache_lock = threading.RLock()
  28. _dirname = os.path.join(os.path.dirname(__file__), 'localedata')
  29. def exists(name):
  30. """Check whether locale data is available for the given locale.
  31. :param name: the locale identifier string
  32. :return: `True` if the locale data exists, `False` otherwise
  33. :rtype: `bool`
  34. """
  35. if name in _cache:
  36. return True
  37. return os.path.exists(os.path.join(_dirname, '%s.dat' % name))
  38. def list():
  39. """Return a list of all locale identifiers for which locale data is
  40. available.
  41. :return: a list of locale identifiers (strings)
  42. :rtype: `list`
  43. :since: version 0.8.1
  44. """
  45. return [stem for stem, extension in [
  46. os.path.splitext(filename) for filename in os.listdir(_dirname)
  47. ] if extension == '.dat' and stem != 'root']
  48. def load(name, merge_inherited=True):
  49. """Load the locale data for the given locale.
  50. The locale data is a dictionary that contains much of the data defined by
  51. the Common Locale Data Repository (CLDR). This data is stored as a
  52. collection of pickle files inside the ``babel`` package.
  53. >>> d = load('en_US')
  54. >>> d['languages']['sv']
  55. u'Swedish'
  56. Note that the results are cached, and subsequent requests for the same
  57. locale return the same dictionary:
  58. >>> d1 = load('en_US')
  59. >>> d2 = load('en_US')
  60. >>> d1 is d2
  61. True
  62. :param name: the locale identifier string (or "root")
  63. :param merge_inherited: whether the inherited data should be merged into
  64. the data of the requested locale
  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' or not merge_inherited:
  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' and merge_inherited:
  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. def merge(dict1, dict2):
  98. """Merge the data from `dict2` into the `dict1` dictionary, making copies
  99. of nested dictionaries.
  100. >>> d = {1: 'foo', 3: 'baz'}
  101. >>> merge(d, {1: 'Foo', 2: 'Bar'})
  102. >>> items = d.items(); items.sort(); items
  103. [(1, 'Foo'), (2, 'Bar'), (3, 'baz')]
  104. :param dict1: the dictionary to merge into
  105. :param dict2: the dictionary containing the data that should be merged
  106. """
  107. for key, val2 in dict2.items():
  108. if val2 is not None:
  109. val1 = dict1.get(key)
  110. if isinstance(val2, dict):
  111. if val1 is None:
  112. val1 = {}
  113. if isinstance(val1, Alias):
  114. val1 = (val1, val2)
  115. elif isinstance(val1, tuple):
  116. alias, others = val1
  117. others = others.copy()
  118. merge(others, val2)
  119. val1 = (alias, others)
  120. else:
  121. val1 = val1.copy()
  122. merge(val1, val2)
  123. else:
  124. val1 = val2
  125. dict1[key] = val1
  126. class Alias(object):
  127. """Representation of an alias in the locale data.
  128. An alias is a value that refers to some other part of the locale data,
  129. as specified by the `keys`.
  130. """
  131. def __init__(self, keys):
  132. self.keys = tuple(keys)
  133. def __repr__(self):
  134. return '<%s %r>' % (type(self).__name__, self.keys)
  135. def resolve(self, data):
  136. """Resolve the alias based on the given data.
  137. This is done recursively, so if one alias resolves to a second alias,
  138. that second alias will also be resolved.
  139. :param data: the locale data
  140. :type data: `dict`
  141. """
  142. base = data
  143. for key in self.keys:
  144. data = data[key]
  145. if isinstance(data, Alias):
  146. data = data.resolve(base)
  147. elif isinstance(data, tuple):
  148. alias, others = data
  149. data = alias.resolve(base)
  150. return data
  151. class LocaleDataDict(DictMixin, dict):
  152. """Dictionary wrapper that automatically resolves aliases to the actual
  153. values.
  154. """
  155. def __init__(self, data, base=None):
  156. dict.__init__(self, data)
  157. if base is None:
  158. base = data
  159. self.base = base
  160. def __getitem__(self, key):
  161. orig = val = dict.__getitem__(self, key)
  162. if isinstance(val, Alias): # resolve an alias
  163. val = val.resolve(self.base)
  164. if isinstance(val, tuple): # Merge a partial dict with an alias
  165. alias, others = val
  166. val = alias.resolve(self.base).copy()
  167. merge(val, others)
  168. if type(val) is dict: # Return a nested alias-resolving dict
  169. val = LocaleDataDict(val, base=self.base)
  170. if val is not orig:
  171. self[key] = val
  172. return val
  173. def copy(self):
  174. return LocaleDataDict(dict.copy(self), base=self.base)