Example #1
0
def template(file):
    path = resource_filename(__name__, os.path.join('templates', file))

    if not os.path.isfile(path):
        raise error.Bug("Could not find template {}".format(path))
    content = util.file_read(path)
    return string.Template(content)
Example #2
0
    def template(self, key):
        """get template
        Get a template which was added to the filesystem in the corresponding
        'templates/' directory.

        Example:
        ::

            # path: components/node/attributes/image/templates/disk.xml
            # in image attribute
            self.template("disk.xml")
            < Template('...') >

            # path: components/node/templates/qemu/simple.xml
            # in node component
            self.template("qemu/simple.xml")
            < Template('...') >

        Args:
            key: Key path to template

        Returns:
            The specified template

        Throws:
            error.Bug if template could not found
        """
        if key not in self._templates:
            raise error.Bug("Could not find template {}".format(key))

        content = util.file_read(self._templates[key])
        return string.Template(content)
Example #3
0
File: entity.py Project: xii/xii
    def template(self, key):
        """get template
        Get a template which was added to the filesystem in the corresponding
        'templates/' directory.

        Example:
        ::

            # path: components/node/attributes/image/templates/disk.xml
            # in image attribute
            self.template("disk.xml")
            < Template('...') >

            # path: components/node/templates/qemu/simple.xml
            # in node component
            self.template("qemu/simple.xml")
            < Template('...') >

        Args:
            key: Key path to template

        Returns:
            The specified template

        Throws:
            error.Bug if template could not found
        """
        if key not in self._templates:
            raise error.Bug("Could not find template {}".format(key))

        content = util.file_read(self._templates[key])
        return string.Template(content)
Example #4
0
def test_file_read_invalid(monkeypatch):
    monkeypatch.setattr("os.open", fake_invalid_open())

    with raises(error.NotFound):
        util.file_read("/invalid/directory/file")
Example #5
0
def test_file_read(monkeypatch):
    monkeypatch.setattr("__builtin__.open", fake_open(u"test stream", "/tmp/test", "r"))
    assert(util.file_read("/tmp/test") == "test stream")