Permalink
Browse files

removed greentornado and rect

  • Loading branch information...
heynemann committed May 2, 2011
1 parent 28a9b56 commit 34af846ead9369616f137be01854bf066604d8b7
Showing with 19 additions and 193 deletions.
  1. +0 −132 thumbor/lib/greentornado.py
  2. +0 −61 thumbor/rect.py
  3. +19 −0 vows/http_loader_vows.py
View
@@ -1,132 +0,0 @@
-# Originally from https://github.com/vishnevskiy/greentornado
-
-from eventlet.hubs import timer, get_hub, use_hub
-import tornado.ioloop
-import tornado.web
-import eventlet
-import time
-import sys
-import inspect
-
-def greenify(cls_or_func):
- """Decorate classes or functions with this to make them spawn as
- greenlets when initialized or called."""
-
- if not isinstance(get_hub(), TornadoHub):
- use_hub(TornadoHub)
-
- if inspect.isclass(cls_or_func) and tornado.web.RequestHandler in inspect.getmro(cls_or_func):
- execute = cls_or_func._execute
- cls_or_func._execute = lambda self, *args, **kwargs: eventlet.spawn_n(execute, self, *args, **kwargs)
- return cls_or_func
- else:
- def wrapper(*args, **kwargs):
- eventlet.spawn_n(cls_or_func, *args, **kwargs)
- setattr(wrapper, 'original', cls_or_func)
- return wrapper
-
-class Timer(timer.Timer):
- """Fix Eventlet's Timer to work with Tornado's IOLoop."""
-
- def __init__(self, *args, **kwargs):
- timer.Timer.__init__(self, *args, **kwargs)
- self.schedule()
-
- def schedule(self):
- """Schedule this timer to run in the IOLoop."""
-
- self.called = False
- self.scheduled_time = get_hub().io_loop.add_timeout(time.time() + self.seconds, self)
- return self
-
- def cancel(self):
- """Prevent this timer from being called. If the timer has already
- been called or canceled, has no effect."""
-
- if not self.called:
- self.called = True
- get_hub().io_loop.remove_timeout(self.scheduled_time)
- try:
- del self.tpl
- except AttributeError:
- pass
-
-class LocalTimer(Timer):
- def __init__(self, *args, **kwargs):
- self.greenlet = eventlet.getcurrent()
- Timer.__init__(self, *args, **kwargs)
-
- @property
- def pending(self):
- if self.greenlet is None or self.greenlet.dead:
- return False
- return not self.called
-
- def __call__(self, *args):
- if not self.called:
- self.called = True
- if self.greenlet is not None and self.greenlet.dead:
- return
- callback, args, kwargs = self.tpl
- callback(*args, **kwargs)
-
- def cancel(self):
- self.greenlet = None
- Timer.cancel(self)
-
-def call_later(cls, seconds, func, *args, **kwargs):
- assert callable(func), '%s is not callable' % func
- if not isinstance(seconds, (int, long, float)):
- raise TypeError('Seconds must be int, long, or float, was ' + type(seconds))
- assert sys.maxint >= seconds >= 0, '%s is not greater than or equal to 0 seconds' % seconds
- return cls(seconds, func, *args, **kwargs)
-
-class TornadoHub(object):
- READ = tornado.ioloop.IOLoop.READ
- WRITE = tornado.ioloop.IOLoop.WRITE
-
- def __init__(self):
- self.greenlet = eventlet.getcurrent()
- self.io_loop = tornado.ioloop.IOLoop.instance()
-
- def switch(self):
- assert eventlet.getcurrent() is not self.greenlet, 'Cannot switch to MAINLOOP from MAINLOOP'
-
- try:
- eventlet.getcurrent().parent = self.greenlet
- except ValueError:
- pass
-
- return self.greenlet.switch()
-
- def stop(self):
- self.io_loop.stop()
-
- abort = stop
-
- def add(self, event, fd, callback):
- self.io_loop.add_handler(fd, callback, event)
- return fd
-
- def remove(self, fd):
- self.io_loop.remove_handler(fd)
-
- def schedule_call_local(self, seconds, func, *args, **kwargs):
- def call_if_greenlet_alive(*args1, **kwargs1):
- if t.greenlet.dead:
- return
- return func(*args1, **kwargs1)
- t = call_later(LocalTimer, seconds, call_if_greenlet_alive, *args, **kwargs)
- return t
-
- schedule_call = schedule_call_local
-
- def schedule_call_global(self, seconds, func, *args, **kwargs):
- return call_later(Timer, seconds, func, *args, **kwargs)
-
- @property
- def running(self):
- return self.ioloop.running
-
-Hub = TornadoHub
-
View
@@ -1,61 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-
-# thumbor imaging service
-# https://github.com/globocom/thumbor/wiki
-
-# Licensed under the MIT license:
-# http://www.opensource.org/licenses/mit-license
-# Copyright (c) 2011 globo.com timehome@corp.globo.com
-
-class BoundingRect(object):
- def __init__(self, width, height):
- self.width = width
- self.height = height
-
- def set_size(self, width, height, halign, valign):
- self.target_width = width
- self.target_height = height
- self.calculate_proportional_dimensions()
- self.calculate_crops(halign, valign)
-
- def calculate_proportional_dimensions(self):
- if not self.target_width:
- self.target_width = self.target_height * self.width / self.height
-
- if not self.target_height:
- self.target_height = self.target_width * self.height / self.width
-
- def calculate_crops(self, halign, valign):
-
- if isinstance(valign, basestring):
- self.top = ((self.height - self.target_height) / 2) / self.height
- if self.top < 0:
- self.top = 0
- self.bottom = 1 - self.top
- if valign == "top":
- self.bottom = 1 - (2 * self.top)
- self.top = 0
- elif valign == "bottom":
- self.top = 2 * self.top
- self.bottom = 1
- else:
- crop_height = self.height - self.target_height
- self.top = (crop_height * valign) / self.height
- self.bottom = (self.height - (crop_height * (1 - valign))) / self.height
-
- if isinstance(halign, basestring):
- self.left = ((self.width - self.target_width) / 2) / self.width
- if self.left < 0:
- self.left = 0
- self.right = 1 - self.left
- if halign == "left":
- self.right = 1 - (2 * self.left)
- self.left = 0
- elif halign == "right":
- self.left = 2 * self.left
- self.right = 1
- else:
- crop_width = self.width - self.target_width
- self.left = (crop_width * halign) / self.width
- self.right = (self.width - (crop_width * (1 - halign))) / self.width
View
@@ -13,6 +13,7 @@
from pyvows import Vows, expect
from tornado_pyvows.context import TornadoContext, TornadoSubContext
import tornado.web
+from tornado.options import options
import thumbor.loaders.http_loader as loader
@@ -30,6 +31,24 @@ def get(self):
@Vows.batch
class HttpLoader(Vows.Context):
+ class ValidateURL(Vows.Context):
+ def topic(self):
+ return loader.validate('http://www.google.com/logo.jpg')
+
+ def should_not_validate(self, topic):
+ expect(topic).to_be_false()
+
+ class AllowAll(Vows.Context):
+ def topic(self):
+ old_sources = options.ALLOWED_SOURCES
+ options.ALLOWED_SOURCES = []
+ is_valid = loader.validate('http://www.google.com/logo.jpg')
+ options.ALLOWED_SOURCES = old_sources
+ return is_valid
+
+ def should_validate(self, topic):
+ expect(topic).to_be_true()
+
class NormalizeURL(Vows.Context):
class WhenStartsWithHttp(Vows.Context):
def topic(self):

0 comments on commit 34af846

Please sign in to comment.