<s> from __future__ import unicode_literals <EOL> import os <EOL> from sprinter import lib <EOL> EMPTY = object ( ) <EOL> bool_to_str = { <EOL> '<STR_LIT:bool>' : { True : '<STR_LIT:true>' , False : '<STR_LIT:false>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT:t>' , False : '<STR_LIT:f>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT:y>' , False : '<STR_LIT:n>' } , <EOL> '<STR_LIT>' : { True : '<STR_LIT:yes>' , False : '<STR_LIT>' } <EOL> } <EOL> class InputException ( Exception ) : <EOL> pass <EOL> class Input ( object ) : <EOL> """<STR_LIT>""" <EOL> value = EMPTY <EOL> default = EMPTY <EOL> is_secret = False <EOL> prompt = None <EOL> in_type = None <EOL> out_type = None <EOL> def is_empty ( self , with_defaults = True ) : <EOL> return self . value is EMPTY and ( not with_defaults or self . default is EMPTY ) <EOL> def is_bool ( self ) : <EOL> return ( self . in_type == '<STR_LIT:bool>' or <EOL> self . in_type == '<STR_LIT>' or <EOL> self . in_type == '<STR_LIT>' or <EOL> self . in_type == '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> str_value = '<STR_LIT>' <EOL> if self . value is not EMPTY and self . value is not None : <EOL> str_value = self . value <EOL> elif self . default is not EMPTY : <EOL> str_value = self . default <EOL> if self . in_type == '<STR_LIT:file>' or self . in_type == '<STR_LIT:path>' : <EOL> return os . path . expanduser ( str_value ) <EOL> elif self . is_bool ( ) : <EOL> bool_value = None <EOL> if self . in_type == '<STR_LIT:bool>' or self . in_type == '<STR_LIT>' : <EOL> bool_value = lib . is_affirmative ( str_value ) <EOL> elif self . in_type == '<STR_LIT>' : <EOL> bool_value = str_value . lower ( ) == '<STR_LIT:yes>' <EOL> out_type = self . in_type if self . out_type is None else self . out_type <EOL> return bool_to_str [ out_type ] [ bool_value ] <EOL> else : <EOL> return str_value <EOL> def __eq__ ( self , other ) : <EOL> for val in ( '<STR_LIT:value>' , '<STR_LIT:default>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> a = getattr ( self , val , None ) <EOL> b = getattr ( other , val , None ) <EOL> if not ( a == b or a is b ) : <EOL> return False <EOL> return True <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> __repr__ = __str__ <EOL> class Inputs ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _inputs = { } <EOL> def add_input ( self , key , input_instance = None ) : <EOL> """<STR_LIT>""" <EOL> self . _inputs [ key ] = input_instance or Input ( ) <EOL> def is_input ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return key in self . _inputs <EOL> def is_set ( self , key ) : <EOL> if key not in self . _inputs : <EOL> raise InputException ( "<STR_LIT>" . format ( key ) ) <EOL> return self . _inputs [ key ] . value is not EMPTY <EOL> def set_input ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> if key not in self . _inputs : <EOL> raise InputException ( "<STR_LIT>" . format ( key ) ) <EOL> self . _inputs [ key ] . value = value <EOL> def get_input ( self , key , force = False ) : <EOL> """<STR_LIT>""" <EOL> if key not in self . _inputs : <EOL> raise InputException ( "<STR_LIT>" . format ( key ) ) <EOL> if self . _inputs [ key ] . prompt : <EOL> prompt = self . _inputs [ key ] . prompt <EOL> elif self . _inputs [ key ] . is_bool ( ) : <EOL> prompt = "<STR_LIT>" . format ( key ) <EOL> else : <EOL> prompt = "<STR_LIT>" . format ( key ) <EOL> help_text = self . _inputs [ key ] . help if hasattr ( self . _inputs [ key ] , '<STR_LIT>' ) else None <EOL> if self . _inputs [ key ] . value is EMPTY or force : <EOL> default_value = None <EOL> if self . _inputs [ key ] . default is not EMPTY : <EOL> default_value = self . _inputs [ key ] . default <EOL> if self . _inputs [ key ] . value is not EMPTY : <EOL> default_value = self . _inputs [ key ] . value <EOL> input_value = EMPTY <EOL> while input_value is EMPTY or input_value == '<STR_LIT:?>' : <EOL> if input_value == '<STR_LIT:?>' and help_text : <EOL> print ( help_text ) <EOL> input_value = lib . prompt ( <EOL> prompt , <EOL> default = default_value , <EOL> bool_type = self . _inputs [ key ] . in_type , <EOL> secret = self . _inputs [ key ] . is_secret ) <EOL> self . _inputs [ key ] . value = input_value <EOL> return self . _inputs [ key ] . value <EOL> def get_unset_inputs ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( [ k for k , v in self . _inputs . items ( ) if v . is_empty ( False ) ] ) <EOL> def prompt_unset_inputs ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> for k , v in self . _inputs . items ( ) : <EOL> if force or v . is_empty ( False ) : <EOL> self . get_input ( k , force = force ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _inputs . keys ( ) <EOL> def values ( self , with_defaults = True ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( ( k , str ( v ) ) for k , v in self . _inputs . items ( ) if not v . is_empty ( with_defaults ) ) ) <EOL> def write_values ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( ( k , v . value ) for k , v in self . _inputs . items ( ) if not v . is_secret and not v . is_empty ( False ) ) ) <EOL> def add_inputs_from_inputstring ( self , input_string ) : <EOL> """<STR_LIT>""" <EOL> raw_params = input_string . split ( '<STR_LIT:\n>' ) <EOL> param_attributes = ( self . _parse_param_line ( rp ) for rp in raw_params if len ( rp . strip ( '<STR_LIT>' ) ) > <NUM_LIT:0> ) <EOL> for param , attributes in param_attributes : <EOL> self . add_input ( param , attributes ) <EOL> def _parse_param_line ( self , line ) : <EOL> """<STR_LIT>""" <EOL> value = line . strip ( '<STR_LIT>' ) <EOL> if len ( value ) > <NUM_LIT:0> : <EOL> i = Input ( ) <EOL> if value . find ( '<STR_LIT:#>' ) != - <NUM_LIT:1> : <EOL> value , extra_attributes = value . split ( '<STR_LIT:#>' ) <EOL> try : <EOL> extra_attributes = eval ( extra_attributes ) <EOL> except SyntaxError : <EOL> raise InputException ( "<STR_LIT>" . format ( value ) ) <EOL> if not isinstance ( extra_attributes , dict ) : <EOL> raise InputException ( "<STR_LIT>" . format ( value ) ) <EOL> if '<STR_LIT>' in extra_attributes : <EOL> i . prompt = extra_attributes [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in extra_attributes : <EOL> i . help = extra_attributes [ '<STR_LIT>' ] <EOL> if '<STR_LIT:type>' in extra_attributes : <EOL> i . in_type = extra_attributes [ '<STR_LIT:type>' ] <EOL> if i . in_type . find ( '<STR_LIT:/>' ) != - <NUM_LIT:1> : <EOL> i . in_type , i . out_type = i . in_type . split ( '<STR_LIT:/>' ) <EOL> if '<STR_LIT>' in extra_attributes : <EOL> i . out_type = extra_attributes [ '<STR_LIT>' ] <EOL> if value . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> value , default = value . split ( '<STR_LIT>' ) <EOL> i . default = default <EOL> if value . endswith ( '<STR_LIT:?>' ) : <EOL> value = value [ : - <NUM_LIT:1> ] <EOL> i . is_secret = True <EOL> return ( value , i ) <EOL> return None </s>
