Skip to content

Commit

Permalink
Use a method to populate lintable content cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Feb 15, 2022
1 parent 9482195 commit 2909b21
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,29 @@ def get(self, key: Any, default: Any = None) -> Any:
def content(self) -> str:
"""Retrieve file content, from internal cache or disk."""
if self._content is None:
with open(self.path.resolve(), mode="r", encoding="utf-8") as f:
self._content = f.read()
if self._original_content is None:
self._original_content = self._content
self._populate_content_cache_from_disk()
return self._content

def _populate_content_cache_from_disk(self) -> None:
with open(self.path.resolve(), mode="r", encoding="utf-8") as f:
self._content = f.read()
if self._original_content is None:
self._original_content = self._content

@content.setter
def content(self, value: str) -> None:
"""Update ``content`` and calculate ``updated``."""
"""Update ``content`` and calculate ``updated``.
To calculate ``updated`` this will read the file from disk if the cache
has not already been populated.
"""
if not isinstance(value, str):
raise TypeError(f"Expected str but got {type(value)}")
if self._original_content is None:
if self._content is not None:
self._original_content = self._content
elif self.path.exists():
# use self.content to populate self._original_content
_ = self.content
self._populate_content_cache_from_disk()
else:
# new file
self._original_content = ""
Expand Down

0 comments on commit 2909b21

Please sign in to comment.