Skip to content

Commit fe02ab9

Browse files
Merge pull request softlayer#596 from softlayer/shell
Adds Shell / Moves fixtures
2 parents c635bb4 + 60a7bc2 commit fe02ab9

72 files changed

Lines changed: 387 additions & 123 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SoftLayer/CLI/call_api.py

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,63 +31,4 @@ def cli(env, service, method, parameters, _id, mask, limit, offset):
3131
mask=mask,
3232
limit=limit,
3333
offset=offset)
34-
return format_api_result(result)
35-
36-
37-
def format_api_result(value):
38-
"""Convert raw API responses to response tables."""
39-
if isinstance(value, list):
40-
return format_api_list(value)
41-
if isinstance(value, dict):
42-
return format_api_dict(value)
43-
return value
44-
45-
46-
def format_api_dict(result):
47-
"""Format dictionary responses into key-value table."""
48-
49-
table = formatting.KeyValueTable(['Name', 'Value'])
50-
table.align['Name'] = 'r'
51-
table.align['Value'] = 'l'
52-
53-
for key, value in result.items():
54-
value = format_api_result(value)
55-
table.add_row([key, value])
56-
57-
return table
58-
59-
60-
def format_api_list(result):
61-
"""Format list responses into a table."""
62-
63-
if not result:
64-
return result
65-
66-
if isinstance(result[0], dict):
67-
return format_api_list_objects(result)
68-
69-
table = formatting.Table(["Value"])
70-
for item in result:
71-
table.add_row([format_api_result(item)])
72-
return table
73-
74-
75-
def format_api_list_objects(result):
76-
"""Format list of objects into a table."""
77-
78-
all_keys = set()
79-
for item in result:
80-
all_keys = all_keys.union(item.keys())
81-
82-
all_keys = sorted(all_keys)
83-
table = formatting.Table(all_keys)
84-
85-
for item in result:
86-
values = []
87-
for key in all_keys:
88-
value = format_api_result(item.get(key))
89-
values.append(value)
90-
91-
table.add_row(values)
92-
93-
return table
34+
return formatting.iter_to_table(result)

