forked from ub-nsf-hsi/shasta-ub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
52 lines (39 loc) · 1.12 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
from contextlib import contextmanager
class SkipWith(Exception):
pass
@contextmanager
def skip_run(flag, f):
"""To skip a block of code.
Parameters
----------
flag : str
skip or run.
Returns
-------
None
"""
@contextmanager
def check_active():
deactivated = ["skip"]
p = ColorPrint() # printing options
if flag in deactivated:
p.print_skip("{:>12} {:>2} {:>12}".format("Skipping the block", "|", f))
raise SkipWith()
else:
p.print_run("{:>12} {:>3} {:>12}".format("Running the block", "|", f))
yield
try:
yield check_active
except SkipWith:
pass
class ColorPrint:
@staticmethod
def print_skip(message, end="\n"):
sys.stderr.write("\x1b[88m" + message.strip() + "\x1b[0m" + end)
@staticmethod
def print_run(message, end="\n"):
sys.stdout.write("\x1b[1;32m" + message.strip() + "\x1b[0m" + end)
@staticmethod
def print_warn(message, end="\n"):
sys.stderr.write("\x1b[1;33m" + message.strip() + "\x1b[0m" + end)