Fix docs.
[pithos] / pithos / lib / dictconfig.py
1 # This is a copy of the Python logging.config.dictconfig module.
2 # It is provided here for backwards compatibility for Python versions
3 # prior to 2.7.
4 #
5 # Copyright 2009-2010 by Vinay Sajip. All Rights Reserved.
6 #
7 # Permission to use, copy, modify, and distribute this software and its
8 # documentation for any purpose and without fee is hereby granted,
9 # provided that the above copyright notice appear in all copies and that
10 # both that copyright notice and this permission notice appear in
11 # supporting documentation, and that the name of Vinay Sajip
12 # not be used in advertising or publicity pertaining to distribution
13 # of the software without specific, written prior permission.
14 # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
16 # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
18 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
21 import logging.handlers
22 import re
23 import sys
24 import types
25
26 IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
27
28 def valid_ident(s):
29     m = IDENTIFIER.match(s)
30     if not m:
31         raise ValueError('Not a valid Python identifier: %r' % s)
32     return True
33
34 #
35 # This function is defined in logging only in recent versions of Python
36 #
37 try:
38     from logging import _checkLevel
39 except ImportError:
40     def _checkLevel(level):
41         if isinstance(level, int):
42             rv = level
43         elif str(level) == level:
44             if level not in logging._levelNames:
45                 raise ValueError('Unknown level: %r' % level)
46             rv = logging._levelNames[level]
47         else:
48             raise TypeError('Level not an integer or a '
49                             'valid string: %r' % level)
50         return rv
51
52 # The ConvertingXXX classes are wrappers around standard Python containers,
53 # and they serve to convert any suitable values in the container. The
54 # conversion converts base dicts, lists and tuples to their wrapped
55 # equivalents, whereas strings which match a conversion format are converted
56 # appropriately.
57 #
58 # Each wrapper should have a configurator attribute holding the actual
59 # configurator to use for conversion.
60
61 class ConvertingDict(dict):
62     """A converting dictionary wrapper."""
63
64     def __getitem__(self, key):
65         value = dict.__getitem__(self, key)
66         result = self.configurator.convert(value)
67         #If the converted value is different, save for next time
68         if value is not result:
69             self[key] = result
70             if type(result) in (ConvertingDict, ConvertingList,
71                                 ConvertingTuple):
72                 result.parent = self
73                 result.key = key
74         return result
75
76     def get(self, key, default=None):
77         value = dict.get(self, key, default)
78         result = self.configurator.convert(value)
79         #If the converted value is different, save for next time
80         if value is not result:
81             self[key] = result
82             if type(result) in (ConvertingDict, ConvertingList,
83                                 ConvertingTuple):
84                 result.parent = self
85                 result.key = key
86         return result
87
88     def pop(self, key, default=None):
89         value = dict.pop(self, key, default)
90         result = self.configurator.convert(value)
91         if value is not result:
92             if type(result) in (ConvertingDict, ConvertingList,
93                                 ConvertingTuple):
94                 result.parent = self
95                 result.key = key
96         return result
97
98 class ConvertingList(list):
99     """A converting list wrapper."""
100     def __getitem__(self, key):
101         value = list.__getitem__(self, key)
102         result = self.configurator.convert(value)
103         #If the converted value is different, save for next time
104         if value is not result:
105             self[key] = result
106             if type(result) in (ConvertingDict, ConvertingList,
107                                 ConvertingTuple):
108                 result.parent = self
109                 result.key = key
110         return result
111
112     def pop(self, idx=-1):
113         value = list.pop(self, idx)
114         result = self.configurator.convert(value)
115         if value is not result:
116             if type(result) in (ConvertingDict, ConvertingList,
117                                 ConvertingTuple):
118                 result.parent = self
119         return result
120
121 class ConvertingTuple(tuple):
122     """A converting tuple wrapper."""
123     def __getitem__(self, key):
124         value = tuple.__getitem__(self, key)
125         result = self.configurator.convert(value)
126         if value is not result:
127             if type(result) in (ConvertingDict, ConvertingList,
128                                 ConvertingTuple):
129                 result.parent = self
130                 result.key = key
131         return result
132
133 class BaseConfigurator(object):
134     """
135     The configurator base class which defines some useful defaults.
136     """
137
138     CONVERT_PATTERN = re.compile(r'^(?P<prefix>[a-z]+)://(?P<suffix>.*)$')
139
140     WORD_PATTERN = re.compile(r'^\s*(\w+)\s*')
141     DOT_PATTERN = re.compile(r'^\.\s*(\w+)\s*')
142     INDEX_PATTERN = re.compile(r'^\[\s*(\w+)\s*\]\s*')
143     DIGIT_PATTERN = re.compile(r'^\d+$')
144
145     value_converters = {
146         'ext' : 'ext_convert',
147         'cfg' : 'cfg_convert',
148     }
149
150     # We might want to use a different one, e.g. importlib
151     importer = __import__
152
153     def __init__(self, config):
154         self.config = ConvertingDict(config)
155         self.config.configurator = self
156
157     def resolve(self, s):
158         """
159         Resolve strings to objects using standard import and attribute
160         syntax.
161         """
162         name = s.split('.')
163         used = name.pop(0)
164         try:
165             found = self.importer(used)
166             for frag in name:
167                 used += '.' + frag
168                 try:
169                     found = getattr(found, frag)
170                 except AttributeError:
171                     self.importer(used)
172                     found = getattr(found, frag)
173             return found
174         except ImportError:
175             e, tb = sys.exc_info()[1:]
176             v = ValueError('Cannot resolve %r: %s' % (s, e))
177             v.__cause__, v.__traceback__ = e, tb
178             raise v
179
180     def ext_convert(self, value):
181         """Default converter for the ext:// protocol."""
182         return self.resolve(value)
183
184     def cfg_convert(self, value):
185         """Default converter for the cfg:// protocol."""
186         rest = value
187         m = self.WORD_PATTERN.match(rest)
188         if m is None:
189             raise ValueError("Unable to convert %r" % value)
190         else:
191             rest = rest[m.end():]
192             d = self.config[m.groups()[0]]
193             #print d, rest
194             while rest:
195                 m = self.DOT_PATTERN.match(rest)
196                 if m:
197                     d = d[m.groups()[0]]
198                 else:
199                     m = self.INDEX_PATTERN.match(rest)
200                     if m:
201                         idx = m.groups()[0]
202                         if not self.DIGIT_PATTERN.match(idx):
203                             d = d[idx]
204                         else:
205                             try:
206                                 n = int(idx) # try as number first (most likely)
207                                 d = d[n]
208                             except TypeError:
209                                 d = d[idx]
210                 if m:
211                     rest = rest[m.end():]
212                 else:
213                     raise ValueError('Unable to convert '
214                                      '%r at %r' % (value, rest))
215         #rest should be empty
216         return d
217
218     def convert(self, value):
219         """
220         Convert values to an appropriate type. dicts, lists and tuples are
221         replaced by their converting alternatives. Strings are checked to
222         see if they have a conversion format and are converted if they do.
223         """
224         if not isinstance(value, ConvertingDict) and isinstance(value, dict):
225             value = ConvertingDict(value)
226             value.configurator = self
227         elif not isinstance(value, ConvertingList) and isinstance(value, list):
228             value = ConvertingList(value)
229             value.configurator = self
230         elif not isinstance(value, ConvertingTuple) and\
231                  isinstance(value, tuple):
232             value = ConvertingTuple(value)
233             value.configurator = self
234         elif isinstance(value, basestring): # str for py3k
235             m = self.CONVERT_PATTERN.match(value)
236             if m:
237                 d = m.groupdict()
238                 prefix = d['prefix']
239                 converter = self.value_converters.get(prefix, None)
240                 if converter:
241                     suffix = d['suffix']
242                     converter = getattr(self, converter)
243                     value = converter(suffix)
244         return value
245
246     def configure_custom(self, config):
247         """Configure an object with a user-supplied factory."""
248         c = config.pop('()')
249         if not hasattr(c, '__call__') and hasattr(types, 'ClassType') and type(c) != types.ClassType:
250             c = self.resolve(c)
251         props = config.pop('.', None)
252         # Check for valid identifiers
253         kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
254         result = c(**kwargs)
255         if props:
256             for name, value in props.items():
257                 setattr(result, name, value)
258         return result
259
260     def as_tuple(self, value):
261         """Utility function which converts lists to tuples."""
262         if isinstance(value, list):
263             value = tuple(value)
264         return value
265
266 class DictConfigurator(BaseConfigurator):
267     """
268     Configure logging using a dictionary-like object to describe the
269     configuration.
270     """
271
272     def configure(self):
273         """Do the configuration."""
274
275         config = self.config
276         if 'version' not in config:
277             raise ValueError("dictionary doesn't specify a version")
278         if config['version'] != 1:
279             raise ValueError("Unsupported version: %s" % config['version'])
280         incremental = config.pop('incremental', False)
281         EMPTY_DICT = {}
282         logging._acquireLock()
283         try:
284             if incremental:
285                 handlers = config.get('handlers', EMPTY_DICT)
286                 # incremental handler config only if handler name
287                 # ties in to logging._handlers (Python 2.7)
288                 if sys.version_info[:2] == (2, 7):
289                     for name in handlers:
290                         if name not in logging._handlers:
291                             raise ValueError('No handler found with '
292                                              'name %r'  % name)
293                         else:
294                             try:
295                                 handler = logging._handlers[name]
296                                 handler_config = handlers[name]
297                                 level = handler_config.get('level', None)
298                                 if level:
299                                     handler.setLevel(_checkLevel(level))
300                             except StandardError, e:
301                                 raise ValueError('Unable to configure handler '
302                                                  '%r: %s' % (name, e))
303                 loggers = config.get('loggers', EMPTY_DICT)
304                 for name in loggers:
305                     try:
306                         self.configure_logger(name, loggers[name], True)
307                     except StandardError, e:
308                         raise ValueError('Unable to configure logger '
309                                          '%r: %s' % (name, e))
310                 root = config.get('root', None)
311                 if root:
312                     try:
313                         self.configure_root(root, True)
314                     except StandardError, e:
315                         raise ValueError('Unable to configure root '
316                                          'logger: %s' % e)
317             else:
318                 disable_existing = config.pop('disable_existing_loggers', True)
319
320                 logging._handlers.clear()
321                 del logging._handlerList[:]
322
323                 # Do formatters first - they don't refer to anything else
324                 formatters = config.get('formatters', EMPTY_DICT)
325                 for name in formatters:
326                     try:
327                         formatters[name] = self.configure_formatter(
328                                                             formatters[name])
329                     except StandardError, e:
330                         raise ValueError('Unable to configure '
331                                          'formatter %r: %s' % (name, e))
332                 # Next, do filters - they don't refer to anything else, either
333                 filters = config.get('filters', EMPTY_DICT)
334                 for name in filters:
335                     try:
336                         filters[name] = self.configure_filter(filters[name])
337                     except StandardError, e:
338                         raise ValueError('Unable to configure '
339                                          'filter %r: %s' % (name, e))
340
341                 # Next, do handlers - they refer to formatters and filters
342                 # As handlers can refer to other handlers, sort the keys
343                 # to allow a deterministic order of configuration
344                 handlers = config.get('handlers', EMPTY_DICT)
345                 for name in sorted(handlers):
346                     try:
347                         handler = self.configure_handler(handlers[name])
348                         handler.name = name
349                         handlers[name] = handler
350                     except StandardError, e:
351                         raise ValueError('Unable to configure handler '
352                                          '%r: %s' % (name, e))
353                 # Next, do loggers - they refer to handlers and filters
354
355                 #we don't want to lose the existing loggers,
356                 #since other threads may have pointers to them.
357                 #existing is set to contain all existing loggers,
358                 #and as we go through the new configuration we
359                 #remove any which are configured. At the end,
360                 #what's left in existing is the set of loggers
361                 #which were in the previous configuration but
362                 #which are not in the new configuration.
363                 root = logging.root
364                 existing = root.manager.loggerDict.keys()
365                 #The list needs to be sorted so that we can
366                 #avoid disabling child loggers of explicitly
367                 #named loggers. With a sorted list it is easier
368                 #to find the child loggers.
369                 existing.sort()
370                 #We'll keep the list of existing loggers
371                 #which are children of named loggers here...
372                 child_loggers = []
373                 #now set up the new ones...
374                 loggers = config.get('loggers', EMPTY_DICT)
375                 for name in loggers:
376                     if name in existing:
377                         i = existing.index(name)
378                         prefixed = name + "."
379                         pflen = len(prefixed)
380                         num_existing = len(existing)
381                         i = i + 1 # look at the entry after name
382                         while (i < num_existing) and\
383                               (existing[i][:pflen] == prefixed):
384                             child_loggers.append(existing[i])
385                             i = i + 1
386                         existing.remove(name)
387                     try:
388                         self.configure_logger(name, loggers[name])
389                     except StandardError, e:
390                         raise ValueError('Unable to configure logger '
391                                          '%r: %s' % (name, e))
392
393                 #Disable any old loggers. There's no point deleting
394                 #them as other threads may continue to hold references
395                 #and by disabling them, you stop them doing any logging.
396                 #However, don't disable children of named loggers, as that's
397                 #probably not what was intended by the user.
398                 for log in existing:
399                     logger = root.manager.loggerDict[log]
400                     if log in child_loggers:
401                         logger.level = logging.NOTSET
402                         logger.handlers = []
403                         logger.propagate = True
404                     elif disable_existing:
405                         logger.disabled = True
406
407                 # And finally, do the root logger
408                 root = config.get('root', None)
409                 if root:
410                     try:
411                         self.configure_root(root)
412                     except StandardError, e:
413                         raise ValueError('Unable to configure root '
414                                          'logger: %s' % e)
415         finally:
416             logging._releaseLock()
417
418     def configure_formatter(self, config):
419         """Configure a formatter from a dictionary."""
420         if '()' in config:
421             factory = config['()'] # for use in exception handler
422             try:
423                 result = self.configure_custom(config)
424             except TypeError, te:
425                 if "'format'" not in str(te):
426                     raise
427                 #Name of parameter changed from fmt to format.
428                 #Retry with old name.
429                 #This is so that code can be used with older Python versions
430                 #(e.g. by Django)
431                 config['fmt'] = config.pop('format')
432                 config['()'] = factory
433                 result = self.configure_custom(config)
434         else:
435             fmt = config.get('format', None)
436             dfmt = config.get('datefmt', None)
437             result = logging.Formatter(fmt, dfmt)
438         return result
439
440     def configure_filter(self, config):
441         """Configure a filter from a dictionary."""
442         if '()' in config:
443             result = self.configure_custom(config)
444         else:
445             name = config.get('name', '')
446             result = logging.Filter(name)
447         return result
448
449     def add_filters(self, filterer, filters):
450         """Add filters to a filterer from a list of names."""
451         for f in filters:
452             try:
453                 filterer.addFilter(self.config['filters'][f])
454             except StandardError, e:
455                 raise ValueError('Unable to add filter %r: %s' % (f, e))
456
457     def configure_handler(self, config):
458         """Configure a handler from a dictionary."""
459         formatter = config.pop('formatter', None)
460         if formatter:
461             try:
462                 formatter = self.config['formatters'][formatter]
463             except StandardError, e:
464                 raise ValueError('Unable to set formatter '
465                                  '%r: %s' % (formatter, e))
466         level = config.pop('level', None)
467         filters = config.pop('filters', None)
468         if '()' in config:
469             c = config.pop('()')
470             if not hasattr(c, '__call__') and hasattr(types, 'ClassType') and type(c) != types.ClassType:
471                 c = self.resolve(c)
472             factory = c
473         else:
474             klass = self.resolve(config.pop('class'))
475             #Special case for handler which refers to another handler
476             if issubclass(klass, logging.handlers.MemoryHandler) and\
477                 'target' in config:
478                 try:
479                     config['target'] = self.config['handlers'][config['target']]
480                 except StandardError, e:
481                     raise ValueError('Unable to set target handler '
482                                      '%r: %s' % (config['target'], e))
483             elif issubclass(klass, logging.handlers.SMTPHandler) and\
484                 'mailhost' in config:
485                 config['mailhost'] = self.as_tuple(config['mailhost'])
486             elif issubclass(klass, logging.handlers.SysLogHandler) and\
487                 'address' in config:
488                 config['address'] = self.as_tuple(config['address'])
489             factory = klass
490         kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
491         try:
492             result = factory(**kwargs)
493         except TypeError, te:
494             if "'stream'" not in str(te):
495                 raise
496             #The argument name changed from strm to stream
497             #Retry with old name.
498             #This is so that code can be used with older Python versions
499             #(e.g. by Django)
500             kwargs['strm'] = kwargs.pop('stream')
501             result = factory(**kwargs)
502         if formatter:
503             result.setFormatter(formatter)
504         if level is not None:
505             result.setLevel(_checkLevel(level))
506         if filters:
507             self.add_filters(result, filters)
508         return result
509
510     def add_handlers(self, logger, handlers):
511         """Add handlers to a logger from a list of names."""
512         for h in handlers:
513             try:
514                 logger.addHandler(self.config['handlers'][h])
515             except StandardError, e:
516                 raise ValueError('Unable to add handler %r: %s' % (h, e))
517
518     def common_logger_config(self, logger, config, incremental=False):
519         """
520         Perform configuration which is common to root and non-root loggers.
521         """
522         level = config.get('level', None)
523         if level is not None:
524             logger.setLevel(_checkLevel(level))
525         if not incremental:
526             #Remove any existing handlers
527             for h in logger.handlers[:]:
528                 logger.removeHandler(h)
529             handlers = config.get('handlers', None)
530             if handlers:
531                 self.add_handlers(logger, handlers)
532             filters = config.get('filters', None)
533             if filters:
534                 self.add_filters(logger, filters)
535
536     def configure_logger(self, name, config, incremental=False):
537         """Configure a non-root logger from a dictionary."""
538         logger = logging.getLogger(name)
539         self.common_logger_config(logger, config, incremental)
540         propagate = config.get('propagate', None)
541         if propagate is not None:
542             logger.propagate = propagate
543
544     def configure_root(self, config, incremental=False):
545         """Configure a root logger from a dictionary."""
546         root = logging.getLogger()
547         self.common_logger_config(root, config, incremental)
548
549 dictConfigClass = DictConfigurator
550
551 def dictConfig(config):
552     """Configure logging using a dictionary."""
553     dictConfigClass(config).configure()