-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.py
More file actions
124 lines (85 loc) · 2.84 KB
/
path.py
File metadata and controls
124 lines (85 loc) · 2.84 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import globals
import json
from utils import read_file, write_file, colorprint
from typing import Dict
# This is being declared globally in order to prevent the data file being read from disk every single time it is required
pathdict: Dict = {"please_dont_use_this_as_a_segment_it_is_required_by_zconfer": ""}
def init_pathdict():
global pathdict
if pathdict.get('please_dont_use_this_as_a_segment_it_is_required_by_zconfer', 'already_init') != 'already_init':
pathdict = json.loads(read_file(globals.jsondata.path))
def dump_pathdict():
global pathdict
write_file(globals.jsondata.path, json.dumps(pathdict))
def check_segment_exists(seg):
global pathdict
init_pathdict()
if seg.upper() not in pathdict:
print(colorprint("Segment {} was not found in PATH".format(seg), "red"))
return False
return True
def generate():
global pathdict
init_pathdict()
pathstring = "export PATH=$PATH:"
for seg in pathdict:
if pathdict[seg]['enabled']:
pathstring = pathstring + pathdict[seg]['value'] + ":"
write_file(globals.modules.path, pathstring[:-1])
def set_segment(seg, value):
global pathdict
init_pathdict()
seg = seg.upper()
pathdict.setdefault(seg, {'value': "", 'enabled': True})
pathdict[seg]['value'] = value
dump_pathdict()
generate()
print(colorprint(f"Successfully set segment {seg} to '{value}'", "green"))
def get_segment(seg):
global pathdict
init_pathdict()
seg = seg.upper()
if not check_segment_exists(seg):
return
print(pathdict[seg]['value'])
def remove_segment(seg):
global pathdict
init_pathdict()
seg = seg.upper()
if not check_segment_exists(seg):
return
del pathdict[seg]
dump_pathdict()
generate()
print(colorprint(f"Successfully removed segment {seg}", "green"))
def enable_segment(seg):
global pathdict
init_pathdict()
seg = seg.upper()
if not check_segment_exists(seg):
return
pathdict[seg]['enabled'] = True
dump_pathdict()
generate()
print(colorprint(f"Successfully enabled segment {seg}", "green"))
def disable_segment(seg):
global pathdict
init_pathdict()
seg = seg.upper()
if not check_segment_exists(seg):
return
pathdict[seg]['enabled'] = False
dump_pathdict()
generate()
print(colorprint(f"Successfully disabled segment {seg}", "green"))
def view():
global pathdict
init_pathdict()
if len(pathdict) == 0:
print(colorprint("No ZConf-created segments in PATH", "red"))
return
print(colorprint("{:20}{:60}{:15}".format("Segment", "Value", "Enabled"), "bold"))
for seg in pathdict:
print("{:20}{:60}{:15}".format(seg, pathdict.get(seg)['value'], ("YES" if pathdict[seg]['enabled'] else "NO")))
print()