]> git.ipfire.org Git - ipfire.org.git/commitdiff
Add experimental support for signature images.
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Dec 2010 00:46:09 +0000 (01:46 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Dec 2010 00:46:09 +0000 (01:46 +0100)
www/webapp/__init__.py
www/webapp/backend/__init__.py
www/webapp/backend/iuse.py [new file with mode: 0644]
www/webapp/handlers.py
www/webapp/handlers_iuse.py [new file with mode: 0644]

index 20cc5d364bb0951b482a6f7ddcb201d0184ccfb6..40eac14aa4916d31cce9711523c3cd1e5ba6d215 100644 (file)
@@ -123,6 +123,11 @@ class Application(tornado.web.Application):
                        (r"/statistics/virtual", StasyStatsVirtualHandler),
                ] + static_handlers)
 
+               # i-use.ipfire.org
+               self.add_handlers(r"i-use\.ipfire\.org", [
+                       (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
+               ])
+
                # source.ipfire.org
 #              self.add_handlers(r"source\.ipfire\.org", [
 #                      (r"/", MainHandler),
index 8712bf7400f79c97f6d829e28099eb0a7ae17530..390783e6b11f5d83532bc4a597fd846a0e4399b8 100644 (file)
@@ -3,6 +3,7 @@
 from accounts  import Accounts
 from banners   import Banners
 from geoip             import GeoIP
+from iuse              import IUse
 from menu              import Menu
 from mirrors   import Mirrors
 from netboot   import NetBoot
diff --git a/www/webapp/backend/iuse.py b/www/webapp/backend/iuse.py
new file mode 100644 (file)
index 0000000..c84fee7
--- /dev/null
@@ -0,0 +1,100 @@
+#!/usr/bin/python
+
+from __future__ import division
+
+import StringIO
+
+from PIL import Image, ImageDraw, PngImagePlugin
+
+from misc import Singleton
+
+class IUse(object):
+       __metaclass__ = Singleton
+
+       images = []
+
+       def add_imagetype(self, type):
+               self.images.append(type)
+
+       def get_imagetype(self, id):
+               id = int(id)
+
+               for image in self.images:
+                       if image.id == id:
+                               return image
+
+
+class ImageObject(object):
+       default_mode = "RGBA"
+       default_size = 100, 100
+
+       def __init__(self, profile):
+               self.profile = profile
+
+               # Create new image
+               self._image = Image.new(self.default_mode, self.default_size)
+
+               self.draw()
+
+       def open(self, filename):
+               image = Image.open(filename, self.default_mode)
+               self._image = image.convert(self.default_mode)
+
+       def save(self, filename):
+               self._image.save(filename, "PNG", optimize=True)
+
+       def to_string(self):
+               f = StringIO.StringIO()
+
+               self.save(f)
+
+               return f.getvalue()
+
+       @property
+       def paint(self):
+               if not hasattr(self, "_draw"):
+                       self._draw = ImageDraw.Draw(self._image)
+
+               return self._draw
+
+       def draw(self):
+               raise NotImplementedError
+
+
+class Image1(ImageObject):
+       id = 0
+
+       default_size = 500, 50
+
+       def draw(self):
+               # Background
+               self.paint.rectangle(((0, 0), self.default_size), fill="#000000")
+
+               # Release information
+               self.paint.text((10, 10), "%s" % self.profile.release)
+
+               # Hardware information
+               hw = [
+                       self.profile.cpu.model_string,
+                       "Memory: %.1fG" % (self.profile.memory / 1024**2),
+               ]
+
+               if self.profile.virtual:
+                       virt = "V-%s" % self.profile.hypervisor.vendor
+                       if self.profile.hypervisor.type == "PV":
+                               virt = "%s-PV" % virt
+                       hw.append(virt)
+
+               if self.profile.cpu.capable_64bit:
+                       hw.append("64")
+
+               self.paint.text((10, 30), "%s" % " | ".join(hw))
+
+
+IUse().add_imagetype(Image1)
+
+
+if __name__ == "__main__":
+       image = Image1("123")
+       image.save("picture.png")
+
index fb459f40e0eb4a0e5d3ba6e4ae37bd96c64783ab..ad67f3d16d6ee002dd69dfdd82c8de57390656ce 100644 (file)
@@ -18,6 +18,7 @@ import backend
 from handlers_admin import *
 from handlers_base import *
 from handlers_download import *
+from handlers_iuse import *
 from handlers_mirrors import *
 from handlers_news import *
 from handlers_planet import *
diff --git a/www/webapp/handlers_iuse.py b/www/webapp/handlers_iuse.py
new file mode 100644 (file)
index 0000000..97f5fac
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/python
+
+import tornado.web
+
+from handlers_base import *
+import backend
+
+class IUseImage(BaseHandler):
+       @property
+       def iuse(self):
+               return backend.IUse()
+
+       @property
+       def stasy(self):
+               return backend.Stasy()
+
+       def get(self, profile_id, image_id):
+               image_cls = self.iuse.get_imagetype(image_id)
+               if not image_cls:
+                       raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id)
+
+               profile = self.stasy.get_profile(profile_id)
+               if not profile:
+                       raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
+
+               self.set_header("Content-type", "image/png")
+
+               # Render the image
+               # XXX use memcached at this place
+               image = image_cls(profile)
+
+               self.write(image.to_string())
+