Skip to content

Commit

Permalink
Merge pull request #99 from zerc/remove-u-from-output
Browse files Browse the repository at this point in the history
Use json.dumps instead of pprint
  • Loading branch information
zerc committed Jan 15, 2016
2 parents f423288 + cb9f553 commit c5bafd9
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions pyuploadcare/ucare_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import time
import argparse
import logging
import pprint
import os
import sys
import re
import json
from math import ceil

import requests
Expand All @@ -19,7 +19,10 @@
from .exceptions import UploadcareException, TimeoutError, UploadError


pp = pprint.PrettyPrinter(indent=2)
def pprint(value):
print(json.dumps(value, indent=2))


logger = logging.getLogger('pyuploadcare')
str_settings = (
'pub_key',
Expand Down Expand Up @@ -52,11 +55,11 @@ def list_files(arg_namespace):
request_limit=arg_namespace.request_limit,
)
files.constructor = lambda x: x
pp.pprint(list(files))
pprint(list(files))


def get_file(arg_namespace):
pp.pprint(File(arg_namespace.path).info())
pprint(File(arg_namespace.path).info())


def store_files(arg_namespace):
Expand Down Expand Up @@ -86,28 +89,28 @@ def _wait_if_needed(arg_namespace, check_func, error_msg):

def _check_upload_args(arg_namespace):
if not conf.secret and (arg_namespace.store or arg_namespace.info):
pp.pprint('Cannot store or get info without "--secret" key')
pprint('Cannot store or get info without "--secret" key')
return False
return True


def _handle_uploaded_file(file_, arg_namespace):
if arg_namespace.store:
file_.store()
pp.pprint('File stored successfully.')
pprint('File stored successfully.')

if arg_namespace.info:
pp.pprint(file_.info())
pprint(file_.info())

if arg_namespace.cdnurl:
pp.pprint('CDN url: {0}'.format(file_.cdn_url))
pprint('CDN url: {0}'.format(file_.cdn_url))


def upload_from_url(arg_namespace):
if not _check_upload_args(arg_namespace):
return
file_from_url = File.upload_from_url(arg_namespace.url)
pp.pprint(file_from_url)
pprint(file_from_url)

if arg_namespace.wait or arg_namespace.store:
timeout = arg_namespace.timeout
Expand All @@ -118,7 +121,8 @@ def upload_from_url(arg_namespace):
break
if status in ('failed', 'error'):
raise UploadError(
'could not upload file from url: {0}'.format(file_from_url.info())
'could not upload file from url: {0}'.format(
file_from_url.info())
)
time.sleep(1)
else:
Expand All @@ -127,7 +131,7 @@ def upload_from_url(arg_namespace):
if arg_namespace.store or arg_namespace.info:
file_ = file_from_url.get_file()
if file_ is None:
pp.pprint('Cannot store or get info.')
pprint('Cannot store or get info.')
return

_handle_uploaded_file(file_, arg_namespace)
Expand All @@ -144,7 +148,7 @@ def upload(arg_namespace):
def create_group(arg_namespace):
files = [File(uuid) for uuid in arg_namespace.paths]
group = FileGroup.create(files)
pp.pprint(group)
pprint(group.info())


def sync_files(arg_namespace):
Expand All @@ -166,7 +170,7 @@ def sync_files(arg_namespace):
os.makedirs(dirname)

if os.path.exists(local_filepath) and not arg_namespace.replace:
pp.pprint(
pprint(
'File `{0}` already exists. '
'To override it use `--replace` option'.format(
local_filepath))
Expand All @@ -178,8 +182,8 @@ def sync_files(arg_namespace):
try:
response.raise_for_status()
except requests.HTTPError as e:
pp.pprint(('Can\'t download file: `{0}`. '
'Origin error: {1}').format(url, e))
pprint(('Can\'t download file: `{0}`. '
'Origin error: {1}').format(url, e))
continue

save_file_locally(local_filepath, response, f.size())
Expand Down Expand Up @@ -482,7 +486,7 @@ def main(arg_namespace=None,
try:
arg_namespace.func(arg_namespace)
except UploadcareException as exc:
pp.pprint('ERROR: {0}'.format(exc))
pprint('ERROR: {0}'.format(exc))


if __name__ == '__main__':
Expand Down

0 comments on commit c5bafd9

Please sign in to comment.