<s> import logging . handlers <EOL> import re <EOL> import sys <EOL> import types <EOL> from django . utils import six <EOL> IDENTIFIER = re . compile ( '<STR_LIT>' , re . I ) <EOL> def valid_ident ( s ) : <EOL> m = IDENTIFIER . match ( s ) <EOL> if not m : <EOL> raise ValueError ( '<STR_LIT>' % s ) <EOL> return True <EOL> try : <EOL> from logging import _checkLevel <EOL> except ImportError : <EOL> def _checkLevel ( level ) : <EOL> if isinstance ( level , int ) : <EOL> rv = level <EOL> elif str ( level ) == level : <EOL> if level not in logging . _levelNames : <EOL> raise ValueError ( '<STR_LIT>' % level ) <EOL> rv = logging . _levelNames [ level ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % level ) <EOL> return rv <EOL> class ConvertingDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __getitem__ ( self , key ) : <EOL> value = dict . __getitem__ ( self , key ) <EOL> result = self . configurator . convert ( value ) <EOL> if value is not result : <EOL> self [ key ] = result <EOL> if type ( result ) in ( ConvertingDict , ConvertingList , <EOL> ConvertingTuple ) : <EOL> result . parent = self <EOL> result . key = key <EOL> return result <EOL> def get ( self , key , default = None ) : <EOL> value = dict . get ( self , key , default ) <EOL> result = self . configurator . convert ( value ) <EOL> if value is not result : <EOL> self [ key ] = result <EOL> if type ( result ) in ( ConvertingDict , ConvertingList , <EOL> ConvertingTuple ) : <EOL> result . parent = self <EOL> result . key = key <EOL> return result <EOL> def pop ( self , key , default = None ) : <EOL> value = dict . pop ( self , key , default ) <EOL> result = self . configurator . convert ( value ) <EOL> if value is not result : <EOL> if type ( result ) in ( ConvertingDict , ConvertingList , <EOL> ConvertingTuple ) : <EOL> result . parent = self <EOL> result . key = key <EOL> return result <EOL> class ConvertingList ( list ) : <EOL> """<STR_LIT>""" <EOL> def __getitem__ ( self , key ) : <EOL> value = list . __getitem__ ( self , key ) <EOL> result = self . configurator . convert ( value ) <EOL> if value is not result : <EOL> self [ key ] = result <EOL> if type ( result ) in ( ConvertingDict , ConvertingList , <EOL> ConvertingTuple ) : <EOL> result . parent = self <EOL> result . key = key <EOL> return result <EOL> def pop ( self , idx = - <NUM_LIT:1> ) : <EOL> value = list . pop ( self , idx ) <EOL> result = self . configurator . convert ( value ) <EOL> if value is not result : <EOL> if type ( result ) in ( ConvertingDict , ConvertingList , <EOL> ConvertingTuple ) : <EOL> result . parent = self <EOL> return result <EOL> class ConvertingTuple ( tuple ) : <EOL> """<STR_LIT>""" <EOL> def __getitem__ ( self , key ) : <EOL> value = tuple . __getitem__ ( self , key ) <EOL> result = self . configurator . convert ( value ) <EOL> if value is not result : <EOL> if type ( result ) in ( ConvertingDict , ConvertingList , <EOL> ConvertingTuple ) : <EOL> result . parent = self <EOL> result . key = key <EOL> return result <EOL> class BaseConfigurator ( object ) : <EOL> """<STR_LIT>""" <EOL> CONVERT_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> WORD_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> DOT_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> INDEX_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> DIGIT_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> value_converters = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> importer = __import__ <EOL> def __init__ ( self , config ) : <EOL> self . config = ConvertingDict ( config ) <EOL> self . config . configurator = self <EOL> def resolve ( self , s ) : <EOL> """<STR_LIT>""" <EOL> name = s . split ( '<STR_LIT:.>' ) <EOL> used = name . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> found = self . importer ( used ) <EOL> for frag in name : <EOL> used += '<STR_LIT:.>' + frag <EOL> try : <EOL> found = getattr ( found , frag ) <EOL> except AttributeError : <EOL> self . importer ( used ) <EOL> found = getattr ( found , frag ) <EOL> return found <EOL> except ImportError : <EOL> e , tb = sys . exc_info ( ) [ <NUM_LIT:1> : ] <EOL> v = ValueError ( '<STR_LIT>' % ( s , e ) ) <EOL> v . __cause__ , v . __traceback__ = e , tb <EOL> raise v <EOL> def ext_convert ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . resolve ( value ) <EOL> def cfg_convert ( self , value ) : <EOL> """<STR_LIT>""" <EOL> rest = value <EOL> m = self . WORD_PATTERN . match ( rest ) <EOL> if m is None : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> else : <EOL> rest = rest [ m . end ( ) : ] <EOL> d = self . config [ m . groups ( ) [ <NUM_LIT:0> ] ] <EOL> while rest : <EOL> m = self . DOT_PATTERN . match ( rest ) <EOL> if m : <EOL> d = d [ m . groups ( ) [ <NUM_LIT:0> ] ] <EOL> else : <EOL> m = self . INDEX_PATTERN . match ( rest ) <EOL> if m : <EOL> idx = m . groups ( ) [ <NUM_LIT:0> ] <EOL> if not self . DIGIT_PATTERN . match ( idx ) : <EOL> d = d [ idx ] <EOL> else : <EOL> try : <EOL> n = int ( idx ) <EOL> d = d [ n ] <EOL> except TypeError : <EOL> d = d [ idx ] <EOL> if m : <EOL> rest = rest [ m . end ( ) : ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( value , rest ) ) <EOL> return d <EOL> def convert ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , ConvertingDict ) and isinstance ( value , dict ) : <EOL> value = ConvertingDict ( value ) <EOL> value . configurator = self <EOL> elif not isinstance ( value , ConvertingList ) and isinstance ( value , list ) : <EOL> value = ConvertingList ( value ) <EOL> value . configurator = self <EOL> elif not isinstance ( value , ConvertingTuple ) and isinstance ( value , tuple ) : <EOL> value = ConvertingTuple ( value ) <EOL> value . configurator = self <EOL> elif isinstance ( value , six . string_types ) : <EOL> m = self . CONVERT_PATTERN . match ( value ) <EOL> if m : <EOL> d = m . groupdict ( ) <EOL> prefix = d [ '<STR_LIT>' ] <EOL> converter = self . value_converters . get ( prefix , None ) <EOL> if converter : <EOL> suffix = d [ '<STR_LIT>' ] <EOL> converter = getattr ( self , converter ) <EOL> value = converter ( suffix ) <EOL> return value <EOL> def configure_custom ( self , config ) : <EOL> """<STR_LIT>""" <EOL> c = config . pop ( '<STR_LIT>' ) <EOL> if not hasattr ( c , '<STR_LIT>' ) and hasattr ( types , '<STR_LIT>' ) and type ( c ) != types . ClassType : <EOL> c = self . resolve ( c ) <EOL> props = config . pop ( '<STR_LIT:.>' , None ) <EOL> kwargs = dict ( [ ( k , config [ k ] ) for k in config if valid_ident ( k ) ] ) <EOL> result = c ( ** kwargs ) <EOL> if props : <EOL> for name , value in props . items ( ) : <EOL> setattr ( result , name , value ) <EOL> return result <EOL> def as_tuple ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , list ) : <EOL> value = tuple ( value ) <EOL> return value <EOL> class DictConfigurator ( BaseConfigurator ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self ) : <EOL> """<STR_LIT>""" <EOL> config = self . config <EOL> if '<STR_LIT:version>' not in config : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if config [ '<STR_LIT:version>' ] != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % config [ '<STR_LIT:version>' ] ) <EOL> incremental = config . pop ( '<STR_LIT>' , False ) <EOL> EMPTY_DICT = { } <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> if incremental : <EOL> handlers = config . get ( '<STR_LIT>' , EMPTY_DICT ) <EOL> if sys . version_info [ : <NUM_LIT:2> ] == ( <NUM_LIT:2> , <NUM_LIT:7> ) : <EOL> for name in handlers : <EOL> if name not in logging . _handlers : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> else : <EOL> try : <EOL> handler = logging . _handlers [ name ] <EOL> handler_config = handlers [ name ] <EOL> level = handler_config . get ( '<STR_LIT>' , None ) <EOL> if level : <EOL> handler . setLevel ( _checkLevel ( level ) ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , e ) ) <EOL> loggers = config . get ( '<STR_LIT>' , EMPTY_DICT ) <EOL> for name in loggers : <EOL> try : <EOL> self . configure_logger ( name , loggers [ name ] , True ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , e ) ) <EOL> root = config . get ( '<STR_LIT:root>' , None ) <EOL> if root : <EOL> try : <EOL> self . configure_root ( root , True ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % e ) <EOL> else : <EOL> disable_existing = config . pop ( '<STR_LIT>' , True ) <EOL> logging . _handlers . clear ( ) <EOL> del logging . _handlerList [ : ] <EOL> formatters = config . get ( '<STR_LIT>' , EMPTY_DICT ) <EOL> for name in formatters : <EOL> try : <EOL> formatters [ name ] = self . configure_formatter ( <EOL> formatters [ name ] ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , e ) ) <EOL> filters = config . get ( '<STR_LIT>' , EMPTY_DICT ) <EOL> for name in filters : <EOL> try : <EOL> filters [ name ] = self . configure_filter ( filters [ name ] ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , e ) ) <EOL> handlers = config . get ( '<STR_LIT>' , EMPTY_DICT ) <EOL> for name in sorted ( handlers ) : <EOL> try : <EOL> handler = self . configure_handler ( handlers [ name ] ) <EOL> handler . name = name <EOL> handlers [ name ] = handler <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , e ) ) <EOL> root = logging . root <EOL> existing = list ( root . manager . loggerDict ) <EOL> existing . sort ( ) <EOL> child_loggers = [ ] <EOL> loggers = config . get ( '<STR_LIT>' , EMPTY_DICT ) <EOL> for name in loggers : <EOL> if name in existing : <EOL> i = existing . index ( name ) <EOL> prefixed = name + "<STR_LIT:.>" <EOL> pflen = len ( prefixed ) <EOL> num_existing = len ( existing ) <EOL> i = i + <NUM_LIT:1> <EOL> while ( i < num_existing ) and ( existing [ i ] [ : pflen ] == prefixed ) : <EOL> child_loggers . append ( existing [ i ] ) <EOL> i = i + <NUM_LIT:1> <EOL> existing . remove ( name ) <EOL> try : <EOL> self . configure_logger ( name , loggers [ name ] ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , e ) ) <EOL> for log in existing : <EOL> logger = root . manager . loggerDict [ log ] <EOL> if log in child_loggers : <EOL> logger . level = logging . NOTSET <EOL> logger . handlers = [ ] <EOL> logger . propagate = True <EOL> elif disable_existing : <EOL> logger . disabled = True <EOL> root = config . get ( '<STR_LIT:root>' , None ) <EOL> if root : <EOL> try : <EOL> self . configure_root ( root ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % e ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> def configure_formatter ( self , config ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in config : <EOL> factory = config [ '<STR_LIT>' ] <EOL> try : <EOL> result = self . configure_custom ( config ) <EOL> except TypeError as te : <EOL> if "<STR_LIT>" not in str ( te ) : <EOL> raise <EOL> config [ '<STR_LIT>' ] = config . pop ( '<STR_LIT>' ) <EOL> config [ '<STR_LIT>' ] = factory <EOL> result = self . configure_custom ( config ) <EOL> else : <EOL> fmt = config . get ( '<STR_LIT>' , None ) <EOL> dfmt = config . get ( '<STR_LIT>' , None ) <EOL> result = logging . Formatter ( fmt , dfmt ) <EOL> return result <EOL> def configure_filter ( self , config ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in config : <EOL> result = self . configure_custom ( config ) <EOL> else : <EOL> name = config . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> result = logging . Filter ( name ) <EOL> return result <EOL> def add_filters ( self , filterer , filters ) : <EOL> """<STR_LIT>""" <EOL> for f in filters : <EOL> try : <EOL> filterer . addFilter ( self . config [ '<STR_LIT>' ] [ f ] ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' % ( f , e ) ) <EOL> def configure_handler ( self , config ) : <EOL> """<STR_LIT>""" <EOL> formatter = config . pop ( '<STR_LIT>' , None ) <EOL> if formatter : <EOL> try : <EOL> formatter = self . config [ '<STR_LIT>' ] [ formatter ] <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( formatter , e ) ) <EOL> level = config . pop ( '<STR_LIT>' , None ) <EOL> filters = config . pop ( '<STR_LIT>' , None ) <EOL> if '<STR_LIT>' in config : <EOL> c = config . pop ( '<STR_LIT>' ) <EOL> if not hasattr ( c , '<STR_LIT>' ) and hasattr ( types , '<STR_LIT>' ) and type ( c ) != types . ClassType : <EOL> c = self . resolve ( c ) <EOL> factory = c <EOL> else : <EOL> klass = self . resolve ( config . pop ( '<STR_LIT:class>' ) ) <EOL> if issubclass ( klass , logging . handlers . MemoryHandler ) and '<STR_LIT:target>' in config : <EOL> try : <EOL> config [ '<STR_LIT:target>' ] = self . config [ '<STR_LIT>' ] [ config [ '<STR_LIT:target>' ] ] <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( config [ '<STR_LIT:target>' ] , e ) ) <EOL> elif issubclass ( klass , logging . handlers . SMTPHandler ) and '<STR_LIT>' in config : <EOL> config [ '<STR_LIT>' ] = self . as_tuple ( config [ '<STR_LIT>' ] ) <EOL> elif issubclass ( klass , logging . handlers . SysLogHandler ) and '<STR_LIT:address>' in config : <EOL> config [ '<STR_LIT:address>' ] = self . as_tuple ( config [ '<STR_LIT:address>' ] ) <EOL> factory = klass <EOL> kwargs = dict ( [ ( k , config [ k ] ) for k in config if valid_ident ( k ) ] ) <EOL> try : <EOL> result = factory ( ** kwargs ) <EOL> except TypeError as te : <EOL> if "<STR_LIT>" not in str ( te ) : <EOL> raise <EOL> kwargs [ '<STR_LIT>' ] = kwargs . pop ( '<STR_LIT>' ) <EOL> result = factory ( ** kwargs ) <EOL> if formatter : <EOL> result . setFormatter ( formatter ) <EOL> if level is not None : <EOL> result . setLevel ( _checkLevel ( level ) ) <EOL> if filters : <EOL> self . add_filters ( result , filters ) <EOL> return result <EOL> def add_handlers ( self , logger , handlers ) : <EOL> """<STR_LIT>""" <EOL> for h in handlers : <EOL> try : <EOL> logger . addHandler ( self . config [ '<STR_LIT>' ] [ h ] ) <EOL> except StandardError as e : <EOL> raise ValueError ( '<STR_LIT>' % ( h , e ) ) <EOL> def common_logger_config ( self , logger , config , incremental = False ) : <EOL> """<STR_LIT>""" <EOL> level = config . get ( '<STR_LIT>' , None ) <EOL> if level is not None : <EOL> logger . setLevel ( _checkLevel ( level ) ) <EOL> if not incremental : <EOL> for h in logger . handlers [ : ] : <EOL> logger . removeHandler ( h ) <EOL> handlers = config . get ( '<STR_LIT>' , None ) <EOL> if handlers : <EOL> self . add_handlers ( logger , handlers ) <EOL> filters = config . get ( '<STR_LIT>' , None ) <EOL> if filters : <EOL> self . add_filters ( logger , filters ) <EOL> def configure_logger ( self , name , config , incremental = False ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( name ) <EOL> self . common_logger_config ( logger , config , incremental ) <EOL> propagate = config . get ( '<STR_LIT>' , None ) <EOL> if propagate is not None : <EOL> logger . propagate = propagate <EOL> def configure_root ( self , config , incremental = False ) : <EOL> """<STR_LIT>""" <EOL> root = logging . getLogger ( ) <EOL> self . common_logger_config ( root , config , incremental ) <EOL> dictConfigClass = DictConfigurator <EOL> def dictConfig ( config ) : <EOL> """<STR_LIT>""" <EOL> dictConfigClass ( config ) . configure ( ) </s>
