7
7
import winreg
8
8
9
9
_registry = winreg .ConnectRegistry (None , winreg .HKEY_LOCAL_MACHINE )
10
- _key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_ALL_ACCESS )
10
+
11
+ try :
12
+ _write_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_WRITE )
13
+ _read_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_READ )
14
+ except PermissionError :
15
+ raise PermissionError ("Permission is denied to read and/or write to Session Manager key." )
16
+
11
17
12
18
def __get_current_values ():
13
19
"""Get Values.
@@ -22,8 +28,8 @@ def __get_current_values():
22
28
i = 0
23
29
while True :
24
30
try :
25
- if winreg .EnumValue (_key ,i )[0 ] == "PendingFileRenameOperations" :
26
- file_ops_values = winreg .EnumValue (_key ,i )[1 ]
31
+ if winreg .EnumValue (_read_key ,i )[0 ] == "PendingFileRenameOperations" :
32
+ file_ops_values = winreg .EnumValue (_read_key ,i )[1 ]
27
33
break
28
34
except Exception :
29
35
break
@@ -42,7 +48,7 @@ def __set_registry(values):
42
48
values (str[]): List of strings to write to PendingFileRenameOperations key.
43
49
44
50
"""
45
- winreg .SetValueEx (_key , "PendingFileRenameOperations" , 0 , winreg .REG_MULTI_SZ , values )
51
+ winreg .SetValueEx (_write_key , "PendingFileRenameOperations" , 0 , winreg .REG_MULTI_SZ , values )
46
52
47
53
48
54
def DeleteFile (file_path ):
@@ -132,6 +138,27 @@ def PrintFileOperations():
132
138
print ("Moving {} to {}" .format (i [0 ], i [1 ]))
133
139
134
140
141
+ def RemoveFileOperation (file_op_index ):
142
+ """Remove File Operation from Occuring.
143
+
144
+ Args:
145
+ file_op_index (int): Index of file operation to remove. Same indexes as GetFileOperations().
146
+
147
+ Raises:
148
+ TypeError: file_op_index isn't an integer.
149
+ IndexError: The passed in index doesn't exist.
150
+
151
+ """
152
+ values = __get_current_values ()
153
+ if not isinstance (file_op_index , int ):
154
+ raise TypeError ("Index for operation to remove must be an integer!" )
155
+ try :
156
+ del values [file_op_index * 2 :file_op_index * 2 + 2 ]
157
+ except IndexError :
158
+ raise IndexError ("Index {} does not exist!" .format (str (file_op_index ))) # Re-raising here to be more descriptive for debugging
159
+ __set_registry (values )
160
+
161
+
135
162
if __name__ == "__main__" :
136
163
print ("Currently pending file operations: " )
137
164
PrintFileOperations ()
0 commit comments