Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] Fix storage table outputs and help text. #1402

Merged
merged 4 commits into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion azure-cli.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@
<Compile Include="command_modules\azure-cli-role\azure\cli\command_modules\role\tests\test_graph.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_command_type.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_factory.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_format.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_help.py">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -916,7 +919,6 @@
<Content Include="azure-cli\setup.cfg" />
<Content Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\template.json" />
<Content Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\tests\recordings\test_acr.yaml" />
<Content Include="command_modules\azure-cli-acr\azure\README.rst" />
<Content Include="command_modules\azure-cli-acr\requirements.txt" />
<Content Include="command_modules\azure-cli-acs\requirements.txt" />
<Content Include="command_modules\azure-cli-appservice\azure\cli\command_modules\appservice\tests\recordings\test_appservice_error_polish.yaml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint:disable=line-too-long

from collections import OrderedDict

def build_table_output(result, projection):

if not isinstance(result, list):
result = [result]

final_list = []

for item in result:
def _value_from_path(path):
obj = item # pylint: disable=cell-var-from-loop
try:
for part in path.split('.'):
obj = obj.get(part, None)
except AttributeError:
obj = None
return obj or ' '

item_dict = OrderedDict()
for element in projection:
item_dict[element[0]] = _value_from_path(element[1])
final_list.append(item_dict)
return final_list

def transform_container_list(result):
return build_table_output(result, [
('Name', 'name'),
('Lease Status', 'properties.leaseStatus'),
('Last Modified', 'properties.lastModified')
])

def transform_container_show(result):
return build_table_output(result, [
('Name', 'name'),
('Lease Status', 'properties.lease.status'),
('Last Modified', 'properties.lastModified')
])

def transform_blob_output(result):
return build_table_output(result, [
('Name', 'name'),
('Blob Type', 'properties.blobType'),
('Length', 'properties.contentLength'),
('Content Type', 'properties.contentSettings.contentType'),
('Last Modified', 'properties.lastModified')
])

def transform_share_list(result):
return build_table_output(result, [
('Name', 'name'),
('Quota', 'properties.quota'),
('Last Modified', 'properties.lastModified')
])

def transform_file_output(result):
""" Transform to convert SDK file/dir list output to something that
more clearly distinguishes between files and directories. """
new_result = []

for item in result.get('items', [result]):
new_entry = OrderedDict()
item_name = item['name']
try:
_ = item['properties']['contentLength']
is_dir = False
except KeyError:
item_name = '{}/'.format(item_name)
is_dir = True
new_entry['Name'] = item_name
new_entry['Content Length'] = ' ' if is_dir else item['properties']['contentLength']
new_entry['Type'] = 'dir' if is_dir else 'file'
new_entry['Last Modified'] = item['properties']['lastModified'] or ' '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will item['properties']['lastModified'] always exist?
Or is this check to see if item['properties']['lastModified'] is False, empty etc.?

Otherwise, you may get a KeyError.

Copy link
Member Author

@tjprescott tjprescott Nov 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If lastModified is null, then what happens is the table just leaves that column off. The or ' ' ensures that it at least has a space so you will get a blank "cell". Otherwise your table dynamic when it really isn't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay that works

new_result.append(new_entry)
return sorted(new_result, key=lambda k: k['Name'])

def transform_entity_show(result):
timestamp = result.pop('Timestamp')
result.pop('etag')

# Reassemble the output
new_result = OrderedDict()
new_result['Partition'] = result.pop('PartitionKey')
new_result['Row'] = result.pop('RowKey')
for key in sorted(result.keys()):
new_result[key] = result[key]
new_result['Timestamp'] = timestamp
return new_result

def transform_message_show(result):
ordered_result = []
for item in result:
new_result = OrderedDict()
new_result['MessageId'] = item.pop('id')
new_result['Content'] = item.pop('content')
new_result['InsertionTime'] = item.pop('insertionTime')
new_result['ExpirationTime'] = item.pop('expirationTime')
for key in sorted(item.keys()):
new_result[key] = item[key]
ordered_result.append(new_result)
return ordered_result

def transform_boolean_for_table(result):
for key in result:
result[key] = str(result[key])
return result
Loading