Bug 1041820 - Replace DefaultOnReadDict with defaultdict. r=gps

This commit is contained in:
Mike Hommey 2014-07-23 13:31:43 +09:00
Родитель 1c8e21d2d8
Коммит 1f566b2657
7 изменённых файлов: 37 добавлений и 110 удалений

Просмотреть файл

@ -26,7 +26,7 @@ from ..frontend.data import (
WebIDLFile,
)
from ..util import DefaultOnReadDict
from collections import defaultdict
class XPIDLManager(object):
@ -142,7 +142,7 @@ class TestManager(object):
self.config = config
self.topsrcdir = mozpath.normpath(config.topsrcdir)
self.tests_by_path = DefaultOnReadDict({}, global_default=[])
self.tests_by_path = defaultdict(list)
def add(self, t, flavor=None, topsrcdir=None):
t = dict(t)

Просмотреть файл

@ -184,8 +184,8 @@ class MozbuildSandbox(Sandbox):
d['SRCDIR'] = mozpath.join(topsrcdir, reldir).rstrip('/')
d['OBJDIR'] = mozpath.join(topobjdir, reldir).rstrip('/')
d['CONFIG'] = ReadOnlyDefaultDict(self.config.substs_unicode,
global_default=None)
d['CONFIG'] = ReadOnlyDefaultDict(lambda: None,
self.config.substs_unicode)
var = metadata.get('var', None)
if var and var in ['TOOL_DIRS', 'TEST_TOOL_DIRS']:

Просмотреть файл

@ -28,10 +28,7 @@ import sys
from contextlib import contextmanager
from mozbuild.util import (
ReadOnlyDefaultDict,
ReadOnlyDict,
)
from mozbuild.util import ReadOnlyDict
class SandboxDerivedValue(object):

Просмотреть файл

