<s> import pprint <EOL> from epsilon import structlike <EOL> class _unset ( object ) : <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> unset = _unset ( ) <EOL> class AttributeSet ( structlike . record ( '<STR_LIT>' , <EOL> bold = False , underline = False , reverseVideo = False , <EOL> blink = False , fg = '<STR_LIT>' , bg = '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * a , ** kw ) : <EOL> super ( AttributeSet , self ) . __init__ ( * a , ** kw ) <EOL> assert self . fg is unset or self . fg in '<STR_LIT>' <EOL> assert self . bg is unset or self . bg in '<STR_LIT>' <EOL> assert self . bold is unset or self . bold in ( True , False ) <EOL> assert self . underline is unset or self . underline in ( True , False ) <EOL> assert self . reverseVideo is unset or self . reverseVideo in ( True , False ) <EOL> assert self . blink is unset or self . blink in ( True , False ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT:=>' . join ( ( k , str ( v ) ) ) <EOL> for ( k , v ) <EOL> in zip ( self . __names__ , self ) <EOL> if v is not unset ] ) ) <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:6> <EOL> def __getitem__ ( self , index ) : <EOL> return [ self . bold , self . underline , self . reverseVideo , self . blink , self . fg , self . bg ] [ index ] <EOL> def __setitem__ ( self , index , value ) : <EOL> setattr ( self , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] [ index ] , value ) <EOL> def clone ( self ) : <EOL> return self . __class__ ( * self ) <EOL> def update ( self , other ) : <EOL> for i in range ( len ( self ) ) : <EOL> if other [ i ] is not unset : <EOL> self [ i ] = other [ i ] <EOL> return self <EOL> _flags = { '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT>' : '<STR_LIT:4>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:5>' } <EOL> def toVT102 ( self , state ) : <EOL> passive = [ ] <EOL> active = [ ] <EOL> reset = False <EOL> for attr in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> was = getattr ( state , attr ) <EOL> willBe = getattr ( self , attr ) <EOL> if was is unset : <EOL> if willBe is unset : <EOL> pass <EOL> elif willBe : <EOL> active . append ( self . _flags [ attr ] ) <EOL> else : <EOL> reset = True <EOL> elif was : <EOL> if willBe is unset : <EOL> passive . append ( self . _flags [ attr ] ) <EOL> elif willBe : <EOL> passive . append ( self . _flags [ attr ] ) <EOL> else : <EOL> reset = True <EOL> else : <EOL> if willBe is unset : <EOL> pass <EOL> elif willBe : <EOL> active . append ( self . _flags [ attr ] ) <EOL> else : <EOL> pass <EOL> for x , attr in ( '<STR_LIT:3>' , '<STR_LIT>' ) , ( '<STR_LIT:4>' , '<STR_LIT>' ) : <EOL> was = getattr ( state , attr ) <EOL> willBe = getattr ( self , attr ) <EOL> if was is unset : <EOL> if willBe is unset : <EOL> pass <EOL> elif willBe == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> active . append ( x + willBe ) <EOL> elif was == '<STR_LIT>' : <EOL> if willBe is unset : <EOL> pass <EOL> elif willBe == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> active . append ( x + willBe ) <EOL> else : <EOL> if willBe is unset : <EOL> passive . append ( x + was ) <EOL> elif willBe == '<STR_LIT>' : <EOL> reset = True <EOL> elif willBe == was : <EOL> passive . append ( x + was ) <EOL> else : <EOL> active . append ( x + willBe ) <EOL> if reset : <EOL> active . extend ( passive ) <EOL> active . insert ( <NUM_LIT:0> , '<STR_LIT:0>' ) <EOL> if active : <EOL> return '<STR_LIT>' + '<STR_LIT:;>' . join ( active ) + '<STR_LIT:m>' <EOL> return '<STR_LIT>' <EOL> class AttributeStack ( object ) : <EOL> def __init__ ( self , initialAttributes ) : <EOL> self . _stack = [ initialAttributes ] <EOL> def __repr__ ( self ) : <EOL> return pprint . pformat ( self . _stack ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _stack ) <EOL> def push ( self , attrs ) : <EOL> self . _stack . append ( self . get ( ) . clone ( ) . update ( attrs ) ) <EOL> def duptop ( self ) : <EOL> self . _stack . append ( self . get ( ) . clone ( ) ) <EOL> def update ( self , attrs ) : <EOL> self . get ( ) . update ( attrs ) <EOL> def pop ( self ) : <EOL> return self . _stack . pop ( ) <EOL> def get ( self ) : <EOL> return self . _stack [ - <NUM_LIT:1> ] <EOL> class fg : <EOL> pass <EOL> class bg : <EOL> pass <EOL> neutral = AttributeSet ( unset , unset , unset , unset , unset , unset ) <EOL> for cls , attr in [ ( fg , '<STR_LIT>' ) , <EOL> ( bg , '<STR_LIT>' ) ] : <EOL> for n , color in enumerate ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> value = neutral . clone ( ) <EOL> setattr ( value , attr , str ( n ) ) <EOL> setattr ( cls , color , value ) <EOL> cls . normal = neutral . clone ( ) <EOL> setattr ( cls . normal , attr , '<STR_LIT>' ) <EOL> del n , cls , attr , color <EOL> for attr in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' : <EOL> value = neutral . clone ( ) <EOL> setattr ( value , attr , True ) <EOL> locals ( ) [ attr ] = value <EOL> def flatten ( dag , currentAttrs = None , useColors = True ) : <EOL> """<STR_LIT>""" <EOL> if currentAttrs is None : <EOL> if not useColors : <EOL> currentAttrs = AttributeSet ( ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> attrs = AttributeStack ( currentAttrs ) <EOL> attrs . duptop ( ) <EOL> stack = [ iter ( dag ) ] <EOL> dirty = False <EOL> while stack : <EOL> try : <EOL> obj = stack [ - <NUM_LIT:1> ] . next ( ) <EOL> except StopIteration : <EOL> stack . pop ( ) <EOL> attrs . pop ( ) <EOL> if len ( attrs ) : <EOL> if useColors : <EOL> dirty = bool ( attrs . get ( ) . toVT102 ( currentAttrs ) ) <EOL> else : <EOL> if isinstance ( obj , AttributeSet ) : <EOL> attrs . update ( obj ) <EOL> if useColors : <EOL> dirty = bool ( attrs . get ( ) . toVT102 ( currentAttrs ) ) <EOL> elif isinstance ( obj , ( str , unicode ) ) : <EOL> if obj : <EOL> if dirty : <EOL> if useColors : <EOL> yield attrs . get ( ) . toVT102 ( currentAttrs ) <EOL> currentAttrs = attrs . get ( ) . clone ( ) <EOL> dirty = False <EOL> yield obj <EOL> else : <EOL> try : <EOL> newIter = iter ( obj ) <EOL> except TypeError : <EOL> if dirty : <EOL> if useColors : <EOL> yield attrs . get ( ) . toVT102 ( currentAttrs ) <EOL> currentAttrs = attrs . get ( ) . clone ( ) <EOL> dirty = False <EOL> yield obj <EOL> else : <EOL> stack . append ( newIter ) <EOL> attrs . duptop ( ) <EOL> if dirty and len ( attrs ) : <EOL> if useColors : <EOL> yield attrs . get ( ) . toVT102 ( currentAttrs ) <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] </s>
