77"""
88from __future__ import print_function
99import logging
10+ import os
1011import sys
12+ import time
1113import types
1214
1315import click
1618from SoftLayer .CLI import environment
1719from SoftLayer .CLI import exceptions
1820from SoftLayer .CLI import formatting
21+ from SoftLayer import consts
1922
2023# pylint: disable=too-many-public-methods, broad-except, unused-argument
2124# pylint: disable=redefined-builtin, super-init-not-called
2225
26+ START_TIME = time .time ()
2327DEBUG_LOGGING_MAP = {
2428 0 : logging .CRITICAL ,
2529 1 : logging .WARNING ,
@@ -73,57 +77,44 @@ def get_command(self, ctx, name):
7377 'auto_envvar_prefix' : 'SLCLI' })
7478@click .option ('--format' ,
7579 default = DEFAULT_FORMAT ,
80+ show_default = True ,
7681 help = "Output format" ,
7782 type = click .Choice (VALID_FORMATS ))
7883@click .option ('--config' , '-C' ,
7984 required = False ,
8085 default = click .get_app_dir ('softlayer' , force_posix = True ),
86+ show_default = True ,
8187 help = "Config file location" ,
8288 type = click .Path (resolve_path = True ))
83- @click .option ('--debug' ,
84- required = False ,
85- default = None ,
86- help = "Sets the debug noise level" ,
87- type = click .Choice (sorted ([str (key ) for key
88- in DEBUG_LOGGING_MAP .keys ()])))
8989@click .option ('--verbose' , '-v' ,
90- help = "Sets the debug noise level" ,
90+ help = "Sets the debug noise level, specify multiple times "
91+ "for more verbosity." ,
9192 type = click .IntRange (0 , 3 , clamp = True ),
9293 count = True )
93- @click .option ('--timings' ,
94- required = False ,
95- is_flag = True ,
96- help = "Time each API call and display after results" )
9794@click .option ('--proxy' ,
9895 required = False ,
9996 help = "HTTP[S] proxy to be use to make API calls" )
10097@click .option ('--really / --not-really' , '-y' ,
10198 is_flag = True ,
10299 required = False ,
103100 help = "Confirm all prompt actions" )
104- @click .option ('--fixtures / --no-fixtures' ,
105- envvar = 'SL_FIXTURES' ,
101+ @click .option ('--demo / --no-demo' ,
106102 is_flag = True ,
107103 required = False ,
108- help = "Use fixtures instead of actually making API calls" )
104+ help = "Use demo data instead of actually making API calls" )
109105@click .version_option (prog_name = "slcli (SoftLayer Command-line)" )
110106@environment .pass_env
111107def cli (env ,
112108 format = 'table' ,
113109 config = None ,
114- debug = 0 ,
115110 verbose = 0 ,
116111 proxy = None ,
117112 really = False ,
118- fixtures = False ,
113+ demo = False ,
119114 ** kwargs ):
120115 """Main click CLI entry-point."""
121116
122- # Set logging level
123- if debug is not None :
124- verbose = int (debug )
125-
126- if verbose :
117+ if verbose > 0 :
127118 logger = logging .getLogger ()
128119 logger .addHandler (logging .StreamHandler ())
129120 logger .setLevel (DEBUG_LOGGING_MAP .get (verbose , logging .DEBUG ))
@@ -134,7 +125,7 @@ def cli(env,
134125 env .format = format
135126 if env .client is None :
136127 # Environment can be passed in explicitly. This is used for testing
137- if fixtures :
128+ if demo :
138129 client = SoftLayer .BaseClient (
139130 transport = SoftLayer .FixtureTransport (),
140131 auth = None ,
@@ -147,23 +138,32 @@ def cli(env,
147138 )
148139 env .client = client
149140
141+ env .vars ['_start' ] = time .time ()
150142 env .vars ['_timings' ] = SoftLayer .TimingTransport (env .client .transport )
151143 env .client .transport = env .vars ['_timings' ]
152144
153145
154146@cli .resultcallback ()
155147@environment .pass_env
156- def output_result (env , timings = False , * args , ** kwargs ):
157- """Outputs the results returned by the CLI and also outputs timings."""
148+ def output_diagnostics (env , verbose = 0 , ** kwargs ):
149+ """Output diagnostic information."""
150+
151+ if verbose > 0 :
152+ diagnostic_table = formatting .Table (['name' , 'value' ])
153+ diagnostic_table .add_row (['execution_time' , time .time () - START_TIME ])
158154
159- if timings and env .vars .get ('_timings' ):
160- timing_table = formatting .Table (['service' , 'method' , 'time' ])
155+ api_call_value = []
156+ for call , _ , duration in env .vars ['_timings' ].get_last_calls ():
157+ api_call_value .append (
158+ "%s::%s (%fs)" % (call .service , call .method , duration ))
161159
162- calls = env .vars ['_timings' ].get_last_calls ()
163- for call , _ , duration in calls :
164- timing_table .add_row ([call .service , call .method , duration ])
160+ diagnostic_table .add_row (['api_calls' , api_call_value ])
161+ diagnostic_table .add_row (['version' , consts .USER_AGENT ])
162+ diagnostic_table .add_row (['python_version' , sys .version ])
163+ diagnostic_table .add_row (['library_location' ,
164+ os .path .dirname (SoftLayer .__file__ )])
165165
166- env .err (env .fmt (timing_table ))
166+ env .err (env .fmt (diagnostic_table ))
167167
168168
169169def main (reraise_exceptions = False , ** kwargs ):
0 commit comments