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)
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)
def test_file_read_invalid(monkeypatch): monkeypatch.setattr("os.open", fake_invalid_open()) with raises(error.NotFound): util.file_read("/invalid/directory/file")
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")