Skip to content

Commit

Permalink
Merge pull request #31 from srirams6/master
Browse files Browse the repository at this point in the history
Better printing and PEP8.
  • Loading branch information
sananth12 committed Mar 6, 2015
2 parents f3b447a + 34c17f2 commit 14f458b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
21 changes: 13 additions & 8 deletions image_scraper/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
class DirectoryAccessError(Exception):
pass
pass


class DirectoryCreateError(Exception):
pass
pass


class ImageDownloadError(Exception):
status_code=0
def __init__(self, status_code=0):
self.status_code = status_code
status_code = 0

def __init__(self, status_code=0):
self.status_code = status_code


class ImageSizeError(Exception):
image_size=0
def __init__(self, image_size):
self.image_size = image_size
image_size = 0

def __init__(self, image_size):
self.image_size = image_size
4 changes: 2 additions & 2 deletions image_scraper/mains.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def console_main():
if no_to_download == 0:
no_to_download = len(images)

print "Found %s images: " % len(images)
print "Found {0} images: ".format(len(images))

try:
process_download_path(download_path)
Expand Down Expand Up @@ -56,7 +56,7 @@ def console_main():
break

pbar.finish()
print "\nDone!\nDownloaded %s images" % (count-failed-over_max_filesize)
print "\nDone!\nDownloaded {0} images\nFailed: {1}\n".format(count-failed-over_max_filesize, failed)
return


Expand Down
47 changes: 28 additions & 19 deletions image_scraper/progressbar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys, time
import sys
import time
from array import array
try:
from fcntl import ioctl
Expand All @@ -7,6 +8,7 @@
pass
import signal


class ProgressBarWidget(object):
"""This is an element of ProgressBar formatting.
Expand All @@ -24,6 +26,7 @@ def update(self, pbar):
At least this function must be overriden."""
pass


class ProgressBarWidgetHFill(object):
"""This is a variable width element of ProgressBar formatting.
Expand All @@ -49,6 +52,7 @@ class ETA(ProgressBarWidget):
"Widget for the Estimated Time of Arrival"
def format_time(self, seconds):
return time.strftime('%H:%M:%S', time.gmtime(seconds))

def update(self, pbar):
if pbar.currval == 0:
return 'ETA: --:--:--'
Expand All @@ -59,13 +63,15 @@ def update(self, pbar):
eta = elapsed * pbar.maxval / pbar.currval - elapsed
return 'ETA: %s' % self.format_time(eta)


class FileTransferSpeed(ProgressBarWidget):
"Widget for showing the transfer speed (useful for file transfers)."
def __init__(self):
self.fmt = '%6.2f %s'
self.units = ['B','K','M','G','T','P']
self.units = ['B', 'K', 'M', 'G', 'T', 'P']

def update(self, pbar):
if pbar.seconds_elapsed < 2e-6:#== 0:
if pbar.seconds_elapsed < 2e-6: # == 0:
bps = 0.0
else:
bps = float(pbar.currval) / pbar.seconds_elapsed
Expand All @@ -76,33 +82,39 @@ def update(self, pbar):
spd /= 1000
return self.fmt % (spd, u+'/s')


class RotatingMarker(ProgressBarWidget):
"A rotating marker for filling the bar of progress."
def __init__(self, markers='|/-\\'):
self.markers = markers
self.curmark = -1

def update(self, pbar):
if pbar.finished:
return self.markers[0]
self.curmark = (self.curmark + 1)%len(self.markers)
self.curmark = (self.curmark + 1) % len(self.markers)
return self.markers[self.curmark]


class Percentage(ProgressBarWidget):
"Just the percentage done."
def update(self, pbar):
return '%3d%%' % pbar.percentage()


class Bar(ProgressBarWidgetHFill):
"The bar of progress. It will strech to fill the line."
def __init__(self, marker='#', left='|', right='|'):
self.marker = marker
self.left = left
self.right = right

def _format_marker(self, pbar):
if isinstance(self.marker, (str, unicode)):
return self.marker
else:
return self.marker.update(pbar)

def update(self, pbar, width):
percent = pbar.percentage()
cwidth = width - len(self.left) - len(self.right)
Expand All @@ -111,17 +123,20 @@ def update(self, pbar, width):
bar = (self.left + (m*marked_width).ljust(cwidth) + self.right)
return bar


class ReverseBar(Bar):
"The reverse bar of progress, or bar of regress. :)"
def update(self, pbar, width):
percent = pbar.percentage()
cwidth = width - len(self.left) - len(self.right)
marked_width = int(percent * cwidth / 100)
m = self._format_marker(pbar)
bar = (self.left + (m*marked_width).rjust(cwidth) + self.right)
bar = (self.left + (m * marked_width).rjust(cwidth) + self.right)
return bar

default_widgets = [Percentage(), ' ', Bar()]


class ProgressBar(object):
"""This is the ProgressBar class, it updates and prints the bar.
Expand Down Expand Up @@ -161,7 +176,7 @@ def __init__(self, maxval=100, widgets=default_widgets, term_width=None,
self.signal_set = False
if term_width is None:
try:
self.handle_resize(None,None)
self.handle_resize(None, None)
signal.signal(signal.SIGWINCH, self.handle_resize)
self.signal_set = True
except:
Expand All @@ -176,7 +191,7 @@ def __init__(self, maxval=100, widgets=default_widgets, term_width=None,
self.seconds_elapsed = 0

def handle_resize(self, signum, frame):
h,w=array('h', ioctl(self.fd,termios.TIOCGWINSZ,'\0'*8))[:2]
h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2]
self.term_width = w

def percentage(self):
Expand Down Expand Up @@ -245,13 +260,9 @@ def finish(self):
self.update(self.maxval)
if self.signal_set:
signal.signal(signal.SIGWINCH, signal.SIG_DFL)






if __name__=='__main__':
if __name__ == '__main__':
import os

def example1():
Expand All @@ -269,11 +280,11 @@ class CrazyFileTransferSpeed(FileTransferSpeed):
"It's bigger between 45 and 80 percent"
def update(self, pbar):
if 45 < pbar.percentage() < 80:
return 'Bigger Now ' + FileTransferSpeed.update(self,pbar)
return 'Bigger Now ' + FileTransferSpeed.update(self, pbar)
else:
return FileTransferSpeed.update(self,pbar)
return FileTransferSpeed.update(self, pbar)

widgets = [CrazyFileTransferSpeed(),' <<<', Bar(), '>>> ', Percentage(),' ', ETA()]
widgets = [CrazyFileTransferSpeed(), ' <<<', Bar(), '>>> ', Percentage(), ' ', ETA()]
pbar = ProgressBar(widgets=widgets, maxval=10000000)
# maybe do something
pbar.start()
Expand All @@ -294,19 +305,17 @@ def example3():

def example4():
widgets = ['Test: ', Percentage(), ' ',
Bar(marker='0',left='[',right=']'),
Bar(marker='0', left='[', right=']'),
' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=500)
pbar.start()
for i in range(100,500+1,50):
for i in range(100, 500+1, 50):
time.sleep(0.2)
pbar.update(i)
pbar.finish()
print


example1()
example2()
example3()
example4()

1 change: 1 addition & 0 deletions image_scraper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
from exceptions import *


def process_links(links, formats=["jpg", "png", "gif", "svg", "jpeg"]):
x = []
for l in links:
Expand Down

0 comments on commit 14f458b

Please sign in to comment.