Skip to content

Commit 6b5243f

Browse files
committed
See comments
1 parent be9d198 commit 6b5243f

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

movefile_restart/movefile_restart.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88

99
_registry = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
1010

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-
17-
1811
def __get_current_values():
1912
"""Get Values.
2013
@@ -24,6 +17,10 @@ def __get_current_values():
2417
str[]: List of strings in PendingFileRenameOperations
2518
2619
"""
20+
try:
21+
_read_key = winreg.OpenKey(_registry, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", 0, winreg.KEY_READ)
22+
except PermissionError:
23+
raise PermissionError("Permission Denied to read registry key.") # Re-raise to make clear to end-user/library user
2724
file_ops_values = None
2825
i = 0
2926
while True:
@@ -48,6 +45,10 @@ def __set_registry(values):
4845
values (str[]): List of strings to write to PendingFileRenameOperations key.
4946
5047
"""
48+
try:
49+
_write_key = winreg.OpenKey(_registry, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", 0, winreg.KEY_WRITE)
50+
except PermissionError:
51+
raise PermissionError("Permission Denied to write registry key.")
5152
winreg.SetValueEx(_write_key, "PendingFileRenameOperations", 0, winreg.REG_MULTI_SZ, values)
5253

5354

@@ -64,9 +65,16 @@ def DeleteFile(file_path):
6465
6566
"""
6667
file_path = file_path.replace("/", "\\")
67-
if not (os.path.isfile(file_path)):
68-
raise FileNotFoundError("Path {} does not exist!".format(file_path))
6968
values = __get_current_values()
69+
if not (os.path.isfile(file_path)):
70+
values.reverse()
71+
try:
72+
file_path_index = values.index("\\??\\" + file_path)
73+
except IndexError:
74+
file_path_index = -1
75+
if file_path_index % 2 != 0 or file_path_index == -1:
76+
raise FileNotFoundError("Path {} does not exist and is not being created during a move operation!".format(file_path))
77+
values.reverse()
7078
values.append("\\??\\" + file_path)
7179
values.append("")
7280
__set_registry(values)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="movefile-restart",
8-
version="0.3.0",
8+
version="0.4.0",
99
author="hammy3502",
1010
author_email="hammy275@gmail.com",
1111
description="A small library for Windows to queue files to be moved, deleted, or renamed on reboot.",

0 commit comments

Comments
 (0)