<s> """<STR_LIT>""" <EOL> import re <EOL> from conary . build . errors import MacroKeyError <EOL> class Macros ( dict ) : <EOL> def __init__ ( self , macros = { } , shadow = False , ignoreUnknown = False ) : <EOL> self . __tracked = { } <EOL> self . __track = False <EOL> self . __overrides = { } <EOL> self . __callbacks = { } <EOL> self . __ignoreUnknown = ignoreUnknown <EOL> if shadow : <EOL> self . __macros = macros <EOL> else : <EOL> self . __macros = { } <EOL> self . update ( macros ) <EOL> def _get ( self , key ) : <EOL> return dict . __getitem__ ( self , key ) <EOL> def update ( self , other ) : <EOL> if isinstance ( other , dict ) : <EOL> for key , item in other . iteritems ( ) : <EOL> self [ key ] = item <EOL> else : <EOL> for key , item in other : <EOL> self [ key ] = item <EOL> def setCallback ( self , name , callback ) : <EOL> """<STR_LIT>""" <EOL> self . __callbacks [ name ] = callback <EOL> def unsetCallback ( self , name ) : <EOL> del self . __callbacks [ name ] <EOL> def __setitem__ ( self , name , value ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> dict . __setitem__ ( self , name , value ) <EOL> return <EOL> if '<STR_LIT:.>' in name : <EOL> raise MacroError , '<STR_LIT>' % name <EOL> if self . __track : <EOL> self . __tracked [ name ] = <NUM_LIT:1> <EOL> d = { name : self . get ( name ) } <EOL> value = value . replace ( '<STR_LIT:%>' , '<STR_LIT>' ) <EOL> value = value . replace ( '<STR_LIT>' % name , '<STR_LIT>' % name ) <EOL> dict . __setitem__ ( self , name , value % d ) <EOL> def _override ( self , key , value ) : <EOL> self . __overrides [ key ] = value <EOL> self [ key ] = value <EOL> def __setattr__ ( self , name , value ) : <EOL> self . __setitem__ ( name , value ) <EOL> def __getitem__ ( self , name ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> return dict . __getitem__ ( self , name ) <EOL> repmethod = None <EOL> parts = name . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> repmethod = parts [ <NUM_LIT:1> ] <EOL> name = parts [ <NUM_LIT:0> ] <EOL> if name in self . __callbacks : <EOL> self . __callbacks [ name ] ( name ) <EOL> if name in self . __overrides : <EOL> return self . __repmethod ( self . __overrides [ name ] , repmethod ) <EOL> if not name in self : <EOL> try : <EOL> value = self . __macros [ name ] <EOL> except KeyError : <EOL> if self . __ignoreUnknown : <EOL> return '<STR_LIT>' <EOL> raise MacroKeyError ( name ) <EOL> value = self . __macros [ name ] <EOL> self [ name ] = value <EOL> return self . __repmethod ( value , repmethod ) <EOL> else : <EOL> return self . __repmethod ( dict . __getitem__ ( self , name ) % self , repmethod ) <EOL> def __repmethod ( self , name , repmethod ) : <EOL> if repmethod is None : <EOL> return name <EOL> if repmethod == '<STR_LIT>' : <EOL> return re . escape ( name ) <EOL> raise MacroError , '<STR_LIT>' % ( repmethod , name ) <EOL> def __getattr__ ( self , name ) : <EOL> return self . __getitem__ ( name ) <EOL> def trackChanges ( self , flag = True ) : <EOL> self . __track = flag <EOL> def getTrackedChanges ( self ) : <EOL> return self . __tracked . keys ( ) <EOL> def copy ( self , shadow = True ) : <EOL> if not shadow : <EOL> return Macros ( [ ( x , self . _get ( x ) ) for x in dict . __iter__ ( self ) ] ) <EOL> return Macros ( self , shadow ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> return self . copy ( self , shadow = False ) <EOL> def _flatten ( self ) : <EOL> if self . __macros : <EOL> for key in self . __macros . keys ( ) : <EOL> dummy = self [ key ] <EOL> self . __macros = { } <EOL> def __iter__ ( self ) : <EOL> self . _flatten ( ) <EOL> for key in dict . __iter__ ( self ) : <EOL> if not key . startswith ( '<STR_LIT>' ) : <EOL> yield key <EOL> def iterkeys ( self ) : <EOL> for key in self . __iter__ ( ) : <EOL> yield key <EOL> def iteritems ( self ) : <EOL> for key in self . __iter__ ( ) : <EOL> yield ( key , self [ key ] ) <EOL> def itermacros ( self ) : <EOL> for key in self . __iter__ ( ) : <EOL> yield ( key , self . _get ( key ) ) <EOL> def keys ( self ) : <EOL> return [ x for x in self . __iter__ ( ) ] <EOL> class MacroError ( Exception ) : <EOL> def __init__ ( self , msg ) : <EOL> self . msg = msg <EOL> def __repr__ ( self ) : <EOL> return self . msg <EOL> def __str__ ( self ) : <EOL> return repr ( self ) </s>
