<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import os . path <EOL> import string <EOL> import sys <EOL> import SCons . Environment <EOL> import SCons . Errors <EOL> import SCons . Util <EOL> import SCons . Warnings <EOL> from BoolVariable import BoolVariable <EOL> from EnumVariable import EnumVariable <EOL> from ListVariable import ListVariable <EOL> from PackageVariable import PackageVariable <EOL> from PathVariable import PathVariable <EOL> class Variables : <EOL> instance = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , files = [ ] , args = { } , is_global = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . options = [ ] <EOL> self . args = args <EOL> if not SCons . Util . is_List ( files ) : <EOL> if files : <EOL> files = [ files ] <EOL> else : <EOL> files = [ ] <EOL> self . files = files <EOL> self . unknown = { } <EOL> if is_global : <EOL> self = Variables . instance <EOL> if not Variables . instance : <EOL> Variables . instance = self <EOL> def _do_add ( self , key , help = "<STR_LIT>" , default = None , validator = None , converter = None ) : <EOL> class Variable : <EOL> pass <EOL> option = Variable ( ) <EOL> if SCons . Util . is_List ( key ) or SCons . Util . is_Tuple ( key ) : <EOL> option . key = key [ <NUM_LIT:0> ] <EOL> option . aliases = key [ <NUM_LIT:1> : ] <EOL> else : <EOL> option . key = key <EOL> option . aliases = [ key ] <EOL> option . help = help <EOL> option . default = default <EOL> option . validator = validator <EOL> option . converter = converter <EOL> self . options . append ( option ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return map ( lambda o : o . key , self . options ) <EOL> def Add ( self , key , help = "<STR_LIT>" , default = None , validator = None , converter = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if SCons . Util . is_List ( key ) or type ( key ) == type ( ( ) ) : <EOL> apply ( self . _do_add , key ) <EOL> return <EOL> if not SCons . Util . is_String ( key ) or not SCons . Environment . is_valid_construction_var ( key ) : <EOL> raise SCons . Errors . UserError , "<STR_LIT>" % str ( key ) <EOL> self . _do_add ( key , help , default , validator , converter ) <EOL> def AddVariables ( self , * optlist ) : <EOL> """<STR_LIT>""" <EOL> for o in optlist : <EOL> apply ( self . _do_add , o ) <EOL> def Update ( self , env , args = None ) : <EOL> """<STR_LIT>""" <EOL> values = { } <EOL> for option in self . options : <EOL> if not option . default is None : <EOL> values [ option . key ] = option . default <EOL> for filename in self . files : <EOL> if os . path . exists ( filename ) : <EOL> dir = os . path . split ( os . path . abspath ( filename ) ) [ <NUM_LIT:0> ] <EOL> if dir : <EOL> sys . path . insert ( <NUM_LIT:0> , dir ) <EOL> try : <EOL> values [ '<STR_LIT>' ] = filename <EOL> execfile ( filename , { } , values ) <EOL> finally : <EOL> if dir : <EOL> del sys . path [ <NUM_LIT:0> ] <EOL> del values [ '<STR_LIT>' ] <EOL> if args is None : <EOL> args = self . args <EOL> for arg , value in args . items ( ) : <EOL> added = False <EOL> for option in self . options : <EOL> if arg in option . aliases + [ option . key ] : <EOL> values [ option . key ] = value <EOL> added = True <EOL> if not added : <EOL> self . unknown [ arg ] = value <EOL> for option in self . options : <EOL> try : <EOL> env [ option . key ] = values [ option . key ] <EOL> except KeyError : <EOL> pass <EOL> for option in self . options : <EOL> if option . converter and values . has_key ( option . key ) : <EOL> value = env . subst ( '<STR_LIT>' % option . key ) <EOL> try : <EOL> try : <EOL> env [ option . key ] = option . converter ( value ) <EOL> except TypeError : <EOL> env [ option . key ] = option . converter ( value , env ) <EOL> except ValueError , x : <EOL> raise SCons . Errors . UserError , '<STR_LIT>' % ( option . key , x ) <EOL> for option in self . options : <EOL> if option . validator and values . has_key ( option . key ) : <EOL> option . validator ( option . key , env . subst ( '<STR_LIT>' % option . key ) , env ) <EOL> def UnknownVariables ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . unknown <EOL> def Save ( self , filename , env ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fh = open ( filename , '<STR_LIT:w>' ) <EOL> try : <EOL> for option in self . options : <EOL> try : <EOL> value = env [ option . key ] <EOL> try : <EOL> prepare = value . prepare_to_store <EOL> except AttributeError : <EOL> try : <EOL> eval ( repr ( value ) ) <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except : <EOL> value = SCons . Util . to_String ( value ) <EOL> else : <EOL> value = prepare ( ) <EOL> defaultVal = env . subst ( SCons . Util . to_String ( option . default ) ) <EOL> if option . converter : <EOL> defaultVal = option . converter ( defaultVal ) <EOL> if str ( env . subst ( '<STR_LIT>' % option . key ) ) != str ( defaultVal ) : <EOL> fh . write ( '<STR_LIT>' % ( option . key , repr ( value ) ) ) <EOL> except KeyError : <EOL> pass <EOL> finally : <EOL> fh . close ( ) <EOL> except IOError , x : <EOL> raise SCons . Errors . UserError , '<STR_LIT>' % ( filename , x ) <EOL> def GenerateHelpText ( self , env , sort = None ) : <EOL> """<STR_LIT>""" <EOL> if sort : <EOL> options = self . options [ : ] <EOL> options . sort ( lambda x , y , func = sort : func ( x . key , y . key ) ) <EOL> else : <EOL> options = self . options <EOL> def format ( opt , self = self , env = env ) : <EOL> if env . has_key ( opt . key ) : <EOL> actual = env . subst ( '<STR_LIT>' % opt . key ) <EOL> else : <EOL> actual = None <EOL> return self . FormatVariableHelpText ( env , opt . key , opt . help , opt . default , actual , opt . aliases ) <EOL> lines = filter ( None , map ( format , options ) ) <EOL> return string . join ( lines , '<STR_LIT>' ) <EOL> format = '<STR_LIT>' <EOL> format_ = '<STR_LIT>' <EOL> def FormatVariableHelpText ( self , env , key , help , default , actual , aliases = [ ] ) : <EOL> aliases = filter ( lambda a , k = key : a != k , aliases ) <EOL> if len ( aliases ) == <NUM_LIT:0> : <EOL> return self . format % ( key , help , default , actual ) <EOL> else : <EOL> return self . format_ % ( key , help , default , actual , aliases ) </s>