SoftLayer/CLI/core.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
SoftLayer.core
3-
~~~~~~~~~~~~~~
2+
SoftLayer.CLI.core
3+
~~~~~~~~~~~~~~~~~~
44
Core for the SoftLayer CLI
55
66
:license: MIT, see LICENSE for more details.
@@ -84,7 +84,7 @@ def get_command(self, ctx, name):
8484
type=click.Path(resolve_path=True))
8585
@click.option('--debug',
8686
required=False,
87-
default='0',
87+
default=None,
8888
help="Sets the debug noise level",
8989
type=click.Choice(sorted([str(key) for key
9090
in DEBUG_LOGGING_MAP.keys()])))
@@ -99,11 +99,11 @@ def get_command(self, ctx, name):
9999
@click.option('--proxy',
100100
required=False,
101101
help="HTTP[S] proxy to be use to make API calls")
102-
@click.option('--really', '-y',
102+
@click.option('--really / --not-really', '-y',
103103
is_flag=True,
104104
required=False,
105105
help="Confirm all prompt actions")
106-
@click.option('--fixtures',
106+
@click.option('--fixtures / --no-fixtures',
107107
envvar='SL_FIXTURES',
108108
is_flag=True,
109109
required=False,
@@ -122,9 +122,8 @@ def cli(env,
122122
"""Main click CLI entry-point."""
123123

124124
# Set logging level
125-
debug_int = int(debug)
126-
if debug_int:
127-
verbose = debug_int
125+
if debug is not None:
126+
verbose = int(debug)
128127

129128
if verbose:
130129
logger = logging.getLogger()
@@ -172,12 +171,12 @@ def output_result(env, result, timings=False, **kwargs):
172171
env.err(env.fmt(timing_table))
173172

174173

175-
def main():
174+
def main(reraise_exceptions=False, **kwargs):
176175
"""Main program. Catches several common errors and displays them nicely."""
177176
exit_status = 0
178177

179178
try:
180-
cli.main()
179+
cli.main(**kwargs)
181180
except SoftLayer.SoftLayerAPIError as ex:
182181
if 'invalid api token' in ex.faultString.lower():
183182
print("Authentication Failed: To update your credentials,"
@@ -193,6 +192,9 @@ def main():
193192
print(str(ex.message))
194193
exit_status = ex.code
195194
except Exception:
195+
if reraise_exceptions:
196+
raise
197+
196198
import traceback
197199
print("An unexpected error has occured:")
198200
print(str(traceback.format_exc()))

SoftLayer/CLI/environment.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ def __init__(self):
2828
self.commands = {}
2929
self.aliases = {}
3030

31+
self.vars = {}
32+
3133
self.client = None
3234
self.format = 'table'
3335
self.skip_confirmations = False
34-
self._modules_loaded = False
3536
self.config_file = None
3637

38+
self._modules_loaded = False
39+
3740
def out(self, output, newline=True):
3841
"""Outputs a string to the console (stdout)."""
3942
click.echo(output, nl=newline)
@@ -92,23 +95,22 @@ def load(self):
9295
if self._modules_loaded is True:
9396
return
9497

95-
self._load_modules_from_python()
96-
self._load_modules_from_entry_points()
98+
self.load_modules_from_python(routes.ALL_ROUTES)
99+
self.aliases.update(routes.ALL_ALIASES)
100+
self._load_modules_from_entry_points('softlayer.cli')
97101

98102
self._modules_loaded = True
99103

100-
def _load_modules_from_python(self):
104+
def load_modules_from_python(self, route_list):
101105
"""Load modules from the native python source."""
102-
for name, modpath in routes.ALL_ROUTES:
106+
for name, modpath in route_list:
103107
if ':' in modpath:
104108
path, attr = modpath.split(':', 1)
105109
else:
106110
path, attr = modpath, None
107111
self.commands[name] = ModuleLoader(path, attr=attr)
108112

109-
self.aliases = routes.ALL_ALIASES
110-
111-
def _load_modules_from_entry_points(self):
113+
def _load_modules_from_entry_points(self, entry_point_group):
112114
"""Load modules from the entry_points (slower).
113115
114116
Entry points can be used to add new commands to the CLI.
@@ -118,7 +120,7 @@ def _load_modules_from_entry_points(self):
118120
entry_points={'softlayer.cli': ['new-cmd = mymodule.new_cmd.cli']}
119121
120122
"""
121-
for obj in pkg_resources.iter_entry_points(group='softlayer.cli',
123+
for obj in pkg_resources.iter_entry_points(group=entry_point_group,
122124
name=None):
123125
self.commands[obj.name] = obj
124126

SoftLayer/CLI/formatting.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,62 @@ def _format_python_value(value):
338338
if hasattr(value, 'to_python'):
339339
return value.to_python()
340340
return value
341+
342+
343+
def iter_to_table(value):
344+
"""Convert raw API responses to response tables."""
345+
if isinstance(value, list):
346+
return _format_list(value)
347+
if isinstance(value, dict):
348+
return _format_dict(value)
349+
return value
350+
351+
352+
def _format_dict(result):
353+
"""Format dictionary responses into key-value table."""
354+
355+
table = KeyValueTable(['Name', 'Value'])
356+
table.align['Name'] = 'r'
357+
table.align['Value'] = 'l'
358+
359+
for key, value in result.items():
360+
value = iter_to_table(value)
361+
table.add_row([key, value])
362+
363+
return table
364+
365+
366+
def _format_list(result):
367+
"""Format list responses into a table."""
368+
369+
if not result:
370+
return result
371+
372+
if isinstance(result[0], dict):
373+
return _format_list_objects(result)
374+
375+
table = Table(["Value"])
376+
for item in result:
377+
table.add_row([iter_to_table(item)])
378+
return table
379+
380+
381+
def _format_list_objects(result):
382+
"""Format list of objects into a table."""
383+
384+
all_keys = set()
385+
for item in result:
386+
all_keys = all_keys.union(item.keys())
387+
388+
all_keys = sorted(all_keys)
389+
table = Table(all_keys)
390+
391+
for item in result:
392+
values = []
393+
for key in all_keys:
394+
value = iter_to_table(item.get(key))
395+
values.append(value)
396+
397+
table.add_row(values)
398+
399+
return table

SoftLayer/CLI/routes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"""
88

99
ALL_ROUTES = [
10+
('shell', 'SoftLayer.shell.core:cli'),
11+
1012
('call-api', 'SoftLayer.CLI.call_api:cli'),
1113

1214
('vs', 'SoftLayer.CLI.virt'),
File renamed without changes.
File renamed without changes.

SoftLayer/testing/fixtures/SoftLayer_Billing_Order_Quote.py renamed to SoftLayer/fixtures/SoftLayer_Billing_Order_Quote.py

File renamed without changes.
File renamed without changes.

SoftLayer/testing/fixtures/SoftLayer_Dns_Domain_ResourceRecord.py renamed to SoftLayer/fixtures/SoftLayer_Dns_Domain_ResourceRecord.py

File renamed without changes.

0 commit comments

Comments
 (0)