Skip to content
Snippets Groups Projects
Commit 615fe212 authored by nimrod's avatar nimrod
Browse files

Add the readfile function.

parent cc359627
No related branches found
No related tags found
No related merge requests found
Pipeline #2054 canceled
......@@ -117,6 +117,7 @@ Jinja functions
- :code:`run`: Runs a command and returns the stdout, stderr and returncode
using run_. This function replaces the :code:`run` filter.
- :code:`readfile`: Returns the contents of a file.
Example usage can be seen in :code:`tests` and for specific filters in the
docstrings in :code:`template/functions.py`.
......
......@@ -39,3 +39,21 @@ def run(*argv, **kwargs):
proc["stdout"] = proc["stdout"].decode()
proc["stderr"] = proc["stderr"].decode()
return proc
def readfile(path):
"""
Opens a file, returns the contents.
>>> readfile("/dev/null")
''
>>> foo = "foo"
>>> with open("/tmp/foo", "w") as f:
... f.write(foo)
3
>>> foo == readfile("/tmp/foo")
True
"""
with open(path, "r") as f: # pylint: disable=invalid-name
return f.read()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment