Skip to content

Commit

Permalink
Made renaming check for target files obligatory fot NT and Unix
Browse files Browse the repository at this point in the history
  • Loading branch information
matfax committed Oct 14, 2019
1 parent e26a218 commit 43b1ec0
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions mutapath/immutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,21 @@ def mutate(self):
def _op_context(self, name: str, op: Callable):
self.__mutable = mutapath.MutaPath(self)
yield self.__mutable
target = self._contained
next_name = getattr(self.__mutable, "_contained")
current_file = self._contained
target_file = getattr(self.__mutable, "_contained")
try:
target = op(target, next_name)
current_file = op(current_file, target_file)
except FileExistsError as e:
raise PathException(f"{name.capitalize()} to {target.normpath()} failed because the file already exists. "
raise PathException(
f"{name.capitalize()} to {current_file.normpath()} failed because the file already exists. "
f"Falling back to original value {self._contained}.") from e
else:
if not target.exists():
raise PathException(f"{name.capitalize()} to {target.normpath()} failed because can not be found. "
if not current_file.exists():
raise PathException(
f"{name.capitalize()} to {current_file.normpath()} failed because can not be found. "
f"Falling back to original value {self._contained}.")

self._contained = target
self._contained = current_file

def renaming(self):
"""
Expand All @@ -309,7 +311,14 @@ def renaming(self):
Path('/home/doe/folder/b.txt')
"""
return self._op_context("Renaming", path.Path.rename)

def checked_rename(cls: path.Path, target: path.Path):
if target.exists():
raise FileExistsError(f"{target.name} already exists.")
else:
return cls.rename(target)

return self._op_context("Renaming", checked_rename)

def moving(self):
"""
Expand Down

0 comments on commit 43b1ec0

Please sign in to comment.