]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/iuse.py
fireinfo: Refactor generating i-use images
[ipfire.org.git] / src / backend / iuse.py
index a3c0c5724f05eac67144ee7ff612e76766cc079a..99c9d0420181dcaa557149118f23d122fc9518cd 100644 (file)
@@ -7,6 +7,7 @@ import os.path
 from PIL import Image, ImageDraw, ImageFont, PngImagePlugin
 
 from .misc import Object
+from .decorators import *
 
 image_types = []
 
@@ -20,70 +21,42 @@ class IUse(Object):
 
 
 class ImageObject(Object):
-       default_mode = "RGBA"
-       default_size = 100, 100
-
        _filename = None
-       _font = "Ubuntu-R.ttf"
-       _font_size = 10
-
-       def __init__(self, backend, request, profile):
-               Object.__init__(self, backend)
 
+       def init(self, request, profile):
                self.request = request
                self.profile = profile
 
                # Create new image
                if self.filename and os.path.exists(self.filename):
-                       self.open(self.filename)
+                       with Image.open(self.filename) as image:
+                               self._image = image.convert("RGBA")
                else:
-                       self._image = Image.new(self.default_mode, self.default_size)
-
-               self.draw()
-
-       def open(self, filename):
-               logging.debug("Opening image as a template: %s" % filename)
-
-               image = Image.open(filename)
-               self._image = image.convert(self.default_mode)
+                       self._image = Image.new("RGBA", (100, 100))
 
-       def save(self, filename):
-               self._image.save(filename, "PNG", optimize=True)
+               self.render()
 
        def to_string(self):
-               f = io.StringIO()
-
-               self.save(f)
-
-               return f.getvalue()
-
-       @property
-       def paint(self):
-               if not hasattr(self, "_draw"):
-                       self._draw = ImageDraw.Draw(self._image)
+               with io.BytesIO() as f:
+                       self._image.save(f, "PNG", optimize=True)
 
-               return self._draw
+                       return f.getvalue()
 
-       def draw(self):
-               raise NotImplementedError
-
-       @property
+       @lazy_property
        def font(self):
-               if not hasattr(self, "__font"):
-                       fontfile = os.path.join(
-                               self.request.application.settings.get("template_path", ""),
-                               "i-use", "fonts", self._font
-                       )
+               fontfile = os.path.join(
+                       self.request.application.settings.get("static_path", ""),
+                       "fonts/Mukta-Regular.ttf"
+               )
 
-                       self.__font = ImageFont.truetype(fontfile, self._font_size, encoding="unic")
+               return ImageFont.truetype(fontfile, 15, encoding="unic")
 
-               return self.__font
+       @lazy_property
+       def draw(self):
+               return ImageDraw.Draw(self._image)
 
        def draw_text(self, pos, text, **kwargs):
-               if "font" not in kwargs:
-                       kwargs["font"] = self.font
-
-               return self.paint.text(pos, text, **kwargs)
+               return self.draw.text(pos, text, font=self.font, **kwargs)
 
        @property
        def filename(self):
@@ -92,7 +65,7 @@ class ImageObject(Object):
 
                return os.path.join(
                        self.request.application.settings.get("template_path", ""),
-                       "i-use", self._filename
+                       "fireinfo", self._filename
                )
 
        @property
@@ -104,12 +77,9 @@ class Image1(ImageObject):
        id = 0
 
        default_size = 500, 50
-
        _filename = "i-use-1.png"
-       _font = "Ubuntu-R.ttf"
-       _font_size = 14
 
-       def draw(self):
+       def render(self):
                _ = self.locale.translate
 
                line1 = [_("%s on %s") % (self.profile.release_short, self.profile.arch),]
@@ -133,8 +103,8 @@ class Image1(ImageObject):
 
                        line2.append(self.profile.friendly_memory)
 
-               self.draw_text((225, 9), " | ".join(line1))
-               self.draw_text((225, 27), "%s" % " - ".join(line2))
+               self.draw_text((225, 5), " | ".join(line1))
+               self.draw_text((225, 23), "%s" % " - ".join(line2))
 
 
 image_types.append(Image1)