jinja templates support

This commit is contained in:
2021-03-08 15:47:05 +03:00
parent 026c896fb7
commit b3345c4184
8 changed files with 85 additions and 23 deletions

View File

@ -17,10 +17,15 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import jinja2
import os
from typing import Dict
from ahriman.core.configuration import Configuration
from ahriman.core.report.report import Report
from ahriman.core.util import package_like
from ahriman.models.sign_settings import SignSettings
class HTML(Report):
@ -29,25 +34,37 @@ class HTML(Report):
Report.__init__(self, architecture, config)
section = self.config.get_section_name('html', self.architecture)
self.report_path = config.get(section, 'path')
self.css_path = config.get(section, 'css_path')
self.link_path = config.get(section, 'link_path')
self.title = config.get('repository', 'name')
self.template_path = config.get(section, 'template_path')
# base template vars
if SignSettings.from_option(config.get('sign', 'enabled')) != SignSettings.Disabled:
self.pgp_key = config.get('sign', 'key')
else:
self.pgp_key = None
self.homepage = config.get(section, 'homepage', fallback=None)
self.repository = config.get('repository', 'name')
def generate(self, path: str) -> None:
# lets not use libraries here
html = f'''<html lang="en"><head><title>{self.title}</title>'''
if self.css_path:
html += f'''<link rel="stylesheet" type="text/css" href="{self.css_path}">'''
html += '''</head><body>'''
# idea comes from https://stackoverflow.com/a/38642558
templates_dir, template_name = os.path.split(self.template_path)
loader = jinja2.FileSystemLoader(searchpath=templates_dir)
environment = jinja2.Environment(loader=loader)
template = environment.get_template(template_name)
html += '''<ul>'''
for package in sorted(os.listdir(path)):
if '.pkg.' not in package:
packages: Dict[str, str] = {}
for fn in sorted(os.listdir(path)):
if not package_like(fn):
continue
html += f'''<li><a href="{self.link_path}/{package}">{package}</a></li>'''
html += '''</ul>'''
packages[fn] = f'{self.link_path}/{fn}'
html += '''</body></html>'''
html = template.render(
homepage=self.homepage,
link_path=self.link_path,
packages=packages,
pgp_key=self.pgp_key,
repository=self.repository)
with open(self.report_path, 'w') as out:
out.write(html)

View File

@ -29,6 +29,7 @@ from ahriman.core.repo.repo_wrapper import RepoWrapper
from ahriman.core.report.report import Report
from ahriman.core.sign.gpg_wrapper import GPGWrapper
from ahriman.core.upload.uploader import Uploader
from ahriman.core.util import package_like
from ahriman.models.package import Package
from ahriman.models.repository_paths import RepositoryPaths
@ -64,7 +65,7 @@ class Repository:
def packages(self) -> List[Package]:
result: Dict[str, Package] = {}
for fn in os.listdir(self.paths.repository):
if '.pkg.' not in fn:
if not package_like(fn):
continue
full_path = os.path.join(self.paths.repository, fn)
try:
@ -101,7 +102,7 @@ class Repository:
def process_remove(self, packages: List[str]) -> str:
for fn in os.listdir(self.paths.repository):
if '.pkg.' not in fn:
if not package_like(fn):
continue
full_path = os.path.join(self.paths.repository, fn)
@ -146,7 +147,7 @@ class Repository:
self.config.get_section_name('build', self.architecture), 'ignore_packages')
for fn in os.listdir(self.paths.repository):
if '.pkg.' not in fn:
if not package_like(fn):
continue
try:

View File

@ -37,3 +37,7 @@ def check_output(*args: str, exception: Optional[Exception],
logger.debug(line)
raise exception or e
return result
def package_like(filename: str) -> bool:
return '.pkg.' in filename and not filename.endswith('.sig')