diff --git a/README.rst b/README.rst index 40472bb92e943670273462e1a26754c2b3510a77..3ad3ab65f0202aaada81c3a850b6cb05f17c15d7 100644 --- a/README.rst +++ b/README.rst @@ -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`. diff --git a/template/functions.py b/template/functions.py index c219b2e1e9258211a6915dbb796963b259d508fe..3d31471e02355858a0293dd7db221f902b6efdf4 100644 --- a/template/functions.py +++ b/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()