From 2909b21f844ab57096b2addad225e8b3a1d59be1 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Tue, 15 Feb 2022 12:12:25 -0600 Subject: [PATCH] Use a method to populate lintable content cache --- src/ansiblelint/file_utils.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index 47026c8cdee..044dcd486d4 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -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 = ""