comparison babel/localedata.py @ 41:e46b7cd193ee trunk

Load locale data lazily to avoid penalizing usage of `Locale` objects when no locale data is actually needed.
author cmlenz
date Wed, 06 Jun 2007 12:12:44 +0000
parents 6041782ea677
children c3bf81204393
comparison
equal deleted inserted replaced
40:0739bc8e7210 41:e46b7cd193ee
28 __all__ = ['load'] 28 __all__ = ['load']
29 __docformat__ = 'restructuredtext en' 29 __docformat__ = 'restructuredtext en'
30 30
31 _cache = {} 31 _cache = {}
32 _cache_lock = threading.RLock() 32 _cache_lock = threading.RLock()
33 _dirname = os.path.join(os.path.dirname(__file__), 'localedata')
34
35 def exists(name):
36 """Check whether locale data is available for the given locale.
37
38 :param name: the locale identifier string
39 :return: `True` if the locale data exists, `False` otherwise
40 :rtype: `bool`
41 """
42 if name in _cache:
43 return True
44 return os.path.exists(os.path.join(_dirname, '%s.dat' % name))
33 45
34 def load(name): 46 def load(name):
35 """Load the locale data for the given locale. 47 """Load the locale data for the given locale.
36 48
37 The locale data is a dictionary that contains much of the data defined by 49 The locale data is a dictionary that contains much of the data defined by
68 if len(parts) == 1: 80 if len(parts) == 1:
69 parent = 'root' 81 parent = 'root'
70 else: 82 else:
71 parent = '_'.join(parts[:-1]) 83 parent = '_'.join(parts[:-1])
72 data = load(parent).copy() 84 data = load(parent).copy()
73 filename = os.path.join(os.path.dirname(__file__), 85 filename = os.path.join(_dirname, '%s.dat' % name)
74 'localedata/%s.dat' % name)
75 fileobj = open(filename, 'rb') 86 fileobj = open(filename, 'rb')
76 try: 87 try:
77 if name != 'root': 88 if name != 'root':
78 merge(data, pickle.load(fileobj)) 89 merge(data, pickle.load(fileobj))
79 else: 90 else:
Copyright (C) 2012-2017 Edgewall Software