Skip to content

Commit

Permalink
Config and ConfigSection __call__ are now decorator factories or deco…
Browse files Browse the repository at this point in the history
…rators
  • Loading branch information
mjdorma committed Aug 11, 2013
1 parent 11856ea commit 174587d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
47 changes: 34 additions & 13 deletions funconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def dirty(self):
self._dirty, d = False, self._dirty
return d

def __call__(self, func):
def __call__(self, func=None, **kwargs):
"""The :py:class:`ConfigSection` object can be used as a function
decorator.
Expand All @@ -421,12 +421,23 @@ def __call__(self, func):
def func(**k):
pass
:param func: function to be wrapped.
:type func: variable kwargs function
:rtype: wrapped function with defined kwargs defaults bound to this
:py:class:`ConfigSection` object.
:param func: Decorator parameter. The function or method to be wrapped.
:type func: function or method
:param lazy: Factory parameter. Turns lazy_string_cast on or off.
:type lazy: Boolean value default True
:rtype: As a factory returns decorator function. As a decorator
function returns a decorated function.
"""
return lazy_string_cast(self)(wraps_kwargs(self)(func))
lazy = kwargs.get('lazy', True)
def _wraps(func):
if lazy:
return lazy_string_cast(self)(wraps_kwargs(self)(func))
else:
return wraps_kwargs(self)(func)
if inspect.isfunction(func) or inspect.ismethod(func):
return _wraps(func)
else:
return _wraps


ConfigSection._reserved = set(dir(ConfigSection))
Expand Down Expand Up @@ -596,7 +607,7 @@ def __getitem__(self, y):
section = self._sections[s]
return section[option]

def __call__(self, func):
def __call__(self, func=None, **kwargs):
"""The :py:class:`Config` object can be used as a function decorator.
Applying this decorator to a function which takes variable kwargs will
Expand All @@ -614,13 +625,23 @@ def __call__(self, func):
def func(**k):
pass
:param func: function to be wrapped.
:type func: variable kwargs function
:rtype: wrapped function with defined kwargs bound to this
:py:class:`Config` object.
:param func: Decorator parameter. The function or method to be wrapped.
:type func: function or method
:param lazy: Factory parameter. Turns lazy_string_cast on or off.
:type lazy: Boolean value default True
:rtype: As a factory returns decorator function. As a decorator
function returns a decorated function.
"""
return lazy_string_cast(self)(wraps_kwargs(self)(func))

lazy = kwargs.get('lazy', True)
def _wraps(func):
if lazy:
return lazy_string_cast(self)(wraps_kwargs(self)(func))
else:
return wraps_kwargs(self)(func)
if inspect.isfunction(func) or inspect.ismethod(func):
return _wraps(func)
else:
return _wraps

Config._reserved = set(dir(Config))

26 changes: 26 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,29 @@ def func(**k):
self.assertFalse(k['bar'])
func()

def test_not_lazy_flag(self):
config = funconf.Config()
@config(lazy=False)
def func(foo=True):
return foo
self.assertEqual(func(foo="y"), "y")

@config.bar(lazy=False)
def func(foo=True):
return foo
self.assertEqual(func(foo="y"), "y")

def test_is_lazy_flag(self):
config = funconf.Config()
@config(lazy=True)
def func(foo=True):
return foo
self.assertEqual(func(foo="y"), True)

@config.bar(lazy=True)
def func(foo=True):
return foo
self.assertEqual(func(foo="y"), True)



0 comments on commit 174587d

Please sign in to comment.