Commit 615fe212 authored by nimrod's avatar nimrod
Browse files

Add the readfile function.

parent cc359627
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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`.
+18 −0
Original line number Diff line number Diff line
@@ -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()