]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/webapp/backend/iuse.py
3 from __future__
import division
9 from PIL
import Image
, ImageDraw
, ImageFont
, PngImagePlugin
11 from misc
import Singleton
14 __metaclass__
= Singleton
18 def add_imagetype(self
, type):
19 self
.images
.append(type)
21 def get_imagetype(self
, id):
24 for image
in self
.images
:
29 class ImageObject(object):
31 default_size
= 100, 100
34 _font
= "DejaVuSans.ttf"
37 def __init__(self
, request
, profile
):
38 self
.request
= request
39 self
.profile
= profile
42 if self
.filename
and os
.path
.exists(self
.filename
):
43 self
.open(self
.filename
)
45 self
._image
= Image
.new(self
.default_mode
, self
.default_size
)
49 def open(self
, filename
):
50 logging
.debug("Opening image as a template: %s" % filename
)
52 image
= Image
.open(filename
)
53 self
._image
= image
.convert(self
.default_mode
)
55 def save(self
, filename
):
56 self
._image
.save(filename
, "PNG", optimize
=True)
59 f
= StringIO
.StringIO()
67 if not hasattr(self
, "_draw"):
68 self
._draw
= ImageDraw
.Draw(self
._image
)
73 raise NotImplementedError
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
83 self
.__font
= ImageFont
.truetype(fontfile
, self
._font
_size
, encoding
="unic")
87 def draw_text(self
, pos
, text
, **kwargs
):
88 if not kwargs
.has_key("font"):
89 kwargs
["font"] = self
.font
91 return self
.paint
.text(pos
, text
, **kwargs
)
95 if not self
._filename
:
99 self
.request
.application
.settings
.get("template_path", ""),
100 "i-use", self
._filename
105 return self
.request
.locale
108 #class Image1(ImageObject):
111 # default_size = 500, 50
115 # self.paint.rectangle(((0, 0), self.default_size), fill="#000000")
117 # # Release information
118 # self.paint.text((10, 10), "%s" % self.profile.release)
120 # # Hardware information
122 # self.profile.cpu.model_string,
123 # "Mem: %.1fG" % (self.profile.memory / 1024**2),
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
132 # if self.profile.cpu.capable_64bit:
135 # self.paint.text((10, 30), "%s" % " | ".join(hw))
138 #IUse().add_imagetype(Image1)
141 class Image1(ImageObject
):
144 default_size
= 500, 50
146 _filename
= "i-use-1.png"
147 _font
= "DejaVuSans.ttf"
151 _
= self
.locale
.translate
153 line1
= [self
.profile
.release
,]
155 if self
.profile
.virtual
:
156 virt
= "V-%s" % self
.profile
.hypervisor
.vendor
157 if self
.profile
.hypervisor
.type == "para":
158 virt
= "%s-PV" % virt
161 self
.draw_text((225, 8), " | ".join(line1
))
164 line2
.append(self
.profile
.cpu
.friendly_string
)
165 line2
.append(_("Mem: %s") % self
.profile
.friendly_memory
)
167 if self
.profile
.root_size
:
168 line2
.append(_("Disk: %s") % self
.profile
.friendly_root_size
)
172 if self
.profile
.network
:
175 for zone
in ("green", "red", "blue", "orange"):
176 if self
.profile
.network
.has_zone(zone
):
177 zones
.append(_(zone
))
180 line3
.append(_("Networks: %s") % " | ".join(zones
))
182 self
.draw_text((225, 20), "%s" % " - ".join(line2
))
183 self
.draw_text((225, 32), "%s" % " - ".join(line3
))
186 IUse().add_imagetype(Image1
)
189 if __name__
== "__main__":
190 image
= Image1("123")
191 image
.save("picture.png")