77import winreg
88
99_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+
1117
1218def __get_current_values ():
1319 """Get Values.
@@ -22,8 +28,8 @@ def __get_current_values():
2228 i = 0
2329 while True :
2430 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 ]
2733 break
2834 except Exception :
2935 break
@@ -42,7 +48,7 @@ def __set_registry(values):
4248 values (str[]): List of strings to write to PendingFileRenameOperations key.
4349
4450 """
45- winreg .SetValueEx (_key , "PendingFileRenameOperations" , 0 , winreg .REG_MULTI_SZ , values )
51+ winreg .SetValueEx (_write_key , "PendingFileRenameOperations" , 0 , winreg .REG_MULTI_SZ , values )
4652
4753
4854def DeleteFile (file_path ):
@@ -132,6 +138,27 @@ def PrintFileOperations():
132138 print ("Moving {} to {}" .format (i [0 ], i [1 ]))
133139
134140
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+
135162if __name__ == "__main__" :
136163 print ("Currently pending file operations: " )
137164 PrintFileOperations ()
0 commit comments