@ -7,6 +7,7 @@ from __future__ import unicode_literals
import os
import unittest
from collections import defaultdict
from shutil import rmtree
from tempfile import mkdtemp
@ -15,7 +16,6 @@ from mach.logging import LoggingManager
from mozbuild.backend.configenvironment import ConfigEnvironment
from mozbuild.frontend.emitter import TreeMetadataEmitter
from mozbuild.frontend.reader import BuildReader
from mozbuild.util import DefaultOnReadDict
import mozpack.path as mozpath
@ -28,7 +28,11 @@ test_data_path = mozpath.abspath(mozpath.dirname(__file__))
test_data_path = mozpath.join(test_data_path, 'data')
CONFIGS = DefaultOnReadDict({
CONFIGS = defaultdict(lambda: {
'defines': [],
'non_global_defines': [],
'substs': [],
}, {
'android_eclipse': {
'defines': [],
'non_global_defines': [],
@ -75,10 +79,6 @@ CONFIGS = DefaultOnReadDict({
('MOZ_APP_NAME', 'my_app'),
],
},
}, global_default={
'defines': [],
'non_global_defines': [],
'substs': [],
})

Просмотреть файл

@ -7,7 +7,6 @@ import unittest
from mozunit import main
from mozbuild.util import (
DefaultOnReadDict,
ReadOnlyDefaultDict,
ReadOnlyDict,
)
@ -27,78 +26,30 @@ class TestReadOnlyDict(unittest.TestCase):
with self.assertRaises(Exception):
test['baz'] = True
class TestDefaultOnReadDict(unittest.TestCase):
def test_no_defaults(self):
original = {'foo': 1, 'bar': 2}
test = DefaultOnReadDict(original)
self.assertEqual(original, test)
with self.assertRaises(KeyError):
value = test['missing']
test['foo'] = 5
self.assertEqual(test['foo'], 5)
def test_dict_defaults(self):
original = {'foo': 1, 'bar': 2}
test = DefaultOnReadDict(original, defaults={'baz': 3})
self.assertEqual(original, test)
self.assertEqual(test['baz'], 3)
with self.assertRaises(KeyError):
value = test['missing']
test['baz'] = 4
self.assertEqual(test['baz'], 4)
def test_global_default(self):
original = {'foo': 1}
test = DefaultOnReadDict(original, defaults={'bar': 2},
global_default=10)
self.assertEqual(original, test)
self.assertEqual(test['foo'], 1)
self.assertEqual(test['bar'], 2)
self.assertEqual(test['baz'], 10)
test['bar'] = 3
test['baz'] = 12
test['other'] = 11
self.assertEqual(test['bar'], 3)
self.assertEqual(test['baz'], 12)
self.assertEqual(test['other'], 11)
class TestReadOnlyDefaultDict(unittest.TestCase):
def test_simple(self):
original = {'foo': 1, 'bar': 2}
test = ReadOnlyDefaultDict(original)
test = ReadOnlyDefaultDict(bool, original)
self.assertEqual(original, test)
self.assertEqual(test['foo'], 1)
with self.assertRaises(KeyError):
value = test['missing']
def test_assignment(self):
test = ReadOnlyDefaultDict({})
test = ReadOnlyDefaultDict(bool, {})
with self.assertRaises(Exception):
test['foo'] = True
def test_defaults(self):
test = ReadOnlyDefaultDict({}, defaults={'foo': 1})
test = ReadOnlyDefaultDict(bool, {'foo': 1})
self.assertEqual(test['foo'], 1)
self.assertEqual(test['qux'], False)
if __name__ == '__main__':
main()

Просмотреть файл

@ -10,7 +10,7 @@ import os
import mozpack.path as mozpath
from .base import MozbuildObject
from .util import DefaultOnReadDict
from collections import defaultdict
def rewrite_test_base(test, new_base, honor_install_to_subdir=False):
@ -44,8 +44,8 @@ class TestMetadata(object):
"""
def __init__(self, filename=None):
self._tests_by_path = DefaultOnReadDict({}, global_default=[])
self._tests_by_flavor = DefaultOnReadDict({}, global_default=set())
self._tests_by_path = defaultdict(list)
self._tests_by_flavor = defaultdict(set)
self._test_dirs = set()
if filename:

Просмотреть файл

@ -16,7 +16,10 @@ import stat
import sys
import time
from collections import OrderedDict
from collections import (
defaultdict,
OrderedDict,
)
from StringIO import StringIO
@ -46,8 +49,8 @@ def hash_file(path):
class ReadOnlyDict(dict):
"""A read-only dictionary."""
def __init__(self, d):
dict.__init__(self, d)
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
def __setitem__(self, name, value):
raise Exception('Object does not support assignment.')
@ -60,43 +63,19 @@ class undefined_default(object):
undefined = undefined_default()
class DefaultOnReadDict(dict):
"""A dictionary that returns default values for missing keys on read."""
def __init__(self, d, defaults=None, global_default=undefined):
"""Create an instance from an iterable with defaults.
The first argument is fed into the dict constructor.
defaults is a dict mapping keys to their default values.
global_default is the default value for *all* missing keys. If it isn't
specified, no default value for keys not in defaults will be used and
IndexError will be raised on access.
"""
dict.__init__(self, d)
self._defaults = defaults or {}
self._global_default = global_default
def __getitem__(self, k):
try:
return dict.__getitem__(self, k)
except:
pass
if k in self._defaults:
dict.__setitem__(self, k, copy.deepcopy(self._defaults[k]))
elif self._global_default != undefined:
dict.__setitem__(self, k, copy.deepcopy(self._global_default))
return dict.__getitem__(self, k)
class ReadOnlyDefaultDict(DefaultOnReadDict, ReadOnlyDict):
class ReadOnlyDefaultDict(ReadOnlyDict):
"""A read-only dictionary that supports default values on retrieval."""
def __init__(self, d, defaults=None, global_default=undefined):
DefaultOnReadDict.__init__(self, d, defaults, global_default)
def __init__(self, default_factory, *args, **kwargs):
ReadOnlyDict.__init__(self, *args, **kwargs)
self._default_factory = default_factory
def __getitem__(self, key):
try:
return ReadOnlyDict.__getitem__(self, key)
except KeyError:
value = self._default_factory()
dict.__setitem__(self, key, value)
return value
def ensureParentDir(path):