]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/iuse.py
Major update of the webapp.
[people/shoehn/ipfire.org.git] / webapp / backend / iuse.py
1 #!/usr/bin/python
2
3 from __future__ import division
4
5 import StringIO
6 import logging
7 import os.path
8
9 from PIL import Image, ImageDraw, ImageFont, PngImagePlugin
10
11 from misc import Object
12
13 image_types = []
14
15 class IUse(Object):
16 def get_imagetype(self, id):
17 id = int(id)
18
19 for image_type in image_types:
20 if image_type.id == id:
21 return image_type
22
23
24 class ImageObject(Object):
25 default_mode = "RGBA"
26 default_size = 100, 100
27
28 _filename = None
29 _font = "DejaVuSans.ttf"
30 _font_size = 10
31
32 def __init__(self, backend, request, profile):
33 Object.__init__(self, backend)
34
35 self.request = request
36 self.profile = profile
37
38 # Create new image
39 if self.filename and os.path.exists(self.filename):
40 self.open(self.filename)
41 else:
42 self._image = Image.new(self.default_mode, self.default_size)
43
44 self.draw()
45
46 def open(self, filename):
47 logging.debug("Opening image as a template: %s" % filename)
48
49 image = Image.open(filename)
50 self._image = image.convert(self.default_mode)
51
52 def save(self, filename):
53 self._image.save(filename, "PNG", optimize=True)
54
55 def to_string(self):
56 f = StringIO.StringIO()
57
58 self.save(f)
59
60 return f.getvalue()
61
62 @property
63 def paint(self):
64 if not hasattr(self, "_draw"):
65 self._draw = ImageDraw.Draw(self._image)
66
67 return self._draw
68
69 def draw(self):
70 raise NotImplementedError
71
72 @property
73 def font(self):
74 if not hasattr(self, "__font"):
75 fontfile = os.path.join(
76 self.request.application.settings.get("template_path", ""),
77 "i-use", "fonts", self._font
78 )
79
80 self.__font = ImageFont.truetype(fontfile, self._font_size, encoding="unic")
81
82 return self.__font
83
84 def draw_text(self, pos, text, **kwargs):
85 if not kwargs.has_key("font"):
86 kwargs["font"] = self.font
87
88 return self.paint.text(pos, text, **kwargs)
89
90 @property
91 def filename(self):
92 if not self._filename:
93 return
94
95 return os.path.join(
96 self.request.application.settings.get("template_path", ""),
97 "i-use", self._filename
98 )
99
100 @property
101 def locale(self):
102 return self.request.locale
103
104
105 #class Image1(ImageObject):
106 # id = 0
107 #
108 # default_size = 500, 50
109 #
110 # def draw(self):
111 # # Background
112 # self.paint.rectangle(((0, 0), self.default_size), fill="#000000")
113 #
114 # # Release information
115 # self.paint.text((10, 10), "%s" % self.profile.release)
116 #
117 # # Hardware information
118 # hw = [
119 # self.profile.cpu.model_string,
120 # "Mem: %.1fG" % (self.profile.memory / 1024**2),
121 # ]
122 #
123 # if self.profile.virtual:
124 # virt = "V-%s" % self.profile.hypervisor.vendor
125 # if self.profile.hypervisor.type == "para":
126 # virt = "%s-PV" % virt
127 # hw.append(virt)
128 #
129 # if self.profile.cpu.capable_64bit:
130 # hw.append("64")
131 #
132 # self.paint.text((10, 30), "%s" % " | ".join(hw))
133 #
134 #
135 #IUse().add_imagetype(Image1)
136
137
138 class Image1(ImageObject):
139 id = 0
140
141 default_size = 500, 50
142
143 _filename = "i-use-1.png"
144 _font = "DejaVuSans.ttf"
145 _font_size = 10
146
147 def draw(self):
148 _ = self.locale.translate
149
150 line1 = [self.profile.release,]
151
152 if self.profile.virtual:
153 virt = "V-%s" % self.profile.hypervisor.vendor
154 if self.profile.hypervisor.type == "para":
155 virt = "%s-PV" % virt
156 line1.append(virt)
157
158 self.draw_text((225, 8), " | ".join(line1))
159
160 line2 = []
161 line2.append(self.profile.cpu.friendly_string)
162 line2.append(_("Mem: %s") % self.profile.friendly_memory)
163
164 if self.profile.root_size:
165 line2.append(_("Disk: %s") % self.profile.friendly_root_size)
166
167 line3 = []
168
169 if self.profile.network:
170 zones = []
171
172 for zone in ("green", "red", "blue", "orange"):
173 if self.profile.network.has_zone(zone):
174 zones.append(_(zone))
175
176 if zones:
177 line3.append(_("Networks: %s") % " | ".join(zones))
178
179 self.draw_text((225, 20), "%s" % " - ".join(line2))
180 self.draw_text((225, 32), "%s" % " - ".join(line3))
181
182
183 image_types.append(Image1)