<s> from django . template . defaultfilters import slugify <EOL> class EnumMetaclass ( type ) : <EOL> """<STR_LIT>""" <EOL> ENUM_FIELDS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> EXTRA_FIELDS = { } <EOL> ENUM_VALUE_TYPES = [ int , long , basestring ] <EOL> @ classmethod <EOL> def is_enum_variable ( cls , name , val ) : <EOL> """<STR_LIT>""" <EOL> if not name . isupper ( ) : <EOL> return False <EOL> for vtype in cls . ENUM_VALUE_TYPES : <EOL> if isinstance ( val , vtype ) : <EOL> return True <EOL> return False <EOL> def __new__ ( cls , name , bases , d ) : <EOL> names = dict ( ) <EOL> values = [ ] <EOL> trans_names = dict ( ) <EOL> for x in d : <EOL> if cls . is_enum_variable ( x , d [ x ] ) : <EOL> names [ d [ x ] ] = x <EOL> values . append ( d [ x ] ) <EOL> trans_names [ d [ x ] ] = name + u"<STR_LIT:.>" + x <EOL> d [ '<STR_LIT>' ] = names <EOL> d [ '<STR_LIT>' ] = values <EOL> d [ '<STR_LIT>' ] = trans_names <EOL> dn = lambda x : ( x [ <NUM_LIT:0> ] . title ( ) + x [ <NUM_LIT:1> : ] . lower ( ) ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> display_names = dict ( [ ( n [ <NUM_LIT:0> ] , dn ( n [ <NUM_LIT:1> ] ) ) for n in d [ '<STR_LIT>' ] . items ( ) ] ) <EOL> display_names . update ( d . get ( '<STR_LIT>' , { } ) ) <EOL> d [ '<STR_LIT>' ] = display_names <EOL> d [ '<STR_LIT>' ] = map ( lambda x : ( x , d [ '<STR_LIT>' ] [ x ] ) , <EOL> sorted ( d [ '<STR_LIT>' ] ) ) <EOL> d [ '<STR_LIT>' ] = dict ( [ ( n [ <NUM_LIT:0> ] , slugify ( n [ <NUM_LIT:1> ] ) ) <EOL> for n in d [ '<STR_LIT>' ] . items ( ) ] ) <EOL> d [ '<STR_LIT>' ] = dict ( [ ( n [ <NUM_LIT:1> ] , n [ <NUM_LIT:0> ] ) for n in d [ '<STR_LIT>' ] . items ( ) ] ) <EOL> for name , fun in d . get ( '<STR_LIT>' , cls . EXTRA_FIELDS ) . iteritems ( ) : <EOL> d [ name ] = fun ( d ) <EOL> d [ '<STR_LIT>' ] = dict ( [ ( v , { '<STR_LIT:value>' : v } ) for v in values ] ) <EOL> for v in d [ '<STR_LIT>' ] : <EOL> for l in d . get ( '<STR_LIT>' , cls . ENUM_FIELDS ) : <EOL> if l in d and v in d [ l ] : <EOL> d [ '<STR_LIT>' ] [ v ] [ l [ : - <NUM_LIT:1> ] ] = d [ l ] [ v ] <EOL> return type . __new__ ( cls , name , bases , d ) </s>
