QResource 仅适用于 Qt 世界,因此可能的解决方案是使用 Qt 类创建一个加载器:
qloader.py
import os
from collections import abc
from jinja2.loaders import BaseLoader, split_template_path
from jinja2.exceptions import TemplateNotFound
from PyQt5.QtCore import QDir, QDirIterator, QFile, QFileInfo, QIODevice
class QFileSystemLoader(BaseLoader):
def __init__(self, searchpath, encoding="utf-8", followlinks=False):
if not isinstance(searchpath, abc.Iterable) or isinstance(searchpath, str):
searchpath = [searchpath]
self.searchpath = list(searchpath)
self.encoding = encoding
self.followlinks = followlinks
def get_source(self, environment, template):
pieces = split_template_path(template)
for searchpath in self.searchpath:
filename = os.path.join(searchpath, *pieces)
f = QFile(filename)
if not f.exists():
continue
if not f.open(QIODevice.ReadOnly):
continue
contents = f.readAll().data().decode(self.encoding)
f.close()
dt = QFileInfo(f).fileTime(QFile.FileModificationTime)
def uptodate():
return QFileInfo(filename).fileTime(QFile.FileModificationTime) == dt
return contents, filename, uptodate
raise TemplateNotFound(template)
def list_templates(self):
found = set()
for searchpath in self.searchpath:
d = QDir(searchpath)
it_flag = QDirIterator.Subdirectories
if self.followlinks:
it_flag |= QDirIterator.FollowSymlinks
it_filter = QDir.Files | QDir.NoDotAndDotDot | QDir.Hidden | QDir.Readable
if not self.followlinks:
it_filter |= QDir.NoSymLinks
it = QDirIterator(searchpath, it_filter, it_flag)
while it.hasNext():
it.next()
found.add(d.relativeFilePath(it.filePath()))
return sorted(found)
from qloader import QFileSystemLoader
qfile_loader = QFileSystemLoader(":/plantillas/")
env = Environment(loader=qfile_loader)
plantilla = env.get_template("base.md")