<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import re <EOL> import six <EOL> from six . moves import ( <EOL> configparser , <EOL> filter , <EOL> reload_module , <EOL> ) <EOL> from staticconf import config , errors <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> log = logging . getLogger ( __name__ ) <EOL> def flatten_dict ( config_data ) : <EOL> for key , value in six . iteritems ( config_data ) : <EOL> if hasattr ( value , '<STR_LIT>' ) or hasattr ( value , '<STR_LIT>' ) : <EOL> for k , v in flatten_dict ( value ) : <EOL> yield '<STR_LIT>' % ( key , k ) , v <EOL> continue <EOL> yield key , value <EOL> def load_config_data ( loader_func , * args , ** kwargs ) : <EOL> optional = kwargs . pop ( '<STR_LIT>' , False ) <EOL> try : <EOL> return loader_func ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> log . info ( "<STR_LIT>" % e ) <EOL> if not optional : <EOL> raise <EOL> return { } <EOL> def build_loader ( loader_func ) : <EOL> def loader ( * args , ** kwargs ) : <EOL> err_on_unknown = kwargs . pop ( '<STR_LIT>' , False ) <EOL> err_on_dupe = kwargs . pop ( '<STR_LIT>' , False ) <EOL> flatten = kwargs . pop ( '<STR_LIT>' , True ) <EOL> name = kwargs . pop ( '<STR_LIT>' , config . DEFAULT ) <EOL> config_data = load_config_data ( loader_func , * args , ** kwargs ) <EOL> if flatten : <EOL> config_data = dict ( flatten_dict ( config_data ) ) <EOL> namespace = config . get_namespace ( name ) <EOL> namespace . apply_config_data ( config_data , err_on_unknown , err_on_dupe ) <EOL> return config_data <EOL> return loader <EOL> def yaml_loader ( filename ) : <EOL> import yaml <EOL> try : <EOL> from yaml import CLoader as Loader <EOL> except ImportError : <EOL> from yaml import Loader <EOL> with open ( filename ) as fh : <EOL> return yaml . load ( fh , Loader = Loader ) or { } <EOL> def json_loader ( filename ) : <EOL> try : <EOL> import simplejson as json <EOL> assert json <EOL> except ImportError : <EOL> import json <EOL> with open ( filename ) as fh : <EOL> return json . load ( fh ) <EOL> def list_loader ( seq ) : <EOL> return dict ( pair . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) for pair in seq ) <EOL> def auto_loader ( base_dir = '<STR_LIT:.>' , auto_configurations = None ) : <EOL> auto_configurations = auto_configurations or [ <EOL> ( yaml_loader , '<STR_LIT>' ) , <EOL> ( json_loader , '<STR_LIT>' ) , <EOL> ( ini_file_loader , '<STR_LIT>' ) , <EOL> ( xml_loader , '<STR_LIT>' ) , <EOL> ( properties_loader , '<STR_LIT>' ) <EOL> ] <EOL> for config_loader , config_arg in auto_configurations : <EOL> path = os . path . join ( base_dir , config_arg ) <EOL> if os . path . isfile ( path ) : <EOL> return config_loader ( path ) <EOL> msg = "<STR_LIT>" <EOL> raise errors . ConfigurationError ( msg ) <EOL> def python_loader ( module_name ) : <EOL> module = __import__ ( module_name , fromlist = [ '<STR_LIT:*>' ] ) <EOL> reload_module ( module ) <EOL> return object_loader ( module ) <EOL> def object_loader ( obj ) : <EOL> return dict ( ( name , getattr ( obj , name ) ) <EOL> for name in dir ( obj ) if not name . startswith ( '<STR_LIT:_>' ) ) <EOL> def ini_file_loader ( filename ) : <EOL> parser = configparser . SafeConfigParser ( ) <EOL> parser . read ( [ filename ] ) <EOL> config_dict = { } <EOL> for section in parser . sections ( ) : <EOL> for key , value in parser . items ( section , True ) : <EOL> config_dict [ '<STR_LIT>' % ( section , key ) ] = value <EOL> return config_dict <EOL> def xml_loader ( filename , safe = False ) : <EOL> from xml . etree import ElementTree <EOL> def build_from_element ( element ) : <EOL> items = dict ( element . items ( ) ) <EOL> child_items = dict ( <EOL> ( child . tag , build_from_element ( child ) ) <EOL> for child in element ) <EOL> config . has_duplicate_keys ( child_items , items , safe ) <EOL> items . update ( child_items ) <EOL> if element . text : <EOL> if '<STR_LIT:value>' in items and safe : <EOL> msg = "<STR_LIT>" <EOL> raise errors . ConfigurationError ( msg % filename ) <EOL> items [ '<STR_LIT:value>' ] = element . text <EOL> return items <EOL> tree = ElementTree . parse ( filename ) <EOL> return build_from_element ( tree . getroot ( ) ) <EOL> def properties_loader ( filename ) : <EOL> split_pattern = re . compile ( r'<STR_LIT>' ) <EOL> def parse_line ( line ) : <EOL> line = line . strip ( ) <EOL> if not line or line . startswith ( '<STR_LIT:#>' ) : <EOL> return <EOL> try : <EOL> key , value = split_pattern . split ( line , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> msg = "<STR_LIT>" % line <EOL> raise errors . ConfigurationError ( msg ) <EOL> return key . strip ( ) , value . strip ( ) <EOL> with open ( filename ) as fh : <EOL> return dict ( filter ( None , ( parse_line ( line ) for line in fh ) ) ) <EOL> class CompositeConfiguration ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , loaders = None ) : <EOL> self . loaders = loaders or [ ] <EOL> def append ( self , loader ) : <EOL> self . loaders . append ( loader ) <EOL> def load ( self ) : <EOL> output = { } <EOL> for loader_with_args in self . loaders : <EOL> output . update ( loader_with_args [ <NUM_LIT:0> ] ( * loader_with_args [ <NUM_LIT:1> : ] ) ) <EOL> return output <EOL> def __call__ ( self , * args ) : <EOL> return self . load ( ) <EOL> YamlConfiguration = build_loader ( yaml_loader ) <EOL> """<STR_LIT>""" <EOL> JSONConfiguration = build_loader ( json_loader ) <EOL> """<STR_LIT>""" <EOL> ListConfiguration = build_loader ( list_loader ) <EOL> """<STR_LIT>""" <EOL> DictConfiguration = build_loader ( lambda d : d ) <EOL> """<STR_LIT>""" <EOL> ObjectConfiguration = build_loader ( object_loader ) <EOL> """<STR_LIT>""" <EOL> AutoConfiguration = build_loader ( auto_loader ) <EOL> """<STR_LIT>""" <EOL> PythonConfiguration = build_loader ( python_loader ) <EOL> """<STR_LIT>""" <EOL> INIConfiguration = build_loader ( ini_file_loader ) <EOL> """<STR_LIT>""" <EOL> XMLConfiguration = build_loader ( xml_loader ) <EOL> """<STR_LIT>""" <EOL> PropertiesConfiguration = build_loader ( properties_loader ) <EOL> """<STR_LIT>""" </s>
