Skip to content

Commit

Permalink
Fixed bug where verbose output was printed to stdout rather than stderr.
Browse files Browse the repository at this point in the history
  • Loading branch information
woodb committed Jun 6, 2014
1 parent b55c67f commit b74e503
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 26 additions & 7 deletions rural
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ rural
Simple command line utility for uploading local files to Amazon Web Services
(AWS) Simple Storage Service (S3).
"""
import sys
import logging

import click
from xerox import copy
from boto.s3.connection import S3Connection
Expand Down Expand Up @@ -60,7 +63,6 @@ class RuralSession(object):
def _cb_progressbar(uploaded_bytes, total_bytes):
"""Callback function that outputs a progressbar to stderr based on what's
up"""
import sys
pbw = 80 # progress bar width
progress_percent = float(uploaded_bytes) / float(total_bytes)
sys.stderr.write("\r[%s%s] %d%%" % (
Expand All @@ -70,6 +72,20 @@ def _cb_progressbar(uploaded_bytes, total_bytes):
if progress_percent == 1:
sys.stderr.write("\n")

def initialize(verbosity=0):
"""Setup loggers, basically """
global log
log = logging.Logger("rural")
log.verbosity = verbosity
log.stream = sys.stderr
log_handler = logging.StreamHandler(log.stream)
log.addHandler(log_handler)
if verbosity == 3:
log.setLevel(logging.DEBUG)
elif verbosity == 2:
log.setLevel(logging.INFO)
else:
log.setLevel(logging.ERROR)

@click.command()
@click.option('--aws-access-key-id', envvar='AWS_ACCESS_KEY_ID',
Expand All @@ -79,9 +95,10 @@ def _cb_progressbar(uploaded_bytes, total_bytes):
@click.option('--bucket-name', '-b', envvar='RURAL_BUCKET_NAME',
help='bucket name to which to upload/link files', required=True)
@click.option('--verbose', '-v', count=True)
@click.option('--silent/--loud', '-s', help='disable printing of URL', default=False)
@click.argument('filename', required=True)
def upload_copy(aws_access_key_id, aws_secret_access_key, bucket_name,
verbose, filename):
verbose, silent, filename):
"""
Uploads a local file to AWS S3 and copy a publicly accessible link to the
file to the clipboard.
Expand All @@ -93,21 +110,23 @@ def upload_copy(aws_access_key_id, aws_secret_access_key, bucket_name,
RURAL_BUCKET_NAME
"""
initialize(verbose)

cb = None
if verbose > 0:
cb = _cb_progressbar
if verbose > 1:
print("AWS Access Key ID:\t{}".format(aws_access_key_id))
print("AWS Secret Access Key:\t{}".format(aws_secret_access_key))
print("Bucket:\t{}".format(bucket_name))
log.debug("AWS Access Key ID:\t{}".format(aws_access_key_id))
log.debug("AWS Secret Access Key:\t{}".format(aws_secret_access_key))
log.debug("Bucket:\t{}".format(bucket_name))
rural_session = RuralSession(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
bucket_name=bucket_name)
rural_session.upload(filename, cb=cb)
rural_session.publicize()
copy(rural_session.url)
print rural_session.url
if not silent:
print rural_session.url

if __name__ == '__main__':
upload_copy()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__copyright__ = "Copyright 2014, Brandon Wood"
__license__ = "BSD"

__version__ = "0.0.2"
__version__ = "0.0.3"
__maintainer__ = "Brandon Wood"
__email__ = "[email protected]"
__status__ = "Development"
Expand Down

0 comments on commit b74e503

Please sign in to comment.