Skip to content

Commit a7a9973

Browse files
committed
Add progress bar
The dumps I'm looking at take close to a minute to process, so I think a progress bar is nice here. Disable with -q. Sem-Ver: feature Signed-off-by: Zack Cerza <[email protected]>
1 parent 6731636 commit a7a9973

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

must_triage/cmd.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from must_triage import formatters
66
from must_triage import inspectors
7+
from must_triage.progress import ProgressBar
78

89

910
def parse_args():
1011
parser = argparse.ArgumentParser()
1112
parser.add_argument('path', type=dir_or_file)
13+
parser.add_argument('-q', '--quiet', action='store_true')
1214
return parser.parse_args()
1315

1416

@@ -23,7 +25,10 @@ def main():
2325
args = parse_args()
2426
interests = dict()
2527
for inspector_cls in inspectors.all():
26-
inspector = inspector_cls(root=args.path)
28+
inspector_args = dict(root=args.path)
29+
if not args.quiet:
30+
inspector_args['progress_class'] = ProgressBar
31+
inspector = inspector_cls(**inspector_args)
2732
inspectors.merge_interests(interests, inspector.inspect())
2833

2934
print(json.dumps(interests, indent=2, default=formatters.json_serialize))

must_triage/inspectors/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from must_triage.progress import NoProgress
2+
3+
4+
class Inspector:
5+
def __init__(self, root, progress_class=NoProgress):
6+
self.root = root
7+
self._progress_class = progress_class
8+
9+
def inspect(self):
10+
raise NotImplementedError

must_triage/inspectors/ocp.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
import must_triage.fs as fs
44
import must_triage.inspectors as inspectors
55

6+
from must_triage.inspectors.base import Inspector
67

7-
class OCP:
8-
def __init__(self, root):
9-
self.root = root
108

9+
class OCP(Inspector):
1110
def inspect(self):
1211
self.interests = dict()
1312
yamls = fs.find(self.root, lambda p: fs.has_ext(p, ['.yaml', '.yml']))
1413
inspectors.merge_interests(self.interests, self.inspect_yamls(yamls))
1514
return self.interests
1615

1716
def inspect_yamls(self, paths):
17+
if not paths:
18+
return dict()
1819
interests = dict()
19-
for path in paths:
20+
for path in self._progress_class("Reading OCP files").iter(paths):
2021
interests[path] = list()
2122
with open(path) as fd:
2223
try:

must_triage/inspectors/ocs.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import must_triage.fs as fs
66
import must_triage.inspectors as inspectors
77

8+
from must_triage.inspectors.base import Inspector
89

9-
class OCS:
10-
def __init__(self, root):
11-
self.root = root
1210

11+
class OCS(Inspector):
1312
def inspect(self):
1413
self.interests = dict()
1514
jsons = fs.find(
@@ -20,8 +19,10 @@ def inspect(self):
2019
return self.interests
2120

2221
def inspect_jsons(self, paths):
22+
if not paths:
23+
return dict()
2324
interests = dict()
24-
for path in paths:
25+
for path in self._progress_class("Reading OCS files").iter(paths):
2526
interests[path] = list()
2627
if os.stat(path).st_size == 0:
2728
interests[path].append("File is empty")

must_triage/progress.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import progress.bar
2+
3+
4+
class NoProgress:
5+
def __init__(self, *args, **kwargs):
6+
pass
7+
8+
def __enter__(self):
9+
return self
10+
11+
def __exit__(self, exc_type, exc_val, exc_tb):
12+
pass
13+
14+
def next(self):
15+
pass
16+
17+
def iter(self, it):
18+
for x in it:
19+
yield x
20+
21+
def finish(self):
22+
pass
23+
24+
25+
class ProgressBar(progress.bar.IncrementalBar):
26+
suffix = "%(index)d/%(max)d %(elapsed_td)s"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
PyYAML
2+
progress

0 commit comments

Comments
 (0)