<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import math <EOL> import shlex <EOL> from collections import defaultdict <EOL> from wlauto . utils . misc import isiterable , to_identifier <EOL> def identifier ( text ) : <EOL> """<STR_LIT>""" <EOL> return to_identifier ( text ) <EOL> def boolean ( value ) : <EOL> """<STR_LIT>""" <EOL> false_strings = [ '<STR_LIT>' , '<STR_LIT:0>' , '<STR_LIT:n>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if isinstance ( value , basestring ) : <EOL> value = value . lower ( ) <EOL> if value in false_strings or '<STR_LIT:false>' . startswith ( value ) : <EOL> return False <EOL> return bool ( value ) <EOL> def integer ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , basestring ) : <EOL> return int ( value , <NUM_LIT:0> ) <EOL> else : <EOL> return int ( value ) <EOL> def numeric ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , int ) : <EOL> return value <EOL> try : <EOL> fvalue = float ( value ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' . format ( value ) ) <EOL> if not math . isnan ( fvalue ) and not math . isinf ( fvalue ) : <EOL> ivalue = int ( fvalue ) <EOL> if ivalue == fvalue : <EOL> return ivalue <EOL> return fvalue <EOL> def list_of_strs ( value ) : <EOL> """<STR_LIT>""" <EOL> if not isiterable ( value ) : <EOL> raise ValueError ( value ) <EOL> return map ( str , value ) <EOL> list_of_strings = list_of_strs <EOL> def list_of_ints ( value ) : <EOL> """<STR_LIT>""" <EOL> if not isiterable ( value ) : <EOL> raise ValueError ( value ) <EOL> return map ( int , value ) <EOL> list_of_integers = list_of_ints <EOL> def list_of_numbers ( value ) : <EOL> """<STR_LIT>""" <EOL> if not isiterable ( value ) : <EOL> raise ValueError ( value ) <EOL> return map ( numeric , value ) <EOL> def list_of_bools ( value , interpret_strings = True ) : <EOL> """<STR_LIT>""" <EOL> if not isiterable ( value ) : <EOL> raise ValueError ( value ) <EOL> if interpret_strings : <EOL> return map ( boolean , value ) <EOL> else : <EOL> return map ( bool , value ) <EOL> def list_of ( type_ ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , values ) : <EOL> list . __init__ ( self , map ( type_ , values ) ) <EOL> def append ( self , value ) : <EOL> list . append ( self , type_ ( value ) ) <EOL> def extend ( self , other ) : <EOL> list . extend ( self , map ( type_ , other ) ) <EOL> def __setitem__ ( self , idx , value ) : <EOL> list . __setitem__ ( self , idx , type_ ( value ) ) <EOL> return type ( '<STR_LIT>' . format ( type_ . __name__ ) , <EOL> ( list , ) , { <EOL> "<STR_LIT>" : __init__ , <EOL> "<STR_LIT>" : __setitem__ , <EOL> "<STR_LIT>" : append , <EOL> "<STR_LIT>" : extend , <EOL> } ) <EOL> def list_or_string ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , basestring ) : <EOL> return [ value ] <EOL> else : <EOL> try : <EOL> return list ( value ) <EOL> except ValueError : <EOL> return [ str ( value ) ] <EOL> def list_or_caseless_string ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , basestring ) : <EOL> return [ caseless_string ( value ) ] <EOL> else : <EOL> try : <EOL> return map ( caseless_string , value ) <EOL> except ValueError : <EOL> return [ caseless_string ( value ) ] <EOL> def list_or ( type_ ) : <EOL> """<STR_LIT>""" <EOL> list_type = list_of ( type_ ) <EOL> class list_or_type ( list_type ) : <EOL> def __init__ ( self , value ) : <EOL> if isiterable ( value ) : <EOL> list_type . __init__ ( self , value ) <EOL> else : <EOL> list_type . __init__ ( self , [ value ] ) <EOL> return list_or_type <EOL> list_or_integer = list_or ( integer ) <EOL> list_or_number = list_or ( numeric ) <EOL> list_or_bool = list_or ( boolean ) <EOL> regex_type = type ( re . compile ( '<STR_LIT>' ) ) <EOL> def regex ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , regex_type ) : <EOL> return value <EOL> else : <EOL> return re . compile ( value ) <EOL> __counters = defaultdict ( int ) <EOL> def reset_counter ( name = None ) : <EOL> __counters [ name ] = <NUM_LIT:0> <EOL> def counter ( name = None ) : <EOL> """<STR_LIT>""" <EOL> __counters [ name ] += <NUM_LIT:1> <EOL> value = __counters [ name ] <EOL> return value <EOL> class caseless_string ( str ) : <EOL> """<STR_LIT>""" <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , basestring ) : <EOL> other = other . lower ( ) <EOL> return self . lower ( ) == other <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __cmp__ ( self , other ) : <EOL> if isinstance ( basestring , other ) : <EOL> other = other . lower ( ) <EOL> return cmp ( self . lower ( ) , other ) <EOL> def format ( self , * args , ** kwargs ) : <EOL> return caseless_string ( super ( caseless_string , self ) . format ( * args , ** kwargs ) ) <EOL> class arguments ( list ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value = None ) : <EOL> if isiterable ( value ) : <EOL> super ( arguments , self ) . __init__ ( map ( str , value ) ) <EOL> elif isinstance ( value , basestring ) : <EOL> posix = os . name != '<STR_LIT>' <EOL> super ( arguments , self ) . __init__ ( shlex . split ( value , posix = posix ) ) <EOL> elif value is None : <EOL> super ( arguments , self ) . __init__ ( ) <EOL> else : <EOL> super ( arguments , self ) . __init__ ( [ str ( value ) ] ) <EOL> def append ( self , value ) : <EOL> return super ( arguments , self ) . append ( str ( value ) ) <EOL> def extend ( self , values ) : <EOL> return super ( arguments , self ) . extend ( map ( str , values ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:U+0020>' . join ( self ) </s>
