Skip to content

Commit

Permalink
Adding progress-bar hotness (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwatts15 committed Jun 16, 2018
1 parent b4f0ee0 commit 63b6005
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
22 changes: 13 additions & 9 deletions PyOpenWorm/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,19 @@ def clone(self, url=None, update_existing_config=False):
#
# 9) and, finally, print some summary stats about the newly created
# database like how many triples, contexts, total size downloaded, etc.
from tqdm import tqdm
try:
makedirs(self.powdir)
print('Cloning...', file=sys.stderr)
self.repository_provider.clone(url, base=self.powdir)
with tqdm(file=sys.stderr) as progress:
self.repository_provider.clone(url, base=self.powdir, progress=progress)
if not exists(self.config_file):
self._init_config_file()
self._init_store()
print('Deserializing...', file=sys.stderr)
self._load_all_graphs()
except Exception as e:
print('Done!', file=sys.stderr)
except BaseException as e:
self._ensure_no_powdir()
raise e

Expand All @@ -256,15 +259,16 @@ def _load_all_graphs(self):
idx_fname = pth_join(self.powdir, 'graphs', 'index')
if exists(idx_fname):
dest = self._conf()['rdf.graph']
with transaction.manager:
with open(idx_fname) as index_file:
cnt = 0
with open(idx_fname) as index_file:
cnt = 0
for l in index_file:
cnt += 1
index_file.seek(0)
with tqdm(total=cnt) as progress:
for l in index_file:
cnt += 1
index_file.seek(0)
with tqdm(total=cnt) as progress:
for l in index_file:
with transaction.manager:
fname, ctx = l.strip().split(' ')
progress.write(ctx)
with _BatchAddGraph(dest.get_context(ctx)) as g:
g.parse(pth_join(self.powdir, 'graphs', fname), format='nt')
progress.update(1)
Expand Down
25 changes: 23 additions & 2 deletions PyOpenWorm/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,30 @@ def repo(self):
self._repo = Repo(self.base)
return self._repo

def clone(self, url, base):
Repo.clone_from(url, base)
def clone(self, url, base, progress=None):
if progress is not None:
progress = _CloneProgress(progress)
Repo.clone_from(url, base, progress=progress)

@property
def is_dirty(self):
return self.repo().is_dirty()


class _CloneProgress(object):

def __init__(self, progress_reporter):
self.pr = progress_reporter
try:
self.pr.unit = 'objects'
except AttributeError:
pass

self._opcode = 0

def __call__(self, op_code, cur_count, max_count=None, message=''):
if op_code != self._opcode:
self.pr.n = 0
if max_count is not None:
self.pr.total = max_count
self.pr.update(cur_count - self.pr.n)

0 comments on commit 63b6005

Please sign in to comment.