Commit 3384e64a authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Added some convenience functions to the CLI.

parent e91988cf
......@@ -11,7 +11,7 @@ documentation = 'README.md'
license = 'MIT License'
keywords = []
dependencies = ['requests']
dependencies = ['pyyaml', 'requests']
supported = [(2, 7), (3, 3), (3, 4)]
classifiers = [
'Development Status :: 3 - Alpha',
......
import argparse
import datetime
import getpass
import hashlib
import json
import socket
import sys
import yaml
from . import doc_split, usage, version
from .transfer_client import TransferClient
......@@ -230,6 +233,88 @@ def cancel(output_handle, server_name, user_id, ssl_check):
transfer_client.update(user_id, transfer['id'], 'cancelled')
def _write_yaml(output_handle, data):
yaml.safe_dump(data, output_handle, width=76, default_flow_style=False)
def _read_timestamp(timestamp):
return datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
def _reformat_transfer(transfer):
"""
Reformat a transfer object for human readability.
:arg object transfer: Transfer object.
"""
readable_transfer = transfer.copy()
files = readable_transfer.pop('files')
readable_transfer['number_of_files'] = len(files)
if readable_transfer['status'] == 'initiated':
readable_transfer['uploaded'] = len(
filter(lambda x: x['status'] == 'uploaded', files))
if 'end_date' in readable_transfer:
readable_transfer['duration'] = str(
_read_timestamp(readable_transfer['end_date']) -
_read_timestamp(readable_transfer['start_date']))
return readable_transfer
def transfers_summary(output_handle, server_name, user_id, ssl_check):
"""
Give a human readable summary of all transfers.
:arg stream output_handle: Open writeable handle to a file.
:arg str server_name: Name of the transfer server.
:arg str user_id: User ID.
:arg bool ssl_check: Check server SSL certificate.
"""
transfer_client = TransferClient(server_name, ssl_check)
_write_yaml(
output_handle,
map(_reformat_transfer, transfer_client.users(user_id)['transfers']))
def last_transfer_summary(output_handle, server_name, user_id, ssl_check):
"""
Give a human readable summary of the last transfer.
:arg stream output_handle: Open writeable handle to a file.
:arg str server_name: Name of the transfer server.
:arg str user_id: User ID.
:arg bool ssl_check: Check server SSL certificate.
"""
transfer_client = TransferClient(server_name, ssl_check)
transfers = transfer_client.users(user_id)['transfers']
if transfers:
_write_yaml(output_handle, _reformat_transfer(transfers[-1]))
def transfer_summary(
output_handle, server_name, user_id, transfer_id, ssl_check):
"""
Give a human readable summary of a transfer.
:arg stream output_handle: Open writeable handle to a file.
:arg str server_name: Name of the transfer server.
:arg str user_id: User ID.
:arg str transfer_id: Transfer ID.
:arg bool ssl_check: Check server SSL certificate.
"""
transfer_client = TransferClient(server_name, ssl_check)
for transfer in transfer_client.users(user_id)['transfers']:
if transfer['id'] == transfer_id:
_write_yaml(output_handle, transfer)
return
def main():
"""
Main entry point.
......@@ -347,6 +432,21 @@ def main():
description=doc_split(cancel))
subparser.set_defaults(func=cancel)
subparser = subparsers.add_parser(
'transfers_summary', parents=[default_parser],
description=doc_split(transfers_summary))
subparser.set_defaults(func=transfers_summary)
subparser = subparsers.add_parser(
'last_transfer_summary', parents=[default_parser],
description=doc_split(last_transfer_summary))
subparser.set_defaults(func=last_transfer_summary)
subparser = subparsers.add_parser(
'transfer_summary', parents=[default_parser, transfer_id_parser],
description=doc_split(transfer_summary))
subparser.set_defaults(func=transfer_summary)
try:
args = parser.parse_args()
except IOError, error:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment