<s> import imp <EOL> import logging <EOL> import os <EOL> import sys <EOL> from optparse import OptionParser , make_option <EOL> from django . conf import ENVIRONMENT_VARIABLE as SETTINGS_ENVIRONMENT_VARIABLE <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . management import base <EOL> from . utils import uppercase_attributes , reraise <EOL> from . values import Value , setup_value <EOL> installed = False <EOL> CONFIGURATION_ENVIRONMENT_VARIABLE = '<STR_LIT>' <EOL> CONFIGURATION_ARGUMENT = '<STR_LIT>' <EOL> CONFIGURATION_ARGUMENT_HELP = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> configuration_options = ( make_option ( CONFIGURATION_ARGUMENT , <EOL> help = CONFIGURATION_ARGUMENT_HELP ) , ) <EOL> def install ( check_options = False ) : <EOL> global installed <EOL> if not installed : <EOL> orig_create_parser = base . BaseCommand . create_parser <EOL> def create_parser ( self , prog_name , subcommand ) : <EOL> parser = orig_create_parser ( self , prog_name , subcommand ) <EOL> if isinstance ( parser , OptionParser ) : <EOL> for option in configuration_options : <EOL> parser . add_option ( option ) <EOL> else : <EOL> parser . add_argument ( CONFIGURATION_ARGUMENT , <EOL> help = CONFIGURATION_ARGUMENT_HELP ) <EOL> return parser <EOL> base . BaseCommand . create_parser = create_parser <EOL> importer = ConfigurationImporter ( check_options = check_options ) <EOL> sys . meta_path . insert ( <NUM_LIT:0> , importer ) <EOL> installed = True <EOL> class ConfigurationImporter ( object ) : <EOL> modvar = SETTINGS_ENVIRONMENT_VARIABLE <EOL> namevar = CONFIGURATION_ENVIRONMENT_VARIABLE <EOL> error_msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self , check_options = False ) : <EOL> self . argv = sys . argv [ : ] <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> self . logger . setLevel ( logging . DEBUG ) <EOL> handler = logging . StreamHandler ( ) <EOL> self . logger . addHandler ( handler ) <EOL> if check_options : <EOL> self . check_options ( ) <EOL> self . validate ( ) <EOL> if check_options : <EOL> self . announce ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . module , <EOL> self . name ) <EOL> @ property <EOL> def module ( self ) : <EOL> return os . environ . get ( self . modvar ) <EOL> @ property <EOL> def name ( self ) : <EOL> return os . environ . get ( self . namevar ) <EOL> def check_options ( self ) : <EOL> parser = base . CommandParser ( None , <EOL> usage = "<STR_LIT>" , <EOL> add_help = False ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( CONFIGURATION_ARGUMENT , <EOL> help = CONFIGURATION_ARGUMENT_HELP ) <EOL> parser . add_argument ( '<STR_LIT:args>' , nargs = '<STR_LIT:*>' ) <EOL> try : <EOL> options , args = parser . parse_known_args ( self . argv [ <NUM_LIT:2> : ] ) <EOL> if options . configuration : <EOL> os . environ [ self . namevar ] = options . configuration <EOL> base . handle_default_options ( options ) <EOL> except base . CommandError : <EOL> pass <EOL> def validate ( self ) : <EOL> if self . name is None : <EOL> raise ImproperlyConfigured ( self . error_msg . format ( self . namevar ) ) <EOL> if self . module is None : <EOL> raise ImproperlyConfigured ( self . error_msg . format ( self . modvar ) ) <EOL> def announce ( self ) : <EOL> if len ( self . argv ) > <NUM_LIT:1> : <EOL> from . import __version__ <EOL> from django . utils . termcolors import colorize <EOL> from django . core . management . color import no_style <EOL> if '<STR_LIT>' in self . argv : <EOL> stylize = no_style ( ) <EOL> else : <EOL> def stylize ( text ) : <EOL> return colorize ( text , fg = '<STR_LIT>' ) <EOL> if ( self . argv [ <NUM_LIT:1> ] == '<STR_LIT>' and <EOL> os . environ . get ( '<STR_LIT>' ) == '<STR_LIT:true>' ) : <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( __version__ , <EOL> self . name ) ) <EOL> self . logger . debug ( stylize ( message ) ) <EOL> def find_module ( self , fullname , path = None ) : <EOL> if fullname is not None and fullname == self . module : <EOL> module = fullname . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> return ConfigurationLoader ( self . name , <EOL> imp . find_module ( module , path ) ) <EOL> return None <EOL> class ConfigurationLoader ( object ) : <EOL> def __init__ ( self , name , location ) : <EOL> self . name = name <EOL> self . location = location <EOL> def load_module ( self , fullname ) : <EOL> if fullname in sys . modules : <EOL> mod = sys . modules [ fullname ] <EOL> else : <EOL> mod = imp . load_module ( fullname , * self . location ) <EOL> cls_path = '<STR_LIT>' . format ( mod . __name__ , self . name ) <EOL> try : <EOL> cls = getattr ( mod , self . name ) <EOL> except AttributeError as err : <EOL> reraise ( err , "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . name , <EOL> mod . __package__ ) ) <EOL> try : <EOL> cls . pre_setup ( ) <EOL> cls . setup ( ) <EOL> obj = cls ( ) <EOL> attributes = uppercase_attributes ( obj ) . items ( ) <EOL> for name , value in attributes : <EOL> if callable ( value ) and not getattr ( value , '<STR_LIT>' , False ) : <EOL> value = value ( ) <EOL> if isinstance ( value , Value ) : <EOL> setup_value ( mod , name , value ) <EOL> continue <EOL> setattr ( mod , name , value ) <EOL> setattr ( mod , '<STR_LIT>' , '<STR_LIT>' . format ( fullname , <EOL> self . name ) ) <EOL> cls . post_setup ( ) <EOL> except Exception as err : <EOL> reraise ( err , "<STR_LIT>" . format ( cls_path ) ) <EOL> return mod </s>
