-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathManager.py
165 lines (119 loc) · 3.61 KB
/
PathManager.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
WINDOWS PATH ENTRIES MANAGER
@author: Marius Munthe-Kaas
@email: [email protected]
"""
import os
import sys
import winreg
class PathSettings:
""" Path-settings class
Contains all neccessary methods to append, remove and list path entries
"""
def __init__(self):
self.checkOs()
self.openPathString()
self.makePathList()
self.pathList.pop()
def checkOs(self):
""" Check if os is win. If not terminate """
if not(sys.platform.startswith("win")):
print("*** ERROR ***\n Operating system not supported (win-only)\n Exits...")
sys.exit()
def openPathString(self):
""" Make a path string from windows registry entry
Raise: WindowsError if the key cannot be opened """
try:
t = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",0, winreg.KEY_READ)
s = winreg.QueryValueEx(t, "Path")
self.pathString = s[0]
except WindowsError as e:
print("Windows error {}".format(e))
def makePathList(self):
""" Make a path list from the path string """
self.pathList = self.pathString.split(";")
def listAll(self):
""" List all path entries """
for count,pathVar in enumerate(self.pathList):
print("{}: {}".format(count, pathVar))
def append(self):
""" Append a new path entry to the list """
readLine = input("Type a path entry")
if(len(readLine) > 0):
self.pathList.append(readLine)
def remove(self):
""" Remove a path entry from the list """
self.listAll()
read = self.readNumber("Choose the number of which the lines to be delete from")
num = int(read)
while num > len(self.pathList) or num < 0:
num = self.readNumber("Error. Type a valid number")
self.pathList.pop(num)
self.savePath()
def commandLoop(self):
""" Print the user command loop """
self.printMenu()
num = self.readNumber("\nChoose a menu option (5 to exit)")
if(num == "5"):
return False
self.initiateAction(num)
def readNumber(self, msg):
""" Read a number. Don't stop until a valid number has been chosen """
sentinel = False
while sentinel != True:
command = input("{}:\n".format(msg))
if command.isdigit():
sentinel = True
return command
def initiateAction(self, actionInt):
""" Call the correct method based on user input
Keyword arguments:
actionInt -- Number read from readNumber()
"""
if actionInt == "1":
self.listAll()
elif actionInt == "2":
self.append()
elif actionInt == "3":
self.remove()
elif actionInt == "4":
self.savePath()
elif actionInt == "5":
return False
def printMenu(self):
""" Print the menu """
print ("\nMenu:")
print ("1: List all path entries")
print ("2: Add new path entry")
print ("3: Delete path entry")
print ("4: Save path entries")
print ("5. EXIT\n")
def generatePathString(self):
""" Make a new path string based on the list """
pathString = ""
for pathVar in self.pathList:
pathString += pathVar + ";"
self.pathString = pathString
def savePath(self):
""" Save the new path string to the registry
Raise:
WindowsError -- If it cannot be saved
"""
self.generatePathString()
print(self.pathString)
try:
t = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",0, winreg.KEY_WRITE)
winreg.SetValueEx(t, "Path", 0, winreg.REG_SZ, "{}".format(self.pathString))
except WindowsError as e:
print("Windows error" + e)
"""
Run the program
"""
var = PathSettings()
print ("\n**************************")
print ("** Manage path-settings **")
print ("**************************")
while True:
choose = var.commandLoop()
if choose == False:
break