def fuzzy_load(name, merge_inherited=True): localedata._cache_lock.acquire() try: data = localedata._cache.get(name) if not data: # Load inherited data if name == 'root' or not merge_inherited: data = {} else: parts = name.split('_') if len(parts) == 1: parent = 'root' else: parent = '_'.join(parts[:-1]) data = fuzzy_load(parent).copy() filename = os.path.join(localedata._dirname, '%s.dat' % name) try: fileobj = open(filename, 'rb') try: if name != 'root' and merge_inherited: localedata.merge(data, pickle.load(fileobj)) else: data = pickle.load(fileobj) localedata._cache[name] = data finally: fileobj.close() except IOError: pass return data finally: localedata._cache_lock.release()
def babel_patched_load(name, merge_inherited=True): # Load additional languages into the babel # cache. We do this so we can add Haitian Creole # using french as a model. _cache_lock.acquire() try: data = _cache.get(name) if not data: # Load inherited data if name == 'root' or not merge_inherited: data = {} else: parts = name.split('_') if len(parts) == 1: parent = 'root' else: parent = '_'.join(parts[:-1]) data = load(parent).copy() filename = os.path.join(_mydirname, '%s.dat' % name) fileobj = open(filename, 'rb') try: if name != 'root' and merge_inherited: merge(data, pickle.load(fileobj)) else: data = pickle.load(fileobj) _cache[name] = data finally: fileobj.close() return data finally: _cache_lock.release()
def test_merge_nested_dict(self): d1 = {'x': {'a': 1, 'b': 2, 'c': 3}} d2 = {'x': {'a': 1, 'b': 12, 'd': 14}} localedata.merge(d1, d2) self.assertEqual({ 'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14} }, d1)
def test_merge_with_alias_and_resolve(self): alias = localedata.Alias('x') d1 = {'x': {'a': 1, 'b': 2, 'c': 3}, 'y': alias} d2 = {'x': {'a': 1, 'b': 12, 'd': 14}, 'y': {'b': 22, 'e': 25}} localedata.merge(d1, d2) self.assertEqual( { 'x': { 'a': 1, 'b': 12, 'c': 3, 'd': 14 }, 'y': (alias, { 'b': 22, 'e': 25 }) }, d1) d = localedata.LocaleDataDict(d1) self.assertEqual( { 'x': { 'a': 1, 'b': 12, 'c': 3, 'd': 14 }, 'y': { 'a': 1, 'b': 22, 'c': 3, 'd': 14, 'e': 25 } }, dict(d.items()))
def test_merge_nested_dict_no_overlap(self): d1 = {'x': {'a': 1, 'b': 2}} d2 = {'y': {'a': 11, 'b': 12}} localedata.merge(d1, d2) self.assertEqual({ 'x': {'a': 1, 'b': 2}, 'y': {'a': 11, 'b': 12} }, d1)
def test_merge_with_alias_and_resolve(self): alias = localedata.Alias('x') d1 = { 'x': {'a': 1, 'b': 2, 'c': 3}, 'y': alias } d2 = { 'x': {'a': 1, 'b': 12, 'd': 14}, 'y': {'b': 22, 'e': 25} } localedata.merge(d1, d2) self.assertEqual({ 'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14}, 'y': (alias, {'b': 22, 'e': 25}) }, d1) d = localedata.LocaleDataDict(d1) self.assertEqual({ 'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14}, 'y': {'a': 1, 'b': 22, 'c': 3, 'd': 14, 'e': 25} }, dict(d.items()))
def test_merge(): d = {1: 'foo', 3: 'baz'} localedata.merge(d, {1: 'Foo', 2: 'Bar'}) assert d == {1: 'Foo', 2: 'Bar', 3: 'baz'}
def test_merge_items(self): d = {1: 'foo', 3: 'baz'} localedata.merge(d, {1: 'Foo', 2: 'Bar'}) self.assertEqual({1: 'Foo', 2: 'Bar', 3: 'baz'}, d)