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