]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/iuse.py
CSS: Add CSS for file listings
[ipfire.org.git] / src / backend / iuse.py
1 #!/usr/bin/python
2
3 import io
4 import logging
5 import os.path
6
7 from PIL import Image, ImageDraw, ImageFont, PngImagePlugin
8
9 from .misc import Object
10 from .decorators import *
11
12 image_types = []
13
14 class IUse(Object):
15 def get_imagetype(self, id):
16 id = int(id)
17
18 for image_type in image_types:
19 if image_type.id == id:
20 return image_type
21
22
23 class ImageObject(Object):
24 _filename = None
25
26 def init(self, request, profile):
27 self.request = request
28 self.profile = profile
29
30 # Create new image
31 if self.filename and os.path.exists(self.filename):
32 with Image.open(self.filename) as image:
33 self._image = image.convert("RGBA")
34 else:
35 self._image = Image.new("RGBA", (100, 100))
36
37 self.render()
38
39 def to_string(self):
40 with io.BytesIO() as f:
41 self._image.save(f, "PNG", optimize=True)
42
43 return f.getvalue()
44
45 @lazy_property
46 def font(self):
47 fontfile = os.path.join(
48 self.request.application.settings.get("static_path", ""),
49 "fonts/Mukta-Regular.ttf"
50 )
51
52 return ImageFont.truetype(fontfile, 15, encoding="unic")
53
54 @lazy_property
55 def draw(self):
56 return ImageDraw.Draw(self._image)
57
58 def draw_text(self, pos, text, **kwargs):
59 return self.draw.text(pos, text, font=self.font, **kwargs)
60
61 @property
62 def filename(self):
63 if not self._filename:
64 return
65
66 return os.path.join(
67 self.request.application.settings.get("template_path", ""),
68 "fireinfo", self._filename
69 )
70
71 @property
72 def locale(self):
73 return self.request.locale
74
75
76 class Image1(ImageObject):
77 id = 0
78
79 default_size = 500, 50
80 _filename = "i-use-1.png"
81
82 def render(self):
83 _ = self.locale.translate
84
85 line1 = [_("%s on %s") % (self.profile.release_short, self.profile.arch),]
86 line2 = []
87
88 # Show the appliance model in the second line if available
89 if self.profile.appliance:
90 line2.append(self.profile.appliance)
91
92 # Show the hypervisor vendor for virtual machines
93 elif self.profile.virtual:
94 if self.profile.hypervisor:
95 line2.append(_("Virtualised on %s") % self.profile.hypervisor)
96 else:
97 line2.append(_("Running in a virtualised environment"))
98
99 # Otherwise show some general hardware information of the machine
100 else:
101 if self.profile.processor:
102 line2.append(self.profile.processor.friendly_string)
103
104 line2.append(self.profile.friendly_memory)
105
106 self.draw_text((225, 5), " | ".join(line1))
107 self.draw_text((225, 23), "%s" % " - ".join(line2))
108
109
110 image_types.append(Image1)