Skip to content

Commit

Permalink
better content download support
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Apr 17, 2018
1 parent c23cb93 commit d024589
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion examples/http/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import appier_console

BIG_BUCK_URL = "http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_1080p_h264.mov"
BIG_BUCK_NAME = "big_buck_bunny_1080p_h264.mov"

url = sys.argv[1] if len(sys.argv) > 1 else BIG_BUCK_URL
name = os.path.basename(appier.legacy.urlparse(url).path)
Expand All @@ -58,10 +59,11 @@ def copy(input, name, buffer_size = 16384):
finally:
output.close()

with appier_console.ctx_http_callbacks("big_buck_bunny_1080p_h264.mov") as callbacks:
with appier_console.ctx_http_callbacks(BIG_BUCK_NAME) as callbacks:
contents, _response = appier.get(
url,
handle = True,
silent = True,
redirect = True,
retry = 0,
use_file = True,
Expand Down
20 changes: 18 additions & 2 deletions src/appier_console/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(
color = None,
template = "{{spinner}}",
stream = sys.stdout,
end_newline = False,
*args, **kwargs
):
threading.Thread.__init__(self, *args, **kwargs)
Expand All @@ -111,6 +112,7 @@ def __init__(
self.color = color
self.template = template
self.stream = stream
self.end_newline = end_newline

def run(self):
threading.Thread.run(self)
Expand All @@ -130,20 +132,34 @@ def run(self):
index = 0
is_first = True

while self.running:
while True:
value = index % len(frames)
if is_first: is_first = False
else: self.stream.write(CLEAR_LINE + "\r")
replacer = frames[value]
if color: replacer = color + replacer + COLOR_RESET

template = appier.legacy.str(self.template)
label = template.replace("{{spinner}}", replacer)

# writes the current label (text) to the output stream
# and runs the flush operation (required to ensure that
# the data contents are properly set in the stream)
self.stream.write(label)
self.stream.flush()

# in case the running flag is not longer set breaks
# the current loop (nothing remaining to be done)
if not self.running: break

# sleeps for the amount of time in the interval and
# then increments the current loop cycle index
time.sleep(interval)
index += 1

self.stream.write(CLEAR_LINE + "\r")
if self.end_newline: self.stream.write("\n")
else: self.stream.write(CLEAR_LINE + "\r")

self.stream.flush()

def stop(self):
Expand Down
9 changes: 5 additions & 4 deletions src/appier_console/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@
def ctx_http_callbacks(
name,
console_threshold = CONSOLE_THRESHOLD,
text_threshold = TEXT_THRESHOLD
text_threshold = TEXT_THRESHOLD,
end_newline = True
):

with base.ctx_loader() as loader:
with base.ctx_loader(end_newline = end_newline) as loader:

status = dict(
length = -1,
Expand All @@ -70,10 +71,10 @@ def ctx_http_callbacks(
)

def callback_init(connection):
loader.set_template("[%s] Establishing connection " % name)
loader.set_template("{{spinner}} [%s] Establishing connection " % name)

def callback_open(connection):
loader.set_template("[%s] Connection established " % name)
loader.set_template("{{spinner}} [%s] Connection established " % name)

def callback_headers(headers):
_length = headers.get("content-length", None)
Expand Down

0 comments on commit d024589

Please sign in to comment.