<s> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> from heatclient . common import template_utils <EOL> from oslo_serialization import jsonutils <EOL> from oslo_utils import encodeutils <EOL> from oslo_utils import importutils <EOL> import prettytable <EOL> import six <EOL> import yaml <EOL> from senlinclient . common import exc <EOL> from senlinclient . common . i18n import _ <EOL> supported_formats = { <EOL> "<STR_LIT>" : lambda x : jsonutils . dumps ( x , indent = <NUM_LIT:2> ) , <EOL> "<STR_LIT>" : lambda x : yaml . safe_dump ( x , default_flow_style = False ) <EOL> } <EOL> def arg ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _decorator ( func ) : <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> 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 import_versioned_module ( version , submodule = None ) : <EOL> module = '<STR_LIT>' % version <EOL> if submodule : <EOL> module = '<STR_LIT:.>' . join ( ( module , submodule ) ) <EOL> return importutils . import_module ( module ) <EOL> def format_nested_dict ( d , fields , column_names ) : <EOL> if d is None : <EOL> return '<STR_LIT>' <EOL> pt = prettytable . PrettyTable ( caching = False , print_empty = False , <EOL> header = True , field_names = column_names ) <EOL> for n in column_names : <EOL> pt . align [ n ] = '<STR_LIT:l>' <EOL> keys = sorted ( d . keys ( ) ) <EOL> for field in keys : <EOL> value = d [ field ] <EOL> if not isinstance ( value , six . string_types ) : <EOL> value = jsonutils . dumps ( value , indent = <NUM_LIT:2> , ensure_ascii = False ) <EOL> pt . add_row ( [ field , value . strip ( '<STR_LIT:">' ) ] ) <EOL> return pt . get_string ( ) <EOL> def nested_dict_formatter ( d , column_names ) : <EOL> return lambda o : format_nested_dict ( o , d , column_names ) <EOL> def json_formatter ( js ) : <EOL> return jsonutils . dumps ( js , indent = <NUM_LIT:2> , ensure_ascii = False ) <EOL> def list_formatter ( record ) : <EOL> return '<STR_LIT:\n>' . join ( record or [ ] ) <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 ) <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> if six . PY3 : <EOL> print ( encodeutils . safe_encode ( pt . get_string ( ** kwargs ) ) . decode ( ) ) <EOL> else : <EOL> print ( encodeutils . safe_encode ( pt . get_string ( ** kwargs ) ) ) <EOL> def print_list ( objs , fields , formatters = None , sortby_index = <NUM_LIT:0> , <EOL> mixed_case_fields = None , field_labels = None ) : <EOL> if not objs : <EOL> objs = [ ] <EOL> try : <EOL> _print_list ( objs , fields , formatters = formatters , <EOL> sortby_index = sortby_index , <EOL> mixed_case_fields = mixed_case_fields , <EOL> field_labels = field_labels ) <EOL> except Exception as ex : <EOL> exc . parse_exception ( ex ) <EOL> def print_dict ( d , formatters = None ) : <EOL> formatters = formatters or { } <EOL> pt = prettytable . PrettyTable ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> caching = False , print_empty = False ) <EOL> pt . align = '<STR_LIT:l>' <EOL> for field in d . keys ( ) : <EOL> if field in formatters : <EOL> pt . add_row ( [ field , formatters [ field ] ( d [ field ] ) ] ) <EOL> else : <EOL> pt . add_row ( [ field , d [ field ] ] ) <EOL> content = pt . get_string ( sortby = '<STR_LIT>' ) <EOL> if six . PY3 : <EOL> print ( encodeutils . safe_encode ( content ) . decode ( ) ) <EOL> else : <EOL> print ( encodeutils . safe_encode ( content ) ) <EOL> def format_parameters ( params , parse_semicolon = True ) : <EOL> """<STR_LIT>""" <EOL> if not params : <EOL> return { } <EOL> if parse_semicolon : <EOL> if len ( params ) == <NUM_LIT:1> : <EOL> params = params [ <NUM_LIT:0> ] . split ( '<STR_LIT:;>' ) <EOL> parameters = { } <EOL> for p in params : <EOL> try : <EOL> ( n , v ) = p . split ( ( '<STR_LIT:=>' ) , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> msg = _ ( '<STR_LIT>' ) % p <EOL> raise exc . CommandError ( msg ) <EOL> if n not in parameters : <EOL> parameters [ n ] = v <EOL> else : <EOL> if not isinstance ( parameters [ n ] , list ) : <EOL> parameters [ n ] = [ parameters [ n ] ] <EOL> parameters [ n ] . append ( v ) <EOL> return parameters <EOL> def get_spec_content ( filename ) : <EOL> with open ( filename , '<STR_LIT:r>' ) as f : <EOL> try : <EOL> data = yaml . load ( f ) <EOL> except Exception as ex : <EOL> raise exc . CommandError ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % six . text_type ( ex ) ) <EOL> return data <EOL> def process_stack_spec ( spec ) : <EOL> try : <EOL> tmplfile = spec . get ( '<STR_LIT>' , None ) <EOL> except AttributeError as ex : <EOL> raise exc . FileFormatError ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % six . text_type ( ex ) ) <EOL> if not tmplfile : <EOL> raise exc . FileFormatError ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> tpl_files , template = template_utils . get_template_contents ( <EOL> template_file = tmplfile ) <EOL> env_files , env = template_utils . process_multiple_environments_and_files ( <EOL> env_paths = spec . get ( '<STR_LIT>' , None ) ) <EOL> new_spec = { <EOL> '<STR_LIT>' : spec . get ( '<STR_LIT>' , True ) , <EOL> '<STR_LIT>' : spec . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : spec . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : spec . get ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> '<STR_LIT>' : template , <EOL> '<STR_LIT>' : dict ( list ( tpl_files . items ( ) ) + list ( env_files . items ( ) ) ) , <EOL> '<STR_LIT>' : env <EOL> } <EOL> return new_spec <EOL> def format_output ( output , format = '<STR_LIT>' ) : <EOL> fmt = format . lower ( ) <EOL> try : <EOL> return supported_formats [ fmt ] ( output ) <EOL> except KeyError : <EOL> raise exc . HTTPUnsupported ( _ ( '<STR_LIT>' ) <EOL> % fmt ) <EOL> def exit ( msg = '<STR_LIT>' ) : <EOL> if msg : <EOL> print ( msg , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
