Skip to content

Commit

Permalink
add wrapping and unwrapping utilities for lithoxyl integration that's…
Browse files Browse the repository at this point in the history
… almost too easy
  • Loading branch information
mahmoud committed May 26, 2016
1 parent 33785ae commit 32cd651
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions lithoxyl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
SensibleFilter,
SensibleFormatter,
SensibleMessageFormatter)
from lithoxyl.sinks import AggregateSink


from boltons.formatutils import DeferredValue
Expand Down
3 changes: 3 additions & 0 deletions lithoxyl/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ def logged_func(*a, **kw):
with rec:
return func_to_log(*a, **kw)

wrapping_info = (self, level, record_name, func_to_log)
logged_func.__lithoxyl_wrapped__ = wrapping_info

return logged_func

return record_wrapper
Expand Down
75 changes: 75 additions & 0 deletions lithoxyl/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,86 @@
# -*- coding: utf-8 -*-

import sys
import inspect
import itertools

from boltons.strutils import indent


def unwrap(target, attr_name):
wrapped_func = getattr(target, attr_name)
try:
unwrapped_func = wrapped_func.__lithoxyl_wrapped__[3]
except Exception:
raise ValueError('%s does not appear to be a wrapped function'
% wrapped_func)
setattr(target, attr_name, unwrapped_func)
return


def unwrap_all(target):
for attr_name in dir(target):
try:
unwrap(target, attr_name)
except ValueError:
continue
return


def wrap_all(logger, level='info', target=None, skip=None,
label=None, level_map=None, extras=None):
"""
"""
ret = []
extras = extras or {}
level_map = level_map or {}

if target is None or isinstance(target, int):
target = target or 1
try:
target_module_name = sys._getframe(target).f_globals.__name__
calling_module = sys.modules[target_module_name]
except Exception:
raise ValueError('unable to wrap all with target: %r' % target)
target = calling_module

if skip is None:
skip = '_'
if isinstance(skip, basestring):
skip_func = lambda attr_name: skip and attr_name.startswith(skip)
elif callable(skip):
skip_func = skip
else:
raise ValueError('skip expected string prefix or callable, not %r' %
(skip,))

if label is None:
try:
label = target.__name__
except AttributeError:
label = '(%s@%s)' % (target.__class__.__name__, hex(id(target)))
label = str(label)

for attr_name in dir(target):
if skip_func(attr_name):
continue
val = getattr(target, attr_name)
if not callable(val) or isinstance(val, type):
continue
kwargs = dict(extras)

kwargs['level'] = level_map.get(attr_name, level)
kwargs['record_name'] = label + '.' + attr_name

log_wrapper = logger.wrap(**kwargs)

wrapped_func = log_wrapper(val)
setattr(target, attr_name, wrapped_func)
ret.append(attr_name)

return ret


def wraps(func, injected=None, **kw):
"""Modeled after the built-in :func:`functools.wraps`, this version of
`wraps` enables a decorator to be more informative and transparent
Expand Down

0 comments on commit 32cd651

Please sign in to comment.