]>
Commit | Line | Data |
---|---|---|
c37ec602 MT |
1 | #!/usr/bin/python |
2 | ||
3 | from __future__ import division | |
4 | ||
5 | import StringIO | |
abc3bb31 MT |
6 | import logging |
7 | import os.path | |
c37ec602 | 8 | |
abc3bb31 | 9 | from PIL import Image, ImageDraw, ImageFont, PngImagePlugin |
c37ec602 | 10 | |
9068dba1 | 11 | from misc import Object |
c37ec602 | 12 | |
9068dba1 | 13 | image_types = [] |
c37ec602 | 14 | |
9068dba1 | 15 | class IUse(Object): |
c37ec602 MT |
16 | def get_imagetype(self, id): |
17 | id = int(id) | |
18 | ||
9068dba1 MT |
19 | for image_type in image_types: |
20 | if image_type.id == id: | |
21 | return image_type | |
c37ec602 MT |
22 | |
23 | ||
9068dba1 | 24 | class ImageObject(Object): |
c37ec602 MT |
25 | default_mode = "RGBA" |
26 | default_size = 100, 100 | |
27 | ||
abc3bb31 | 28 | _filename = None |
66862195 | 29 | _font = "Ubuntu-R.ttf" |
abc3bb31 MT |
30 | _font_size = 10 |
31 | ||
9068dba1 MT |
32 | def __init__(self, backend, request, profile): |
33 | Object.__init__(self, backend) | |
34 | ||
abc3bb31 | 35 | self.request = request |
c37ec602 MT |
36 | self.profile = profile |
37 | ||
38 | # Create new image | |
abc3bb31 MT |
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) | |
c37ec602 MT |
43 | |
44 | self.draw() | |
45 | ||
46 | def open(self, filename): | |
abc3bb31 MT |
47 | logging.debug("Opening image as a template: %s" % filename) |
48 | ||
49 | image = Image.open(filename) | |
c37ec602 MT |
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 | ||
abc3bb31 MT |
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 | ||
c37ec602 MT |
137 | |
138 | class Image1(ImageObject): | |
139 | id = 0 | |
140 | ||
141 | default_size = 500, 50 | |
142 | ||
abc3bb31 | 143 | _filename = "i-use-1.png" |
66862195 MT |
144 | _font = "Ubuntu-R.ttf" |
145 | _font_size = 14 | |
c37ec602 | 146 | |
abc3bb31 MT |
147 | def draw(self): |
148 | _ = self.locale.translate | |
c37ec602 | 149 | |
05bd7298 | 150 | line1 = [_("%s on %s") % (self.profile.release_short, self.profile.arch),] |
abc3bb31 | 151 | line2 = [] |
abc3bb31 | 152 | |
66862195 MT |
153 | # Show the appliance model in the second line if available |
154 | if self.profile.appliance: | |
155 | line2.append(self.profile.appliance) | |
abc3bb31 | 156 | |
66862195 MT |
157 | # Show the hypervisor vendor for virtual machines |
158 | elif self.profile.virtual: | |
159 | if self.profile.hypervisor: | |
160 | line2.append(_("Virtualised on %s") % self.profile.hypervisor) | |
161 | else: | |
162 | line2.append(_("Running in a virtualised environment")) | |
abc3bb31 | 163 | |
66862195 MT |
164 | # Otherwise show some general hardware information of the machine |
165 | else: | |
a0c9a14e MT |
166 | if self.profile.processor: |
167 | line2.append(self.profile.processor.friendly_string) | |
168 | ||
66862195 | 169 | line2.append(self.profile.friendly_memory) |
c37ec602 | 170 | |
66862195 MT |
171 | self.draw_text((225, 9), " | ".join(line1)) |
172 | self.draw_text((225, 27), "%s" % " - ".join(line2)) | |
c37ec602 MT |
173 | |
174 | ||
9068dba1 | 175 | image_types.append(Image1) |