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