<s> import sys , os <EOL> import re <EOL> import codecs <EOL> import StringIO <EOL> import locale <EOL> import copy <EOL> import tokenize <EOL> import token <EOL> from sorteddict import SortedDict <EOL> from traceback import print_exc <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> set <EOL> except : <EOL> from sets import Set as set <EOL> try : <EOL> defaultencoding = locale . getdefaultlocale ( ) [ <NUM_LIT:1> ] <EOL> except : <EOL> defaultencoding = None <EOL> if not defaultencoding : <EOL> defaultencoding = '<STR_LIT>' <EOL> try : <EOL> codecs . lookup ( defaultencoding ) <EOL> except : <EOL> defaultencoding = '<STR_LIT>' <EOL> r_encoding = re . compile ( r'<STR_LIT>' ) <EOL> r_var = re . compile ( ur '<STR_LIT>' , re . U ) <EOL> r_var_env = re . compile ( ur '<STR_LIT>' , re . U ) <EOL> r_pre_var = re . compile ( ur '<STR_LIT>' , re . U ) <EOL> __default_env__ = { } <EOL> def set_env ( env = None ) : <EOL> global __default_env__ <EOL> __default_env__ . update ( env or { } ) <EOL> def merge_data ( values , prev = None ) : <EOL> if prev : <EOL> _type = type ( prev ) <EOL> for value in values : <EOL> if not isinstance ( value , ( list , dict , set ) ) : <EOL> return values [ - <NUM_LIT:1> ] <EOL> if prev and _type != type ( value ) : <EOL> raise ValueError ( "<STR_LIT>" % ( value , _type ) ) <EOL> if not prev : <EOL> prev = value <EOL> _type = type ( prev ) <EOL> continue <EOL> if isinstance ( value , list ) : <EOL> for x in value : <EOL> if x not in prev : <EOL> prev . append ( x ) <EOL> elif isinstance ( value , dict ) : <EOL> for k , v in value . items ( ) : <EOL> if k not in prev : <EOL> prev [ k ] = v <EOL> else : <EOL> prev [ k ] = merge_data ( [ v ] , prev [ k ] ) <EOL> else : <EOL> prev . update ( value ) <EOL> return prev <EOL> def uni_prt ( a , encoding = '<STR_LIT:utf-8>' , beautiful = False , indent = <NUM_LIT:0> , convertors = None ) : <EOL> convertors = convertors or { } <EOL> escapechars = [ ( "<STR_LIT:\\>" , "<STR_LIT>" ) , ( "<STR_LIT:'>" , r"<STR_LIT>" ) , ( '<STR_LIT>' , r'<STR_LIT>' ) , ( '<STR_LIT>' , r'<STR_LIT>' ) , <EOL> ( '<STR_LIT:\t>' , r"<STR_LIT:\t>" ) , ( '<STR_LIT:\r>' , r"<STR_LIT:\r>" ) , ( '<STR_LIT:\n>' , r"<STR_LIT:\n>" ) ] <EOL> s = [ ] <EOL> indent_char = '<STR_LIT:U+0020>' * <NUM_LIT:4> <EOL> if isinstance ( a , ( list , tuple ) ) : <EOL> if isinstance ( a , list ) : <EOL> s . append ( indent_char * indent + '<STR_LIT:[>' ) <EOL> else : <EOL> s . append ( indent_char * indent + '<STR_LIT:(>' ) <EOL> if beautiful : <EOL> s . append ( '<STR_LIT:\n>' ) <EOL> for i , k in enumerate ( a ) : <EOL> if beautiful : <EOL> ind = indent + <NUM_LIT:1> <EOL> else : <EOL> ind = indent <EOL> s . append ( indent_char * ind + uni_prt ( k , encoding , beautiful , indent + <NUM_LIT:1> , convertors = convertors ) ) <EOL> if i < len ( a ) - <NUM_LIT:1> : <EOL> if beautiful : <EOL> s . append ( '<STR_LIT>' ) <EOL> else : <EOL> s . append ( '<STR_LIT:U+002CU+0020>' ) <EOL> if beautiful : <EOL> s . append ( '<STR_LIT:\n>' ) <EOL> if isinstance ( a , list ) : <EOL> s . append ( indent_char * indent + '<STR_LIT:]>' ) <EOL> else : <EOL> if len ( a ) == <NUM_LIT:1> : <EOL> s . append ( '<STR_LIT:U+002C>' ) <EOL> s . append ( indent_char * indent + '<STR_LIT:)>' ) <EOL> elif isinstance ( a , dict ) : <EOL> s . append ( indent_char * indent + '<STR_LIT:{>' ) <EOL> if beautiful : <EOL> s . append ( '<STR_LIT:\n>' ) <EOL> for i , k in enumerate ( a . items ( ) ) : <EOL> key , value = k <EOL> if beautiful : <EOL> ind = indent + <NUM_LIT:1> <EOL> else : <EOL> ind = indent <EOL> s . append ( '<STR_LIT>' % ( indent_char * ind + uni_prt ( key , encoding , beautiful , indent + <NUM_LIT:1> , convertors = convertors ) , uni_prt ( value , encoding , beautiful , indent + <NUM_LIT:1> , convertors = convertors ) ) ) <EOL> if i < len ( a . items ( ) ) - <NUM_LIT:1> : <EOL> if beautiful : <EOL> s . append ( '<STR_LIT>' ) <EOL> else : <EOL> s . append ( '<STR_LIT:U+002CU+0020>' ) <EOL> if beautiful : <EOL> s . append ( '<STR_LIT:\n>' ) <EOL> s . append ( indent_char * indent + '<STR_LIT:}>' ) <EOL> elif isinstance ( a , str ) : <EOL> t = a <EOL> for i in escapechars : <EOL> t = t . replace ( i [ <NUM_LIT:0> ] , i [ <NUM_LIT:1> ] ) <EOL> s . append ( "<STR_LIT>" % t ) <EOL> elif isinstance ( a , unicode ) : <EOL> t = a <EOL> for i in escapechars : <EOL> t = t . replace ( i [ <NUM_LIT:0> ] , i [ <NUM_LIT:1> ] ) <EOL> try : <EOL> s . append ( "<STR_LIT>" % t . encode ( encoding ) ) <EOL> except : <EOL> print_exc ( ) <EOL> else : <EOL> _type = type ( a ) <EOL> c_func = convertors . get ( _type ) <EOL> if c_func : <EOL> s . append ( c_func ( a ) ) <EOL> else : <EOL> s . append ( str ( a ) ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> def eval_value ( value , globals , locals , encoding , include_env ) : <EOL> def sub_ ( m ) : <EOL> txt = filter ( None , m . groups ( ) ) [ <NUM_LIT:0> ] . strip ( ) <EOL> try : <EOL> v = eval_value ( str ( txt ) , globals , locals , encoding , include_env ) <EOL> _type = type ( txt ) <EOL> if not isinstance ( v , ( str , unicode ) ) : <EOL> v = _type ( v ) <EOL> elif not isinstance ( v , _type ) : <EOL> if _type is unicode : <EOL> v = unicode ( v , encoding ) <EOL> else : <EOL> v = v . encode ( encoding ) <EOL> except : <EOL> print_exc ( ) <EOL> v = m . group ( ) <EOL> return v <EOL> if isinstance ( value , ( str , unicode ) ) : <EOL> if include_env : <EOL> v = r_var_env . sub ( sub_ , value ) <EOL> else : <EOL> v = r_var . sub ( sub_ , value ) <EOL> else : <EOL> v = value <EOL> txt = '<STR_LIT>' % ( encoding , v ) <EOL> result = eval ( txt , dict ( globals ) , dict ( locals ) ) <EOL> return result <EOL> class Empty ( object ) : pass <EOL> class EvalValue ( object ) : <EOL> def __init__ ( self , value , filename , lineno , line ) : <EOL> self . value = value <EOL> self . filename = filename <EOL> self . lineno = lineno <EOL> self . line = line <EOL> def __str__ ( self ) : <EOL> return self . value <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> class Lazy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , globals , sec_name , encoding , include_env ) : <EOL> self . key = key <EOL> self . values = [ ] <EOL> self . globals = globals <EOL> self . sec_name = sec_name <EOL> self . encoding = encoding <EOL> self . cached_value = Empty <EOL> self . include_env = include_env <EOL> def eval ( self , value ) : <EOL> try : <EOL> _locals = self . globals [ self . sec_name ] <EOL> if not isinstance ( _locals , SortedDict ) : <EOL> _locals = { } <EOL> v = eval_value ( value , self . globals , _locals , self . encoding , self . include_env ) <EOL> return v <EOL> except Exception as e : <EOL> print_exc ( ) <EOL> raise Exception ( "<STR_LIT>" % value ) <EOL> def add ( self , value , replace = False ) : <EOL> if not replace : <EOL> self . values . append ( value ) <EOL> else : <EOL> self . values = [ value ] <EOL> def get ( self ) : <EOL> if self . cached_value is Empty : <EOL> result = [ ] <EOL> for v in self . values : <EOL> value = self . eval ( v ) <EOL> if not isinstance ( value , ( list , dict , set ) ) and len ( self . values ) > <NUM_LIT:1> : <EOL> self . cached_value = self . eval ( self . values [ - <NUM_LIT:1> ] ) <EOL> break <EOL> else : <EOL> result . append ( value ) <EOL> if result : <EOL> self . cached_value = merge_data ( result ) <EOL> self . globals [ self . sec_name ] [ self . key ] = self . cached_value <EOL> return self . cached_value <EOL> class RawValue ( object ) : <EOL> def __init__ ( self , filename , lineno , text , replace_flag = '<STR_LIT>' ) : <EOL> self . filename = filename <EOL> self . lineno = lineno <EOL> self . text = text <EOL> self . replace_flag = replace_flag <EOL> def __str__ ( self ) : <EOL> _length = <NUM_LIT> <EOL> if len ( self . filename ) > _length : <EOL> s = self . filename . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> t = - <NUM_LIT:1> <EOL> for i in range ( len ( s ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> t = len ( s [ i ] ) + t + <NUM_LIT:1> <EOL> if t > _length : <EOL> break <EOL> filename = '<STR_LIT>' + '<STR_LIT:/>' . join ( s [ i + <NUM_LIT:1> : ] ) <EOL> else : <EOL> filename = self . filename . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> return '<STR_LIT>' % ( filename , self . lineno ) <EOL> def value ( self ) : <EOL> if self . replace_flag : <EOL> op = '<STR_LIT>' <EOL> else : <EOL> op = '<STR_LIT>' <EOL> return "<STR_LIT>" % ( op , self . text ) <EOL> class Section ( SortedDict ) : <EOL> def __init__ ( self , name , comments = None , encoding = None , root = None , info = None ) : <EOL> super ( Section , self ) . __init__ ( ) <EOL> self . _root = root <EOL> self . _name = name <EOL> self . add_comment ( comments = comments ) <EOL> self . _field_comments = { } <EOL> self . _field_flag = { } <EOL> self . _encoding = encoding <EOL> self . _info = info <EOL> if self . _root and self . _lazy : <EOL> self . _root . _globals [ name ] = SortedDict ( ) <EOL> @ property <EOL> def _lazy ( self ) : <EOL> if self . _root : <EOL> return self . _root . _lazy <EOL> else : <EOL> return False <EOL> def add ( self , key , value , comments = None , replace = False ) : <EOL> self . __setitem__ ( key , value , replace ) <EOL> self . _field_flag [ key ] = replace <EOL> self . add_comment ( key , comments ) <EOL> def __setitem__ ( self , key , value , replace = False ) : <EOL> if self . _lazy : <EOL> if not key in self or replace : <EOL> v = Lazy ( key , self . _root . _globals , self . _name , self . _encoding , self . _root . _import_env ) <EOL> else : <EOL> v = self [ key ] <EOL> v . add ( value ) <EOL> else : <EOL> v = value <EOL> if not replace : <EOL> v = merge_data ( [ value ] , self . get ( key ) ) <EOL> super ( Section , self ) . __setitem__ ( key , v , append = replace ) <EOL> def add_comment ( self , key = None , comments = None ) : <EOL> comments = comments or [ ] <EOL> if not isinstance ( comments , ( tuple , list ) ) : <EOL> comments = [ comments ] <EOL> if not key : <EOL> self . _comments = comments <EOL> else : <EOL> self . _field_comments [ key ] = copy . copy ( comments ) <EOL> def comment ( self , key = None ) : <EOL> if not key : <EOL> return self . _comments <EOL> else : <EOL> return self . _field_comments . get ( key , [ ] ) <EOL> def del_comment ( self , key = None ) : <EOL> self . add_comment ( key , None ) <EOL> def dumps ( self , out , convertors = None ) : <EOL> if self . _comments : <EOL> print >> out , '<STR_LIT:\n>' . join ( self . _comments ) <EOL> if self . _root and self . _root . _raw : <EOL> print >> out , '<STR_LIT>' % ( self . _info , self . _name ) <EOL> else : <EOL> print >> out , '<STR_LIT>' % self . _name <EOL> for f in self . keys ( ) : <EOL> comments = self . comment ( f ) <EOL> if comments : <EOL> print >> out , '<STR_LIT:\n>' . join ( comments ) <EOL> if self . _root and self . _root . _raw : <EOL> print >> out , "<STR_LIT>" % ( str ( self [ f ] ) , f , self [ f ] . value ( ) ) <EOL> else : <EOL> if self . _field_flag . get ( f , False ) : <EOL> op = '<STR_LIT>' <EOL> else : <EOL> op = '<STR_LIT>' <EOL> buf = f + op + uni_prt ( self [ f ] , self . _encoding , convertors = convertors ) <EOL> if len ( buf ) > <NUM_LIT> : <EOL> buf = f + op + uni_prt ( self [ f ] , self . _encoding , True , convertors = convertors ) <EOL> print >> out , buf <EOL> def __delitem__ ( self , key ) : <EOL> super ( Section , self ) . __delitem__ ( key ) <EOL> self . del_comment ( key ) <EOL> def __delattr__ ( self , key ) : <EOL> try : <EOL> del self [ key ] <EOL> except KeyError as k : <EOL> raise AttributeError ( k ) <EOL> def __str__ ( self ) : <EOL> buf = StringIO . StringIO ( ) <EOL> self . dumps ( buf ) <EOL> return buf . getvalue ( ) <EOL> class Ini ( SortedDict ) : <EOL> def __init__ ( self , inifile = '<STR_LIT>' , commentchar = None , encoding = None , <EOL> env = None , convertors = None , lazy = False , writable = False , raw = False , <EOL> import_env = True , basepath = '<STR_LIT:.>' , pre_variables = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Ini , self ) . __init__ ( ) <EOL> if isinstance ( inifile , dict ) : <EOL> self . _inifile = '<STR_LIT>' <EOL> data = inifile <EOL> else : <EOL> self . _inifile = inifile <EOL> data = None <EOL> self . _basepath = basepath <EOL> self . _commentchar = commentchar or __default_env__ . get ( '<STR_LIT>' , '<STR_LIT:#>' ) <EOL> self . _encoding = encoding or __default_env__ . get ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> self . _env = __default_env__ . get ( '<STR_LIT>' , { } ) . copy ( ) <EOL> self . _env . update ( env or { } ) <EOL> self . _env [ '<STR_LIT>' ] = set <EOL> self . update ( self . _env ) <EOL> self . _globals = SortedDict ( ) <EOL> self . _pre_variables = pre_variables or { } <EOL> self . _import_env = import_env <EOL> if self . _import_env : <EOL> self . _globals . update ( os . environ ) <EOL> self . _convertors = __default_env__ . get ( '<STR_LIT>' , { } ) . copy ( ) <EOL> self . _convertors . update ( convertors or { } ) <EOL> self . _lazy = lazy <EOL> self . _writable = writable <EOL> self . _raw = raw <EOL> if lazy : <EOL> self . _globals . update ( self . _env . copy ( ) ) <EOL> if self . _inifile : <EOL> self . read ( self . _inifile ) <EOL> if data : <EOL> for k , v in data . items ( ) : <EOL> s = self . add ( k ) <EOL> for _k , _v in v . items ( ) : <EOL> s [ _k ] = _v <EOL> def set_filename ( self , filename ) : <EOL> self . _inifile = filename <EOL> def get_filename ( self ) : <EOL> return self . _inifile <EOL> def set_basepath ( self , basepath ) : <EOL> self . _basepath = basepath <EOL> def set_pre_variables ( self , v ) : <EOL> self . _pre_variables = v or { } <EOL> filename = property ( get_filename , set_filename ) <EOL> def _pre_var ( self , value ) : <EOL> """<STR_LIT>""" <EOL> def sub_ ( m ) : <EOL> return self . _pre_variables . get ( m . group ( ) [ <NUM_LIT:2> : - <NUM_LIT:1> ] . strip ( ) , '<STR_LIT>' ) <EOL> return r_pre_var . sub ( sub_ , value ) <EOL> def read ( self , fobj , filename = '<STR_LIT>' ) : <EOL> encoding = None <EOL> if isinstance ( fobj , ( str , unicode ) ) : <EOL> f = open ( fobj , '<STR_LIT:rb>' ) <EOL> text = f . read ( ) <EOL> f . close ( ) <EOL> else : <EOL> text = fobj . read ( ) <EOL> text = text + '<STR_LIT:\n>' <EOL> begin = <NUM_LIT:0> <EOL> if text . startswith ( codecs . BOM_UTF8 ) : <EOL> begin = <NUM_LIT:3> <EOL> encoding = '<STR_LIT>' <EOL> elif text . startswith ( codecs . BOM_UTF16 ) : <EOL> begin = <NUM_LIT:2> <EOL> encoding = '<STR_LIT>' <EOL> if not encoding : <EOL> try : <EOL> unicode ( text , '<STR_LIT>' ) <EOL> encoding = '<STR_LIT>' <EOL> except : <EOL> encoding = defaultencoding <EOL> self . _encoding = encoding <EOL> f = StringIO . StringIO ( text ) <EOL> f . seek ( begin ) <EOL> lineno = <NUM_LIT:0> <EOL> comments = [ ] <EOL> status = '<STR_LIT:c>' <EOL> section = None <EOL> while <NUM_LIT:1> : <EOL> lastpos = f . tell ( ) <EOL> line = f . readline ( ) <EOL> lineno += <NUM_LIT:1> <EOL> if not line : <EOL> break <EOL> line = line . strip ( ) <EOL> if line : <EOL> if line . startswith ( self . _commentchar ) : <EOL> if lineno == <NUM_LIT:1> : <EOL> b = r_encoding . search ( line [ <NUM_LIT:1> : ] ) <EOL> if b : <EOL> self . _encoding = b . groups ( ) [ <NUM_LIT:0> ] <EOL> continue <EOL> comments . append ( line ) <EOL> elif line . startswith ( '<STR_LIT:[>' ) and line . endswith ( '<STR_LIT:]>' ) : <EOL> sec_name = line [ <NUM_LIT:1> : - <NUM_LIT:1> ] . strip ( ) <EOL> if sec_name . startswith ( '<STR_LIT>' ) : <EOL> _filename = sec_name [ <NUM_LIT:8> : ] . strip ( ) <EOL> _file = os . path . join ( self . _basepath , _filename ) <EOL> if os . path . exists ( _file ) : <EOL> old_encoding = self . _encoding <EOL> old_filename = self . filename <EOL> self . set_filename ( _file ) <EOL> self . read ( _file , filename = _file ) <EOL> self . set_filename ( old_filename ) <EOL> self . _encoding = old_encoding <EOL> else : <EOL> import warnings <EOL> warnings . warn ( Warning ( "<STR_LIT>" % _filename ) , stacklevel = <NUM_LIT:2> ) <EOL> continue <EOL> info = RawValue ( self . _inifile , lineno , sec_name ) <EOL> section = self . add ( sec_name , comments , info = info ) <EOL> comments = [ ] <EOL> elif '<STR_LIT:=>' in line : <EOL> if section is None : <EOL> raise Exception ( "<STR_LIT>" % self . filename ) <EOL> pos = line . find ( '<STR_LIT>' ) <EOL> if pos != - <NUM_LIT:1> : <EOL> begin , end = pos , pos + <NUM_LIT:2> <EOL> replace_flag = True <EOL> else : <EOL> pos = line . find ( '<STR_LIT:=>' ) <EOL> begin , end = pos , pos + <NUM_LIT:1> <EOL> replace_flag = False <EOL> keyname = line [ : begin ] . strip ( ) <EOL> if keyname in self . _env : <EOL> raise KeyError ( "<STR_LIT>" % keyname ) <EOL> rest = line [ end : ] . strip ( ) <EOL> if rest == '<STR_LIT>' : <EOL> value = '<STR_LIT:None>' <EOL> else : <EOL> f . seek ( lastpos + end ) <EOL> try : <EOL> value , iden_existed = self . __read_line ( f ) <EOL> value = self . _pre_var ( value ) <EOL> except Exception as e : <EOL> print_exc ( ) <EOL> raise Exception ( "<STR_LIT>" % ( filename or self . _inifile , lineno , line ) ) <EOL> if self . _lazy : <EOL> if iden_existed : <EOL> v = EvalValue ( value , filename or self . _inifile , lineno , line ) <EOL> else : <EOL> v = value <EOL> else : <EOL> if self . _raw : <EOL> v = RawValue ( self . _inifile , lineno , value , replace_flag ) <EOL> else : <EOL> try : <EOL> v = eval_value ( value , self . env ( ) , self [ sec_name ] , self . _encoding , self . _import_env ) <EOL> except Exception as e : <EOL> print_exc ( ) <EOL> raise Exception ( "<STR_LIT>" % ( value , filename or self . _inifile , lineno , line ) ) <EOL> section . add ( keyname , v , comments , replace = replace_flag ) <EOL> comments = [ ] <EOL> else : <EOL> comments . append ( line ) <EOL> def save ( self , filename = None ) : <EOL> if not filename : <EOL> filename = self . filename <EOL> if not filename : <EOL> filename = sys . stdout <EOL> if isinstance ( filename , ( str , unicode ) ) : <EOL> f = open ( filename , '<STR_LIT:wb>' ) <EOL> need_close = True <EOL> else : <EOL> f = filename <EOL> need_close = False <EOL> print >> f , '<STR_LIT>' % self . _encoding <EOL> for s in self . keys ( ) : <EOL> if s in self . _env : <EOL> continue <EOL> section = self [ s ] <EOL> section . dumps ( f , convertors = self . _convertors ) <EOL> if need_close : <EOL> f . close ( ) <EOL> def __read_line ( self , f ) : <EOL> """<STR_LIT>""" <EOL> g = tokenize . generate_tokens ( f . readline ) <EOL> buf = [ ] <EOL> time = <NUM_LIT:0> <EOL> iden_existed = False <EOL> while <NUM_LIT:1> : <EOL> v = g . next ( ) <EOL> tokentype , t , start , end , line = v <EOL> if tokentype == <NUM_LIT> : <EOL> continue <EOL> if tokentype in ( token . INDENT , token . DEDENT , tokenize . COMMENT ) : <EOL> continue <EOL> if tokentype == token . NAME : <EOL> iden_existed = True <EOL> if tokentype == token . NEWLINE : <EOL> return '<STR_LIT>' . join ( buf ) , iden_existed <EOL> else : <EOL> if t == '<STR_LIT:=>' and time == <NUM_LIT:0> : <EOL> time += <NUM_LIT:1> <EOL> continue <EOL> buf . append ( t ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if key not in self : <EOL> super ( Ini , self ) . __setitem__ ( key , value ) <EOL> def update ( self , value ) : <EOL> for k , v in value . items ( ) : <EOL> self . set_var ( k , v ) <EOL> def add ( self , sec_name , comments = None , info = None ) : <EOL> if sec_name in self : <EOL> section = self [ sec_name ] <EOL> else : <EOL> section = Section ( sec_name , comments , self . _encoding , root = self , info = info ) <EOL> self [ sec_name ] = section <EOL> return section <EOL> def __str__ ( self ) : <EOL> buf = StringIO . StringIO ( ) <EOL> self . save ( buf ) <EOL> return buf . getvalue ( ) <EOL> def get_var ( self , key , default = None ) : <EOL> obj = self <EOL> for i in key . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) : <EOL> obj = obj . get ( i ) <EOL> if obj is None : <EOL> break <EOL> if obj is None : <EOL> return default <EOL> return obj <EOL> def set_var ( self , key , value ) : <EOL> s = key . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> obj = self <EOL> for i in s [ : - <NUM_LIT:1> ] : <EOL> obj = obj . add ( i ) <EOL> obj [ s [ - <NUM_LIT:1> ] ] = value <EOL> return True <EOL> def del_var ( self , key ) : <EOL> s = key . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> obj = self <EOL> for i in s [ : - <NUM_LIT:1> ] : <EOL> obj = obj . get ( i ) <EOL> if obj is None : <EOL> return False <EOL> if s [ - <NUM_LIT:1> ] in obj : <EOL> del obj [ s [ - <NUM_LIT:1> ] ] <EOL> flag = True <EOL> else : <EOL> flag = False <EOL> return flag <EOL> def items ( self ) : <EOL> return ( ( k , self [ k ] ) for k in self . keys ( ) if not k in self . _env ) <EOL> def env ( self ) : <EOL> if self . _import_env : <EOL> d = { } <EOL> d . update ( os . environ . copy ( ) ) <EOL> d . update ( dict ( self ) ) <EOL> return d <EOL> return self <EOL> def freeze ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _lazy = False <EOL> for k , v in self . items ( ) : <EOL> if k in self . _env : <EOL> continue <EOL> for _k , _v in v . items ( ) : <EOL> if isinstance ( _v , Lazy ) : <EOL> if self . writable : <EOL> _v . get ( ) <EOL> else : <EOL> try : <EOL> v . __setitem__ ( _k , _v . get ( ) , replace = True ) <EOL> except : <EOL> print "<STR_LIT>" , _k <EOL> raise <EOL> del _v <EOL> self . _globals = SortedDict ( ) </s>
