This commit is contained in:
Rob Hudson 2014-10-09 10:56:16 -07:00
Родитель 4ce86351c9
Коммит 45c51a7fc9
4 изменённых файлов: 63 добавлений и 56 удалений

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

@ -1,10 +1,9 @@
"""
Wrapper for celery.task.Task that catches and logs errors.
"""
import functools
import itertools
import logging
import functools
import sys
import celery.task
from celery.signals import task_failure
@ -93,4 +92,3 @@ def chunked(seq, n):
if not rv:
break
yield rv

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

@ -10,6 +10,7 @@ import types
IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
def valid_ident(s):
m = IDENTIFIER.match(s)
if not m:
@ -43,13 +44,14 @@ except ImportError:
# Each wrapper should have a configurator attribute holding the actual
# configurator to use for conversion.
class ConvertingDict(dict):
"""A converting dictionary wrapper."""
def __getitem__(self, key):
value = dict.__getitem__(self, key)
result = self.configurator.convert(value)
#If the converted value is different, save for next time
# If the converted value is different, save for next time
if value is not result:
self[key] = result
if type(result) in (ConvertingDict, ConvertingList,
@ -61,7 +63,7 @@ class ConvertingDict(dict):
def get(self, key, default=None):
value = dict.get(self, key, default)
result = self.configurator.convert(value)
#If the converted value is different, save for next time
# If the converted value is different, save for next time
if value is not result:
self[key] = result
if type(result) in (ConvertingDict, ConvertingList,
@ -80,12 +82,13 @@ class ConvertingDict(dict):
result.key = key
return result
class ConvertingList(list):
"""A converting list wrapper."""
def __getitem__(self, key):
value = list.__getitem__(self, key)
result = self.configurator.convert(value)
#If the converted value is different, save for next time
# If the converted value is different, save for next time
if value is not result:
self[key] = result
if type(result) in (ConvertingDict, ConvertingList,
@ -103,6 +106,7 @@ class ConvertingList(list):
result.parent = self
return result
class ConvertingTuple(tuple):
"""A converting tuple wrapper."""
def __getitem__(self, key):
@ -115,6 +119,7 @@ class ConvertingTuple(tuple):
result.key = key
return result
class BaseConfigurator(object):
"""
The configurator base class which defines some useful defaults.
@ -128,11 +133,11 @@ class BaseConfigurator(object):
DIGIT_PATTERN = re.compile(r'^\d+$')
value_converters = {
'ext' : 'ext_convert',
'cfg' : 'cfg_convert',
'ext': 'ext_convert',
'cfg': 'cfg_convert',
}
# We might want to use a different one, e.g. importlib
# We might want to use a different one, e.g. importlib.
importer = __import__
def __init__(self, config):
@ -169,7 +174,6 @@ class BaseConfigurator(object):
else:
rest = rest[m.end():]
d = self.config[m.groups()[0]]
#print d, rest
while rest:
m = self.DOT_PATTERN.match(rest)
if m:
@ -182,7 +186,8 @@ class BaseConfigurator(object):
d = d[idx]
else:
try:
n = int(idx) # try as number first (most likely)
# try as number first (most likely)
n = int(idx)
d = d[n]
except TypeError:
d = d[idx]
@ -191,7 +196,7 @@ class BaseConfigurator(object):
else:
raise ValueError('Unable to convert '
'%r at %r' % (value, rest))
#rest should be empty
# rest should be empty
return d
def convert(self, value):
@ -206,11 +211,10 @@ class BaseConfigurator(object):
elif not isinstance(value, ConvertingList) and isinstance(value, list):
value = ConvertingList(value)
value.configurator = self
elif not isinstance(value, ConvertingTuple) and\
isinstance(value, tuple):
elif not isinstance(value, ConvertingTuple) and isinstance(value, tuple):
value = ConvertingTuple(value)
value.configurator = self
elif isinstance(value, basestring): # str for py3k
elif isinstance(value, basestring): # str for py3k
m = self.CONVERT_PATTERN.match(value)
if m:
d = m.groupdict()
@ -225,7 +229,8 @@ class BaseConfigurator(object):
def configure_custom(self, config):
"""Configure an object with a user-supplied factory."""
c = config.pop('()')
if not hasattr(c, '__call__') and hasattr(types, 'ClassType') and type(c) != types.ClassType:
if (not hasattr(c, '__call__') and hasattr(types, 'ClassType') and
not isinstance(c, types.ClassType)):
c = self.resolve(c)
props = config.pop('.', None)
# Check for valid identifiers
@ -242,6 +247,7 @@ class BaseConfigurator(object):
value = tuple(value)
return value
class DictConfigurator(BaseConfigurator):
"""
Configure logging using a dictionary-like object to describe the
@ -268,7 +274,7 @@ class DictConfigurator(BaseConfigurator):
for name in handlers:
if name not in logging._handlers:
raise ValueError('No handler found with '
'name %r' % name)
'name %r' % name)
else:
try:
handler = logging._handlers[name]
@ -304,7 +310,7 @@ class DictConfigurator(BaseConfigurator):
for name in formatters:
try:
formatters[name] = self.configure_formatter(
formatters[name])
formatters[name])
except StandardError, e:
raise ValueError('Unable to configure '
'formatter %r: %s' % (name, e))
@ -331,25 +337,25 @@ class DictConfigurator(BaseConfigurator):
'%r: %s' % (name, e))
# Next, do loggers - they refer to handlers and filters
#we don't want to lose the existing loggers,
#since other threads may have pointers to them.
#existing is set to contain all existing loggers,
#and as we go through the new configuration we
#remove any which are configured. At the end,
#what's left in existing is the set of loggers
#which were in the previous configuration but
#which are not in the new configuration.
# we don't want to lose the existing loggers,
# since other threads may have pointers to them.
# existing is set to contain all existing loggers,
# and as we go through the new configuration we
# remove any which are configured. At the end,
# what's left in existing is the set of loggers
# which were in the previous configuration but
# which are not in the new configuration.
root = logging.root
existing = root.manager.loggerDict.keys()
#The list needs to be sorted so that we can
#avoid disabling child loggers of explicitly
#named loggers. With a sorted list it is easier
#to find the child loggers.
# The list needs to be sorted so that we can
# avoid disabling child loggers of explicitly
# named loggers. With a sorted list it is easier
# to find the child loggers.
existing.sort()
#We'll keep the list of existing loggers
#which are children of named loggers here...
# We'll keep the list of existing loggers
# which are children of named loggers here...
child_loggers = []
#now set up the new ones...
# now set up the new ones...
loggers = config.get('loggers', EMPTY_DICT)
for name in loggers:
if name in existing:
@ -357,7 +363,7 @@ class DictConfigurator(BaseConfigurator):
prefixed = name + "."
pflen = len(prefixed)
num_existing = len(existing)
i = i + 1 # look at the entry after name
i = i + 1 # look at the entry after name
while (i < num_existing) and\
(existing[i][:pflen] == prefixed):
child_loggers.append(existing[i])
@ -369,11 +375,11 @@ class DictConfigurator(BaseConfigurator):
raise ValueError('Unable to configure logger '
'%r: %s' % (name, e))
#Disable any old loggers. There's no point deleting
#them as other threads may continue to hold references
#and by disabling them, you stop them doing any logging.
#However, don't disable children of named loggers, as that's
#probably not what was intended by the user.
# Disable any old loggers. There's no point deleting
# them as other threads may continue to hold references
# and by disabling them, you stop them doing any logging.
# However, don't disable children of named loggers, as that's
# probably not what was intended by the user.
for log in existing:
logger = root.manager.loggerDict[log]
if log in child_loggers:
@ -397,16 +403,16 @@ class DictConfigurator(BaseConfigurator):
def configure_formatter(self, config):
"""Configure a formatter from a dictionary."""
if '()' in config:
factory = config['()'] # for use in exception handler
factory = config['()'] # for use in exception handler
try:
result = self.configure_custom(config)
except TypeError, te:
if "'format'" not in str(te):
raise
#Name of parameter changed from fmt to format.
#Retry with old name.
#This is so that code can be used with older Python versions
#(e.g. by Django)
# Name of parameter changed from fmt to format.
# Retry with old name.
# This is so that code can be used with older Python versions
# (e.g. by Django)
config['fmt'] = config.pop('format')
config['()'] = factory
result = self.configure_custom(config)
@ -446,12 +452,13 @@ class DictConfigurator(BaseConfigurator):
filters = config.pop('filters', None)
if '()' in config:
c = config.pop('()')
if not hasattr(c, '__call__') and hasattr(types, 'ClassType') and type(c) != types.ClassType:
if (not hasattr(c, '__call__') and hasattr(types, 'ClassType') and
not isinstance(c, types.ClassType)):
c = self.resolve(c)
factory = c
else:
klass = self.resolve(config.pop('class'))
#Special case for handler which refers to another handler
# Special case for handler which refers to another handler
if issubclass(klass, logging.handlers.MemoryHandler) and\
'target' in config:
try:
@ -472,10 +479,10 @@ class DictConfigurator(BaseConfigurator):
except TypeError, te:
if "'stream'" not in str(te):
raise
#The argument name changed from strm to stream
#Retry with old name.
#This is so that code can be used with older Python versions
#(e.g. by Django)
# The argument name changed from strm to stream
# Retry with old name.
# This is so that code can be used with older Python versions
# (e.g. by Django)
kwargs['strm'] = kwargs.pop('stream')
result = factory(**kwargs)
if formatter:
@ -502,7 +509,7 @@ class DictConfigurator(BaseConfigurator):
if level is not None:
logger.setLevel(_checkLevel(level))
if not incremental:
#Remove any existing handlers
# Remove any existing handlers
for h in logger.handlers[:]:
logger.removeHandler(h)
handlers = config.get('handlers', None)
@ -525,8 +532,10 @@ class DictConfigurator(BaseConfigurator):
root = logging.getLogger()
self.common_logger_config(root, config, incremental)
dictConfigClass = DictConfigurator
def dictConfig(config):
"""Configure logging using a dictionary."""
dictConfigClass(config).configure()

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

@ -1,6 +1,6 @@
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.utils import simplejson as json
from django.core.serializers.json import DjangoJSONEncoder
# https://bitbucket.org/offline/django-annoying

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

@ -73,7 +73,7 @@ class MockRedis(object):
def execute(self):
pass
## Keys.
# Keys.
def get(self, key):
return self.kv.get(key)
@ -98,7 +98,7 @@ class MockRedis(object):
return True
return False
## Sets.
# Sets.
def sadd(self, key, val):
v = self.kv.setdefault(key, set())
@ -120,7 +120,7 @@ class MockRedis(object):
sets = [self.kv.get(key, set()) for key in keys]
return reduce(lambda x, y: x & y, sets)
## Hashes.
# Hashes.
def hmget(self, name, keys):
db = self.kv.get(name, StringDict())
@ -160,7 +160,7 @@ class MockRedis(object):
val = db.setdefault(key, 0)
db[key] = val + amount
## Lists.
# Lists.
def rpush(self, name, *vals):
list_ = self.kv.get(name, [])