From c37ec60216b567dd0a74b4893ffb88091fdda230 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 17 Dec 2010 01:46:09 +0100 Subject: [PATCH] Add experimental support for signature images. --- www/webapp/__init__.py | 5 ++ www/webapp/backend/__init__.py | 1 + www/webapp/backend/iuse.py | 100 +++++++++++++++++++++++++++++++++ www/webapp/handlers.py | 1 + www/webapp/handlers_iuse.py | 33 +++++++++++ 5 files changed, 140 insertions(+) create mode 100644 www/webapp/backend/iuse.py create mode 100644 www/webapp/handlers_iuse.py diff --git a/www/webapp/__init__.py b/www/webapp/__init__.py index 20cc5d36..40eac14a 100644 --- a/www/webapp/__init__.py +++ b/www/webapp/__init__.py @@ -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), diff --git a/www/webapp/backend/__init__.py b/www/webapp/backend/__init__.py index 8712bf74..390783e6 100644 --- a/www/webapp/backend/__init__.py +++ b/www/webapp/backend/__init__.py @@ -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 index 00000000..c84fee7b --- /dev/null +++ b/www/webapp/backend/iuse.py @@ -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") + diff --git a/www/webapp/handlers.py b/www/webapp/handlers.py index fb459f40..ad67f3d1 100644 --- a/www/webapp/handlers.py +++ b/www/webapp/handlers.py @@ -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 index 00000000..97f5fac9 --- /dev/null +++ b/www/webapp/handlers_iuse.py @@ -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()) + -- 2.47.3