Skip to content
Permalink
Browse files
Use a mutable mapping to proxy deprecated settings.overrides and sett…
…ings.defaults attribute
  • Loading branch information
dangra committed Jul 8, 2014
1 parent 3cd6146 commit e5e81335e7e3d9d438f77539e7d218943d454b3f
Showing with 36 additions and 10 deletions.
  1. +27 −10 scrapy/settings/__init__.py
  2. +9 −0 scrapy/tests/test_settings/__init__.py
@@ -1,6 +1,7 @@
import six
import json
import warnings
from collections import MutableMapping
from importlib import import_module

from scrapy.utils.deprecate import create_deprecated_class
@@ -119,11 +120,7 @@ def overrides(self):
try:
o = self._overrides
except AttributeError:
class _DictProxy(dict):
def __setitem__(this, key, value):
super(_DictProxy, this).__setitem__(key, value)
self.set(key, value, priority='cmdline')
self._overrides = o = _DictProxy()
self._overrides = o = _DictProxy(self, 'cmdline')
return o

@property
@@ -135,14 +132,34 @@ def defaults(self):
try:
o = self._defaults
except AttributeError:
class _DictProxy(dict):
def __setitem__(this, key, value):
super(_DictProxy, this).__setitem__(key, value)
self.set(key, value, priority='default')
self._defaults = o = _DictProxy()
self._defaults = o = _DictProxy(self, 'default')
return o


class _DictProxy(MutableMapping):

def __init__(self, settings, priority):
self.o = {}
self.settings = settings
self.priority = priority

def __len__(self):
return len(self.o)

def __getitem__(self, k):
return self.o[k]

def __setitem__(self, k, v):
self.settings.set(k, v, priority=self.priority)
self.o[k] = v

def __delitem__(self, k):
del self.o[k]

def __iter__(self, k, v):
return iter(self.o)


class CrawlerSettings(Settings):

def __init__(self, settings_module=None, **kw):
@@ -199,6 +199,15 @@ def test_deprecated_attribute_overrides(self):
self.assertEqual(self.settings.overrides.get('BAR'), 'foo')
self.assertIn('BAR', self.settings.overrides)

self.settings.overrides.update(BAR='bus')
self.assertEqual(self.settings.get('BAR'), 'bus')
self.assertEqual(self.settings.overrides.get('BAR'), 'bus')

self.settings.overrides.setdefault('FOO', 'fez')
self.assertEqual(self.settings.get('FOO'), 'fez')
self.assertEqual(self.settings.overrides.get('FOO'), 'fez')


def test_deprecated_attribute_defaults(self):
self.settings.set('BAR', 'fuz', priority='default')
with warnings.catch_warnings(record=True) as w:

0 comments on commit e5e8133

Please sign in to comment.