-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_path.py
77 lines (60 loc) · 1.87 KB
/
show_path.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
import os
import sys
import argparse
# Can we print in color?
has_colorama = False
try:
import colorama
has_colorama = True
except ImportError:
pass
def nonempty_locations_from(path):
return [location for location in path.split(os.pathsep) if location]
def _print_location(location, is_dir, is_duplicate):
prefix = " "
color = None
if is_duplicate:
prefix = "+ "
if has_colorama:
color = colorama.Fore.CYAN
elif not is_dir:
prefix = "X "
if has_colorama:
color = colorama.Fore.RED
if color:
prefix = color + prefix
print(prefix + location)
def _print_path(path):
locations = nonempty_locations_from(path)
# Initialize colorama for colored output
if has_colorama:
colorama.init(autoreset=True)
# Ignore differences in case for Windows only
ignore_case = sys.platform == 'win32'
printed = set()
for location in locations:
is_dir = os.path.isdir(location)
location_ = location.lower() if ignore_case else location
is_duplicate = location_ in printed
_print_location(location, is_dir, is_duplicate)
printed.add(location_)
def _main():
"""Run the command-line interface."""
desc = "Print the search path in a human-friendly format"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("path_name", nargs="?", default="PATH",
help="variable name of path to show " +
"(default: %(default)s)")
args = parser.parse_args()
path_name = args.path_name
try:
path = os.environ[path_name].strip()
if path:
_print_path(path)
else:
print(f"{path_name} is empty")
except KeyError:
print(f"{path_name} not set")
if __name__ == '__main__':
_main()