]>
Commit | Line | Data |
---|---|---|
c37ec602 MT |
1 | #!/usr/bin/python |
2 | ||
11347e46 | 3 | import io |
abc3bb31 MT |
4 | import logging |
5 | import os.path | |
c37ec602 | 6 | |
abc3bb31 | 7 | from PIL import Image, ImageDraw, ImageFont, PngImagePlugin |
c37ec602 | 8 | |
11347e46 | 9 | from .misc import Object |
85626796 | 10 | from .decorators import * |
c37ec602 | 11 | |
9068dba1 | 12 | image_types = [] |
c37ec602 | 13 | |
9068dba1 | 14 | class 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 | 23 | class 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", ""), | |
49 | "fonts/Mukta-Regular.ttf" | |
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 |
76 | class 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 | |
05bd7298 | 85 | line1 = [_("%s on %s") % (self.profile.release_short, self.profile.arch),] |
abc3bb31 | 86 | line2 = [] |
abc3bb31 | 87 | |
66862195 MT |
88 | # Show the appliance model in the second line if available |
89 | if self.profile.appliance: | |
90 | line2.append(self.profile.appliance) | |
abc3bb31 | 91 | |
66862195 MT |
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")) | |
abc3bb31 | 98 | |
66862195 MT |
99 | # Otherwise show some general hardware information of the machine |
100 | else: | |
a0c9a14e MT |
101 | if self.profile.processor: |
102 | line2.append(self.profile.processor.friendly_string) | |
103 | ||
66862195 | 104 | line2.append(self.profile.friendly_memory) |
c37ec602 | 105 | |
85626796 MT |
106 | self.draw_text((225, 5), " | ".join(line1)) |
107 | self.draw_text((225, 23), "%s" % " - ".join(line2)) | |
c37ec602 MT |
108 | |
109 | ||
9068dba1 | 110 | image_types.append(Image1) |