<s> """<STR_LIT>""" <EOL> from exabgp . util import string_is_hex <EOL> class Resource ( long ) : <EOL> NAME = '<STR_LIT>' <EOL> codes = { } <EOL> names = { } <EOL> cache = { } <EOL> def __new__ ( cls , * args ) : <EOL> key = '<STR_LIT>' . join ( ( str ( _ ) for _ in args ) ) <EOL> if key in Resource . cache . setdefault ( cls , { } ) : <EOL> return Resource . cache [ cls ] [ key ] <EOL> instance = long . __new__ ( cls , * args ) <EOL> Resource . cache [ cls ] [ key ] = instance <EOL> return instance <EOL> def __str__ ( self ) : <EOL> return self . names . get ( self , '<STR_LIT>' % ( self . NAME , long ( self ) ) ) <EOL> @ classmethod <EOL> def _value ( cls , string ) : <EOL> name = string . lower ( ) . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> if name in cls . codes : <EOL> return cls . codes [ name ] <EOL> if string . isdigit ( ) : <EOL> value = int ( string ) <EOL> if value in cls . names : <EOL> return value <EOL> if string_is_hex ( string ) : <EOL> value = int ( string [ <NUM_LIT:2> : ] , <NUM_LIT:16> ) <EOL> if value in cls . names : <EOL> return value <EOL> raise ValueError ( '<STR_LIT>' % ( cls . NAME , name ) ) <EOL> @ classmethod <EOL> def named ( cls , string ) : <EOL> value = <NUM_LIT:0> <EOL> for name in string . split ( '<STR_LIT:+>' ) : <EOL> value += cls . _value ( name ) <EOL> return cls ( value ) <EOL> class BitResource ( Resource ) : <EOL> def bits ( self ) : <EOL> value = int ( self ) <EOL> for bit in self . names . keys ( ) : <EOL> if value & bit : <EOL> yield self . names [ bit ] <EOL> value -= bit <EOL> if value : <EOL> yield self . names . get ( self , '<STR_LIT>' % ( self . NAME , long ( self ) ) ) <EOL> def named_bits ( self ) : <EOL> for value in self . bits ( ) : <EOL> yield value <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:+>' . join ( self . bits ( ) ) </s>
