<s> '''<STR_LIT>''' <EOL> import ufora . util . ThreadLocalStack as ThreadLocalStack <EOL> import ufora . config . Config as Config <EOL> import logging <EOL> import argparse <EOL> import os <EOL> import re <EOL> DEFAULT_CONFIG_ENV_VARNAME = '<STR_LIT>' <EOL> class InitializationException ( Exception ) : <EOL> pass <EOL> def addDefaultArguments ( parser ) : <EOL> addCredentialsParser ( parser ) <EOL> addLoggingParser ( parser ) <EOL> addSystemResourcesParser ( parser ) <EOL> def addCredentialsParser ( parser ) : <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT:user>' , <EOL> required = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT:password>' , <EOL> required = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:-c>' , <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> required = False , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> def addLoggingParser ( parser ) : <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> required = False , <EOL> default = '<STR_LIT>' , <EOL> choices = [ '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT:error>' ] , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> dest = '<STR_LIT>' , <EOL> required = False , <EOL> default = None , <EOL> choices = [ '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT:error>' ] , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> dest = '<STR_LIT>' , <EOL> required = False , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> dest = '<STR_LIT>' , <EOL> required = False , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> def addSystemResourcesParser ( parser ) : <EOL> '''<STR_LIT>''' <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> required = False , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> required = False , <EOL> help = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , <EOL> required = False , <EOL> help = "<STR_LIT>" , <EOL> type = int <EOL> ) <EOL> def defaultParser ( minimalParser = False , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( ** kwds ) <EOL> addCredentialsParser ( parser ) <EOL> if not minimalParser : <EOL> addLoggingParser ( parser ) <EOL> addSystemResourcesParser ( parser ) <EOL> return parser <EOL> class Setup ( object ) : <EOL> def __init__ ( self , configObject ) : <EOL> self . config = configObject <EOL> def processArgs ( self , parsedArguments ) : <EOL> """<STR_LIT>""" <EOL> parsed = parsedArguments <EOL> if '<STR_LIT>' in parsed and parsed . stackLogfile : <EOL> self . config . backgroundStackTraceLoopFilename = parsed . stackLogfile <EOL> if '<STR_LIT>' in parsed and parsed . memoryLogfile : <EOL> self . config . backgroundMemoryUsageLoopFilename = parsed . memoryLogfile <EOL> if '<STR_LIT>' in parsed and parsed . logging : <EOL> self . config . setLoggingLevel ( parsed . logging , parsed . background_logging ) <EOL> if '<STR_LIT:target>' in parsed and parsed . target : <EOL> self . config . target = parsed . target <EOL> if '<STR_LIT>' in parsed and parsed . dataroot : <EOL> path = os . path . expanduser ( parsed . dataroot ) <EOL> self . config . setRootDataDir ( path ) <EOL> if '<STR_LIT>' in parsed and parsed . datarootsubdir : <EOL> path = os . path . join ( self . config . rootDataDir , parsed . datarootsubdir ) <EOL> self . config . setRootDataDir ( path ) <EOL> if '<STR_LIT>' in parsed and parsed . baseport : <EOL> self . config . setAllPorts ( parsed . baseport ) <EOL> self . parsedArguments = parsedArguments <EOL> class PushSetup ( ThreadLocalStack . ThreadLocalStackPushable ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , setup ) : <EOL> super ( PushSetup , self ) . __init__ ( ) <EOL> self . _setup = setup <EOL> def config ( ) : <EOL> tr = PushSetup . getCurrent ( ) <EOL> if tr is None : <EOL> raise InitializationException ( '<STR_LIT>' ) <EOL> return tr . _setup . config <EOL> def currentSetup ( ) : <EOL> tr = PushSetup . getCurrent ( ) <EOL> if tr is not None : <EOL> return tr . _setup <EOL> return tr <EOL> def readConfigDictFromFilename ( fileName ) : <EOL> _vardefRE = re . compile ( "<STR_LIT>" ) <EOL> configData = dict ( ) <EOL> fileText = readFileTextIfExists ( fileName ) <EOL> fileLines = fileText . split ( "<STR_LIT:\n>" ) <EOL> for l in fileLines : <EOL> m = _vardefRE . match ( l ) <EOL> if m is not None : <EOL> configData [ m . groups ( ) [ <NUM_LIT:0> ] ] = m . groups ( ) [ <NUM_LIT:1> ] . strip ( ) <EOL> return configData <EOL> def readFileTextIfExists ( fileName ) : <EOL> fileText = "<STR_LIT>" <EOL> try : <EOL> with open ( fileName , "<STR_LIT:r>" ) as f : <EOL> fileText = f . read ( ) <EOL> except IOError : <EOL> pass <EOL> return fileText <EOL> def _defaultConfigFilenameFromEnvironment ( ) : <EOL> """<STR_LIT>""" <EOL> if DEFAULT_CONFIG_ENV_VARNAME in os . environ : <EOL> return os . environ [ DEFAULT_CONFIG_ENV_VARNAME ] <EOL> else : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> def _createConfigFromFile ( filename = None ) : <EOL> '''<STR_LIT>''' <EOL> return createConfig ( filename ) <EOL> def createConfig ( filename = None , overrides = None ) : <EOL> """<STR_LIT>""" <EOL> if filename is None : <EOL> filename = _defaultConfigFilenameFromEnvironment ( ) <EOL> configDict = readConfigDictFromFilename ( filename ) <EOL> if overrides is not None : <EOL> configDict . update ( overrides ) <EOL> return Config . Config ( configDict ) <EOL> def defaultSetup ( ) : <EOL> config = _createConfigFromFile ( ) <EOL> return Setup ( config ) </s>
