Skip to content

Commit

Permalink
mapper: Gate the file renaming behaviour behind rename=True
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Babej committed Jun 14, 2023
1 parent e787127 commit 26e5de9
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions datafiles/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,20 @@ def save(self, *, include_default_values: Trilean = None, _log=True) -> None:
self._root.save(include_default_values=include_default_values, _log=_log)
return

# Determine whether the attributes that are involved in the path were changed
# Determine whether the expected filepath of the file has changed (which
# happens as a result of modifying attributes that compose the filename).
# Note that this behaviour is gated behind rename=True flag.
file_rename_required = False
original_path = self.path
with hooks.disabled(): # hooks have to be disabled to prevent infinite loop
if "path" in self.__dict__:
del self.__dict__["path"] # invalidate the cached property

# This call of self.path updates the value since the cache is invalidated
if self.path != original_path:
file_rename_required = True
if self._rename:
with hooks.disabled(): # hooks have to be disabled to prevent infinite loop
if "path" in self.__dict__:
del self.__dict__["path"] # invalidate the cached property

# This call of self.path updates the value since the cache is invalidated
if self.path != original_path:
file_rename_required = True

if self.path:
if self.exists and self._frozen:
Expand All @@ -292,7 +296,7 @@ def save(self, *, include_default_values: Trilean = None, _log=True) -> None:
text = self._get_text(include_default_values=include_default_values)

write(self.path, text, display=True)
if file_rename_required:
if self._rename and file_rename_required:
remove(original_path)

self.modified = False
Expand Down

0 comments on commit 26e5de9

Please sign in to comment.