Skip to content

Commit 7343d7a

Browse files
committed
Use __main__.py, 1.0.0, conflict checking optional
1 parent b13153a commit 7343d7a

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ To import, use `import movefile_restart`
1212

1313
From there, you have a couple functions at your disposal:
1414

15-
`movefile_restart.DeleteFile(file_path)`: Queues `file_path` for deletion.
15+
`movefile_restart.DeleteFile(file_path, check_conflicts=True)`: Queues `file_path` for deletion.*
1616

17-
`movefile_restart.MoveFile(from_path, to_path)` or `movefile_restart.RenameFile(from_path, to_path)`: Moves the file from `from_path` to `to_path`.
17+
`movefile_restart.MoveFile(from_path, to_path, check_conflicts=True)` or `movefile_restart.RenameFile(from_path, to_path)`: Moves the file from `from_path` to `to_path`.*
1818

1919
`movefile_restart.GetFileOperations()`: Get a list of tuples containing the source and destination of all file movings queued.
2020

2121
`movefile_restart.PrintFileOperations()`: Print a list of file operations that are scheduled to occur during reboot.
2222

23+
`movefile_restart.RemoveFileOperation(file_op_index)`: Remove a file operation based on its index from `movefile_restart.GetFileOperations()`.
24+
2325
`movefile_restart.CheckPermissions()`: Check for read/write permissions to the registry keys needed for this library.
2426

27+
*: For both of these functions, the `check_conflicts` parameter determines whether or not to perform checks when moving/deleting to make sure if it can be performed successfully in the case of a conflict. It only checks if there is initially a problem (the file being deleted doesn't exist, the source of the file being moved doesn't exist, or the destination file of a move already exists). Setting `check_conflicts` to False when calling these functions will skip all of these checks.
28+
2529
## Current Limitations
2630

27-
* Files cannot currently be un-queued.
28-
* Some weird cases such as: if you're moving `a.txt` to `b.txt`, you cannot queue a deletion for `b.txt` after your file move queue. You would have to restart the computer so `b.txt` exists before deleting it.
29-
* Due to using the Windows Registry for handling these kinds of operations, no other operating system is supported, nor is there planned support.
31+
* Due to using the Windows Registry for handling these kinds of operations, no other operating system is supported, nor is there planned support.
32+
* Some cases can occur where a move/delete can fail. For example, queueing a deletion of file A then queueing a move from file A to file B isn't currently checked for, and will result in the move not occuring (as file A will be deleted before it can be moved).

movefile_restart/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .main import DeleteFile, MoveFile, RenameFile, GetFileOperations, PrintFileOperations, RemoveFileOperation, CheckPermissions
22

3-
__version__ = "0.6.0"
3+
__version__ = "1.0.0"

movefile_restart/__main__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sys
2+
3+
from main import CheckPermissions, PrintFileOperations
4+
5+
if CheckPermissions()[0]:
6+
print("Currently pending file operations: ")
7+
PrintFileOperations()
8+
sys.exit(0)
9+
else:
10+
print("No read permission on registry key!")
11+
sys.exit(1)

movefile_restart/main.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,26 @@ def __set_registry(values):
5252
winreg.SetValueEx(_write_key, "PendingFileRenameOperations", 0, winreg.REG_MULTI_SZ, values)
5353

5454

55-
def DeleteFile(file_path):
55+
def DeleteFile(file_path, check_conflicts=True):
5656
"""Queue File for Deletion.
5757
5858
Adds the Registry information to delete a file on reboot.
5959
6060
Args:
6161
file_path (str): A path to the file to delete.
62+
check_conflicts (bool): Checks file_path to make sure the Delete can happen as supplied. Defaults to True.
6263
6364
Raises:
6465
FileNotFoundError: Raised if the file_path doesn't exist.
6566
6667
"""
6768
file_path = file_path.replace("/", "\\")
6869
values = __get_current_values()
69-
if not (os.path.isfile(file_path)):
70+
if check_conflicts and not (os.path.isfile(file_path)):
7071
values.reverse()
7172
try:
7273
file_path_index = values.index("\\??\\" + file_path)
73-
except IndexError:
74+
except ValueError:
7475
file_path_index = -1
7576
if file_path_index % 2 != 0 or file_path_index == -1:
7677
raise FileNotFoundError("Path {} does not exist and is not being created during a move operation!".format(file_path))
@@ -80,28 +81,29 @@ def DeleteFile(file_path):
8081
__set_registry(values)
8182

8283

83-
def MoveFile(from_path, to_path):
84+
def MoveFile(from_path, to_path, check_conflicts=True):
8485
"""Queue File for Moving.
8586
8687
Adds the Registry information to move a file on reboot.
8788
8889
Args:
8990
from_path (str): The directory being moved from.
9091
to_path (str): The directory being moved to.
92+
check_conflicts (bool): Check from_path and to_path to make sure the Move/Rename can be performed successfully.
9193
9294
Raises:
9395
FileNotFoundError: Raised if the from_path doesn't exist or if the directory of to_path doesn't exist.
9496
FileExistsError: Raised if to_path already exists.
9597
9698
"""
9799
from_path = from_path.replace("/", "\\")
98-
if not os.path.isfile(from_path): # Don't move non-existant path
100+
if check_conflicts and not os.path.isfile(from_path): # Don't move non-existant path
99101
raise FileNotFoundError("Path {} does not exist!".format(from_path))
100102
to_path = to_path.replace("/", "\\")
101-
if not os.path.isdir(os.path.dirname(to_path)): # Don't move to non-existant dir
103+
if check_conflicts and not os.path.isdir(os.path.dirname(to_path)): # Don't move to non-existant dir
102104
raise FileNotFoundError("Path {} does not exist to move to!".format(os.path.dirname(to_path)))
103105
values = __get_current_values()
104-
if os.path.isfile(to_path): # Don't move to already-existing destination unless it will be deleted/moved
106+
if check_conflicts and os.path.isfile(to_path): # Don't move to already-existing destination unless it will be deleted/moved
105107
values.reverse()
106108
try:
107109
to_path_index = values.index("\\??\\" + to_path)
@@ -115,9 +117,9 @@ def MoveFile(from_path, to_path):
115117
__set_registry(values)
116118

117119

118-
def RenameFile(from_path, to_path):
120+
def RenameFile(from_path, to_path, check_conflicts=True):
119121
"""MoveFile Alias."""
120-
MoveFile(from_path, to_path)
122+
MoveFile(from_path, to_path, check_conflicts)
121123

122124

123125
def GetFileOperations():
@@ -139,6 +141,9 @@ def GetFileOperations():
139141
def PrintFileOperations():
140142
"""Prints Pending File Operations."""
141143
vals = GetFileOperations()
144+
if not vals:
145+
print("There are no currently pending file operations!")
146+
return
142147
for i in vals:
143148
if i[1] == "":
144149
print("Deleting {}".format(i[0]))

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.6.0",
8+
version="1.0.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)