-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
42 lines (35 loc) · 799 Bytes
/
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
"""Utilities for console outputs."""
import errno
import os
color2num = dict(
gray=30,
red=31,
green=32,
yellow=33,
blue=34,
magenta=35,
cyan=36,
white=37,
crimson=38,
)
def colorize(string, color, bold=False, highlight=False):
"""Colorize the string for console output."""
attr = []
num = color2num[color]
if highlight:
num += 10
attr.append(str(num))
if bold:
attr.append("1")
return "\x1b[%sm%s\x1b[0m" % (";".join(attr), string)
def mkdir_p(path):
"""Create a directory with path."""
if not path:
return
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise