Skip to content

Commit 6bb1e08

Browse files
Merge pull request #939 from linsword13/render
Cache template file read
2 parents f80af90 + a84c52d commit 6bb1e08

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/ramble/ramble/application.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,8 +2471,7 @@ def _render_object_templates(self, extra_vars_origin, workspace):
24712471
for obj, tpl_config in self._object_templates(workspace):
24722472
extra_vars = extra_vars_origin.copy()
24732473
src_path = tpl_config["src_path"]
2474-
with open(src_path) as f_in:
2475-
content = f_in.read()
2474+
content = workspace.read_file_content(src_path)
24762475
extra_vars_dict = tpl_config.get("extra_vars")
24772476
if extra_vars_dict is not None:
24782477
extra_vars.update(extra_vars_dict)

lib/ramble/ramble/test/workspace_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,16 @@ def test_re_read(tmpdir):
3030
test_workspace = ramble.workspace.Workspace(os.getcwd(), True)
3131
test_workspace.clear()
3232
test_workspace._re_read()
33+
34+
35+
def test_read_file_content(tmpdir):
36+
with tmpdir.as_cwd():
37+
test_workspace = ramble.workspace.Workspace(os.getcwd(), True)
38+
fname = "test.tpl"
39+
with open(fname, "w+") as f:
40+
f.write("test content read")
41+
assert test_workspace.read_file_content(fname) == "test content read"
42+
with open(fname, "w") as f:
43+
f.write("test content read modified")
44+
# The read is cached and does not reflect the latest content
45+
assert test_workspace.read_file_content(fname) == "test content read"

lib/ramble/ramble/workspace/workspace.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,10 @@ def __init__(self, root, dry_run=False, read_default_template=True):
464464
# This can be re-used by all experiments of the workspace.
465465
self.pkg_path_cache = defaultdict(dict)
466466

467+
# A simple dict mapping a file's src_path to its content.
468+
# This is currently used as a cache for reading per-object template contents.
469+
self._inmem_file_cache = {}
470+
467471
self.results = self.default_results()
468472

469473
self.success_list = ramble.success_criteria.ScopedCriteriaList()
@@ -1788,6 +1792,19 @@ def date_string(self):
17881792
now = datetime.datetime.now()
17891793
return now.strftime("%Y-%m-%d_%H.%M.%S")
17901794

1795+
def read_file_content(self, file_path):
1796+
"""Read and cache the file content
1797+
1798+
This should only be used on files that are not modified during the lifetime of the
1799+
workspace command execution.
1800+
"""
1801+
if file_path in self._inmem_file_cache:
1802+
return self._inmem_file_cache[file_path]
1803+
with open(file_path) as f:
1804+
content = f.read()
1805+
self._inmem_file_cache[file_path] = content
1806+
return content
1807+
17911808
@property
17921809
def internal(self):
17931810
"""Whether this workspace is managed by Ramble."""

0 commit comments

Comments
 (0)