summaryrefslogtreecommitdiff
path: root/build.py
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-04-05 11:29:10 +0200
committerJulian T <julian@jtle.dk>2021-04-05 11:29:10 +0200
commitce4983421e2d4bad3a4e563920f46f210aad7334 (patch)
treec9aa6d34a9c501781efbf1d2af7652cf9efdbd60 /build.py
parent246e219c69b6a143cea478c7f0f9300ca73efa79 (diff)
Fix bugs found by pyls
Diffstat (limited to 'build.py')
-rwxr-xr-xbuild.py25
1 files changed, 15 insertions, 10 deletions
diff --git a/build.py b/build.py
index 05b05bd..0759deb 100755
--- a/build.py
+++ b/build.py
@@ -2,7 +2,6 @@
import jinja2
from PIL import Image
import argparse
-from pathlib import Path
import yaml
import os
import re
@@ -11,6 +10,7 @@ import hashlib
imagereg = re.compile("([a-z0-9]*)\\..*")
resreg = re.compile("([0-9]*)x([0-9]*)")
+
def hashfile(fname):
md5 = hashlib.md5()
print(fname)
@@ -20,18 +20,21 @@ def hashfile(fname):
return md5
+
def parse_res(res):
m = resreg.match(res)
return (int(m.group(1)), int(m.group(2)))
+
class FileLoader(jinja2.BaseLoader):
def get_source(self, environment, template):
if not os.path.exists(template):
- raise TemplateNotFound(template)
+ raise jinja2.TemplateNotFound(template)
mtime = os.path.getmtime(template)
with open(template, "r") as f:
source = f.read()
- return source, template, lambda: mtime == getmtime(template)
+ return source, template, lambda: mtime == os.path.getmtime(template)
+
class ImageLocation:
def __init__(self, folder):
@@ -59,9 +62,11 @@ class ImageLocation:
def convert(self, imgname, settings):
# First determine the hash from settings
- thash = hashlib.md5(bytes(
- f"{settings['res']}+{settings['ext']}" + imgname
- , encoding="utf8")).hexdigest()
+ thash = hashlib.md5(
+ bytes(
+ f"{settings['res']}+{settings['ext']}" + imgname,
+ encoding="utf8")
+ ).hexdigest()
# Now check if it exists
if thash in self.existing:
@@ -104,6 +109,7 @@ class ImageLocation:
print(f"Removing file {fname}")
os.remove(fname)
+
class Renderer:
def __init__(self, config_file, loadpath, resolution, extension):
self.config = Renderer.__load_config(config_file)
@@ -134,7 +140,7 @@ class Renderer:
loc = ImageLocation(os.path.join(dest, "imgs"))
jenv = jinja2.Environment(
- loader = FileLoader(),
+ loader=FileLoader(),
autoescape=jinja2.select_autoescape(['html', 'xml'])
)
jenv.globals.update(
@@ -144,11 +150,12 @@ class Renderer:
tmpl = jenv.get_template(template)
with open(os.path.join(dest, "index.html"), "w") as f:
- f.write(tmpl.render({**self.config, **context}))
+ f.write(tmpl.render({**self.config, **context}))
if clean:
loc.clean()
+
parser = argparse.ArgumentParser()
parser.add_argument("--dest", "-d", default="build", help="where to put resulting files")
parser.add_argument("--size", "-s", default="1920x1080", help="size to scale web images to")
@@ -169,5 +176,3 @@ context = {
rend = Renderer(args.config, args.load, args.size, args.ext)
rend.build_to(args.dest, args.template, context, clean=args.clean)
-
-