<s> """<STR_LIT>""" <EOL> import getpass <EOL> import logging <EOL> import os <EOL> import six <EOL> import time <EOL> from oslo_utils import importutils <EOL> from openstackclient . common import exceptions <EOL> def find_resource ( manager , name_or_id , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( name_or_id , int ) or name_or_id . isdigit ( ) : <EOL> return manager . get ( int ( name_or_id ) , ** kwargs ) <EOL> except Exception as ex : <EOL> if type ( ex ) . __name__ == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> raise <EOL> try : <EOL> return manager . get ( name_or_id , ** kwargs ) <EOL> except Exception : <EOL> pass <EOL> if len ( kwargs ) == <NUM_LIT:0> : <EOL> kwargs = { } <EOL> try : <EOL> if '<STR_LIT>' in manager . resource_class . __dict__ : <EOL> kwargs [ manager . resource_class . NAME_ATTR ] = name_or_id <EOL> else : <EOL> kwargs [ '<STR_LIT:name>' ] = name_or_id <EOL> except Exception : <EOL> pass <EOL> try : <EOL> return manager . find ( ** kwargs ) <EOL> except Exception as ex : <EOL> if type ( ex ) . __name__ == '<STR_LIT>' : <EOL> msg = "<STR_LIT>" % ( manager . resource_class . __name__ . lower ( ) , name_or_id ) <EOL> raise exceptions . CommandError ( msg ) <EOL> if type ( ex ) . __name__ == '<STR_LIT>' : <EOL> msg = "<STR_LIT>" % ( manager . resource_class . __name__ . lower ( ) , name_or_id ) <EOL> raise exceptions . CommandError ( msg ) <EOL> else : <EOL> pass <EOL> for resource in manager . list ( ) : <EOL> if ( resource . get ( '<STR_LIT:id>' ) == name_or_id or <EOL> resource . get ( '<STR_LIT:name>' ) == name_or_id ) : <EOL> return resource <EOL> else : <EOL> msg = "<STR_LIT>" % name_or_id <EOL> raise exceptions . CommandError ( msg ) <EOL> def format_dict ( data ) : <EOL> """<STR_LIT>""" <EOL> output = "<STR_LIT>" <EOL> for s in sorted ( data ) : <EOL> output = output + s + "<STR_LIT>" + six . text_type ( data [ s ] ) + "<STR_LIT>" <EOL> return output [ : - <NUM_LIT:2> ] <EOL> def format_list ( data , separator = '<STR_LIT:U+002CU+0020>' ) : <EOL> """<STR_LIT>""" <EOL> return separator . join ( sorted ( data ) ) <EOL> def format_list_of_dicts ( data ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:\n>' . join ( format_dict ( i ) for i in data ) <EOL> def get_field ( item , field ) : <EOL> try : <EOL> if isinstance ( item , dict ) : <EOL> return item [ field ] <EOL> else : <EOL> return getattr ( item , field ) <EOL> except Exception : <EOL> msg = "<STR_LIT>" % field <EOL> raise exceptions . CommandError ( msg ) <EOL> def get_item_properties ( item , fields , mixed_case_fields = None , formatters = None ) : <EOL> """<STR_LIT>""" <EOL> if mixed_case_fields is None : <EOL> mixed_case_fields = [ ] <EOL> if formatters is None : <EOL> formatters = { } <EOL> row = [ ] <EOL> for field in fields : <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 ( item , field_name , '<STR_LIT>' ) <EOL> if field in formatters : <EOL> row . append ( formatters [ field ] ( data ) ) <EOL> else : <EOL> row . append ( data ) <EOL> return tuple ( row ) <EOL> def get_dict_properties ( item , fields , mixed_case_fields = None , formatters = None ) : <EOL> """<STR_LIT>""" <EOL> if mixed_case_fields is None : <EOL> mixed_case_fields = [ ] <EOL> if formatters is None : <EOL> formatters = { } <EOL> row = [ ] <EOL> for field in fields : <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 = item [ field_name ] if field_name in item else '<STR_LIT>' <EOL> if field in formatters : <EOL> row . append ( formatters [ field ] ( data ) ) <EOL> else : <EOL> row . append ( data ) <EOL> return tuple ( row ) <EOL> def sort_items ( items , sort_str ) : <EOL> """<STR_LIT>""" <EOL> if not sort_str : <EOL> return items <EOL> items = list ( items ) <EOL> sort_keys = sort_str . strip ( ) . split ( '<STR_LIT:U+002C>' ) <EOL> for sort_key in reversed ( sort_keys ) : <EOL> reverse = False <EOL> if '<STR_LIT::>' in sort_key : <EOL> sort_key , direction = sort_key . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if not sort_key : <EOL> msg = "<STR_LIT>" <EOL> raise exceptions . CommandError ( msg ) <EOL> if direction not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not direction : <EOL> direction = "<STR_LIT>" <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( direction , sort_key ) ) <EOL> raise exceptions . CommandError ( msg ) <EOL> if direction == '<STR_LIT>' : <EOL> reverse = True <EOL> items . sort ( key = lambda item : get_field ( item , sort_key ) , <EOL> reverse = reverse ) <EOL> return items <EOL> def env ( * vars , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for v in vars : <EOL> value = os . environ . get ( v , None ) <EOL> if value : <EOL> return value <EOL> return kwargs . get ( '<STR_LIT:default>' , '<STR_LIT>' ) <EOL> def get_client_class ( api_name , version , version_map ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> client_path = version_map [ str ( version ) ] <EOL> except ( KeyError , ValueError ) : <EOL> msg = "<STR_LIT>" % ( <EOL> ( api_name , version , '<STR_LIT:U+002CU+0020>' . join ( list ( version_map . keys ( ) ) ) ) ) <EOL> raise exceptions . UnsupportedVersion ( msg ) <EOL> return importutils . import_class ( client_path ) <EOL> def wait_for_status ( status_f , <EOL> res_id , <EOL> status_field = '<STR_LIT:status>' , <EOL> success_status = [ '<STR_LIT>' ] , <EOL> error_status = [ '<STR_LIT:error>' ] , <EOL> sleep_time = <NUM_LIT:5> , <EOL> callback = None ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> res = status_f ( res_id ) <EOL> status = getattr ( res , status_field , '<STR_LIT>' ) . lower ( ) <EOL> if status in success_status : <EOL> retval = True <EOL> break <EOL> elif status in error_status : <EOL> retval = False <EOL> break <EOL> if callback : <EOL> progress = getattr ( res , '<STR_LIT>' , None ) or <NUM_LIT:0> <EOL> callback ( progress ) <EOL> time . sleep ( sleep_time ) <EOL> return retval <EOL> def wait_for_delete ( manager , <EOL> res_id , <EOL> status_field = '<STR_LIT:status>' , <EOL> error_status = [ '<STR_LIT:error>' ] , <EOL> exception_name = [ '<STR_LIT>' ] , <EOL> sleep_time = <NUM_LIT:5> , <EOL> timeout = <NUM_LIT> , <EOL> callback = None ) : <EOL> """<STR_LIT>""" <EOL> total_time = <NUM_LIT:0> <EOL> while total_time < timeout : <EOL> try : <EOL> res = manager . get ( res_id ) <EOL> except Exception as ex : <EOL> if type ( ex ) . __name__ in exception_name : <EOL> return True <EOL> raise <EOL> status = getattr ( res , status_field , '<STR_LIT>' ) . lower ( ) <EOL> if status in error_status : <EOL> return False <EOL> if callback : <EOL> progress = getattr ( res , '<STR_LIT>' , None ) or <NUM_LIT:0> <EOL> callback ( progress ) <EOL> time . sleep ( sleep_time ) <EOL> total_time += sleep_time <EOL> return False <EOL> def get_effective_log_level ( ) : <EOL> """<STR_LIT>""" <EOL> root_log = logging . getLogger ( ) <EOL> min_log_lvl = logging . CRITICAL <EOL> for handler in root_log . handlers : <EOL> min_log_lvl = min ( min_log_lvl , handler . level ) <EOL> return min_log_lvl <EOL> def get_password ( stdin , prompt = None , confirm = True ) : <EOL> message = prompt or "<STR_LIT>" <EOL> if hasattr ( stdin , '<STR_LIT>' ) and stdin . isatty ( ) : <EOL> try : <EOL> while True : <EOL> first_pass = getpass . getpass ( message ) <EOL> if not confirm : <EOL> return first_pass <EOL> second_pass = getpass . getpass ( "<STR_LIT>" + message ) <EOL> if first_pass == second_pass : <EOL> return first_pass <EOL> print ( "<STR_LIT>" ) <EOL> except EOFError : <EOL> raise exceptions . CommandError ( "<STR_LIT>" ) <EOL> raise exceptions . CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def read_blob_file_contents ( blob_file ) : <EOL> try : <EOL> with open ( blob_file ) as file : <EOL> blob = file . read ( ) . strip ( ) <EOL> return blob <EOL> except IOError : <EOL> msg = "<STR_LIT>" <EOL> raise exceptions . CommandError ( msg % blob_file ) <EOL> def build_kwargs_dict ( arg_name , value ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> if value : <EOL> kwargs [ arg_name ] = value <EOL> return kwargs <EOL> def is_ascii ( string ) : <EOL> try : <EOL> string . decode ( '<STR_LIT:ascii>' ) <EOL> return True <EOL> except UnicodeDecodeError : <EOL> return False </s>
