<s> from __future__ import print_function <EOL> import getpass <EOL> import inspect <EOL> import os <EOL> import sys <EOL> import textwrap <EOL> import prettytable <EOL> import six <EOL> from six import moves <EOL> from solumclient . openstack . common . apiclient import exceptions <EOL> from solumclient . openstack . common . gettextutils import _ <EOL> from solumclient . openstack . common import strutils <EOL> from solumclient . openstack . common import uuidutils <EOL> def validate_args ( fn , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> argspec = inspect . getargspec ( fn ) <EOL> num_defaults = len ( argspec . defaults or [ ] ) <EOL> required_args = argspec . args [ : len ( argspec . args ) - num_defaults ] <EOL> def isbound ( method ) : <EOL> return getattr ( method , '<STR_LIT>' , None ) is not None <EOL> if isbound ( fn ) : <EOL> required_args . pop ( <NUM_LIT:0> ) <EOL> missing = [ arg for arg in required_args if arg not in kwargs ] <EOL> missing = missing [ len ( args ) : ] <EOL> if missing : <EOL> raise exceptions . MissingArgs ( missing ) <EOL> def arg ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _decorator ( func ) : <EOL> add_arg ( func , * args , ** kwargs ) <EOL> return func <EOL> return _decorator <EOL> def env ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for arg in args : <EOL> value = os . environ . get ( arg ) <EOL> if value : <EOL> return value <EOL> return kwargs . get ( '<STR_LIT:default>' , '<STR_LIT>' ) <EOL> def add_arg ( func , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( func , '<STR_LIT>' ) : <EOL> func . arguments = [ ] <EOL> if ( args , kwargs ) not in func . arguments : <EOL> func . arguments . insert ( <NUM_LIT:0> , ( args , kwargs ) ) <EOL> def unauthenticated ( func ) : <EOL> """<STR_LIT>""" <EOL> func . unauthenticated = True <EOL> return func <EOL> def isunauthenticated ( func ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( func , '<STR_LIT>' , False ) <EOL> def print_list ( objs , fields , formatters = None , sortby_index = <NUM_LIT:0> , <EOL> mixed_case_fields = None , field_labels = None ) : <EOL> """<STR_LIT>""" <EOL> formatters = formatters or { } <EOL> mixed_case_fields = mixed_case_fields or [ ] <EOL> field_labels = field_labels or fields <EOL> if len ( field_labels ) != len ( fields ) : <EOL> raise ValueError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : field_labels , '<STR_LIT>' : fields } ) <EOL> if sortby_index is None : <EOL> kwargs = { } <EOL> else : <EOL> kwargs = { '<STR_LIT>' : field_labels [ sortby_index ] } <EOL> pt = prettytable . PrettyTable ( field_labels , caching = False ) <EOL> pt . align = '<STR_LIT:l>' <EOL> for o in objs : <EOL> row = [ ] <EOL> for field in fields : <EOL> if field in formatters : <EOL> row . append ( formatters [ field ] ( o ) ) <EOL> else : <EOL> if field in mixed_case_fields : <EOL> field_name = field . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> else : <EOL> field_name = field . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> data = getattr ( o , field_name , '<STR_LIT>' ) <EOL> row . append ( data ) <EOL> pt . add_row ( row ) <EOL> print ( strutils . safe_encode ( pt . get_string ( ** kwargs ) ) ) <EOL> def print_dict ( dct , dict_property = "<STR_LIT>" , wrap = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> pt = prettytable . PrettyTable ( [ dict_property , '<STR_LIT>' ] , caching = False ) <EOL> pt . align = '<STR_LIT:l>' <EOL> for k , v in six . iteritems ( dct ) : <EOL> if isinstance ( v , dict ) : <EOL> v = six . text_type ( v ) <EOL> if wrap > <NUM_LIT:0> : <EOL> v = textwrap . fill ( six . text_type ( v ) , wrap ) <EOL> if v and isinstance ( v , six . string_types ) and r'<STR_LIT:\n>' in v : <EOL> lines = v . strip ( ) . split ( r'<STR_LIT:\n>' ) <EOL> col1 = k <EOL> for line in lines : <EOL> pt . add_row ( [ col1 , line ] ) <EOL> col1 = '<STR_LIT>' <EOL> else : <EOL> pt . add_row ( [ k , v ] ) <EOL> print ( strutils . safe_encode ( pt . get_string ( ) ) ) <EOL> def get_password ( max_password_prompts = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> verify = strutils . bool_from_string ( env ( "<STR_LIT>" ) ) <EOL> pw = None <EOL> if hasattr ( sys . stdin , "<STR_LIT>" ) and sys . stdin . isatty ( ) : <EOL> try : <EOL> for __ in moves . range ( max_password_prompts ) : <EOL> pw1 = getpass . getpass ( "<STR_LIT>" ) <EOL> if verify : <EOL> pw2 = getpass . getpass ( "<STR_LIT>" ) <EOL> else : <EOL> pw2 = pw1 <EOL> if pw1 == pw2 and pw1 : <EOL> pw = pw1 <EOL> break <EOL> except EOFError : <EOL> pass <EOL> return pw <EOL> def find_resource ( manager , name_or_id , ** find_args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return manager . get ( int ( name_or_id ) ) <EOL> except ( TypeError , ValueError , exceptions . NotFound ) : <EOL> pass <EOL> try : <EOL> if six . PY2 : <EOL> tmp_id = strutils . safe_encode ( name_or_id ) <EOL> else : <EOL> tmp_id = strutils . safe_decode ( name_or_id ) <EOL> if uuidutils . is_uuid_like ( tmp_id ) : <EOL> return manager . get ( tmp_id ) <EOL> except ( TypeError , ValueError , exceptions . NotFound ) : <EOL> pass <EOL> if getattr ( manager , '<STR_LIT>' , False ) : <EOL> try : <EOL> return manager . get ( name_or_id ) <EOL> except exceptions . NotFound : <EOL> pass <EOL> try : <EOL> try : <EOL> return manager . find ( human_id = name_or_id , ** find_args ) <EOL> except exceptions . NotFound : <EOL> pass <EOL> try : <EOL> resource = getattr ( manager , '<STR_LIT>' , None ) <EOL> name_attr = resource . NAME_ATTR if resource else '<STR_LIT:name>' <EOL> kwargs = { name_attr : name_or_id } <EOL> kwargs . update ( find_args ) <EOL> return manager . find ( ** kwargs ) <EOL> except exceptions . NotFound : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % { <EOL> "<STR_LIT:name>" : manager . resource_class . __name__ . lower ( ) , <EOL> "<STR_LIT>" : name_or_id <EOL> } <EOL> raise exceptions . CommandError ( msg ) <EOL> except exceptions . NoUniqueMatch : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % { <EOL> "<STR_LIT:name>" : manager . resource_class . __name__ . lower ( ) , <EOL> "<STR_LIT>" : name_or_id <EOL> } <EOL> raise exceptions . CommandError ( msg ) <EOL> def service_type ( stype ) : <EOL> """<STR_LIT>""" <EOL> def inner ( f ) : <EOL> f . service_type = stype <EOL> return f <EOL> return inner <EOL> def get_service_type ( f ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( f , '<STR_LIT>' , None ) <EOL> def pretty_choice_list ( l ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( "<STR_LIT>" % i for i in l ) <EOL> def exit ( msg = '<STR_LIT>' ) : <EOL> if msg : <EOL> print ( msg , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
