2011-06-30 00:34:01 +04:00
|
|
|
|
# coding: utf8
|
|
|
|
|
|
|
|
|
|
# WeasyPrint converts web documents (HTML, CSS, ...) to PDF.
|
|
|
|
|
# Copyright (C) 2011 Simon Sapin
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2011-08-05 13:16:44 +04:00
|
|
|
|
try:
|
|
|
|
|
from urlparse import urljoin
|
|
|
|
|
except ImportError:
|
|
|
|
|
# Python 3
|
|
|
|
|
from urllib.parse import urljoin
|
2011-06-30 00:34:01 +04:00
|
|
|
|
|
|
|
|
|
import functools
|
|
|
|
|
|
|
|
|
|
class MultiFunction(object):
|
|
|
|
|
"""
|
|
|
|
|
A callable with different implementations depending on the type of the
|
|
|
|
|
first argument.
|
2011-07-07 19:33:17 +04:00
|
|
|
|
|
2011-07-01 20:20:30 +04:00
|
|
|
|
This object takes __name__, __module__ and __doc__ from base_function
|
2011-06-30 00:34:01 +04:00
|
|
|
|
if it is given, but does not use it’s body.
|
|
|
|
|
"""
|
2011-07-07 19:33:17 +04:00
|
|
|
|
|
2011-06-30 00:34:01 +04:00
|
|
|
|
def __init__(self, base_function=None):
|
|
|
|
|
self.implementations = {}
|
|
|
|
|
if base_function:
|
|
|
|
|
functools.update_wrapper(self, base_function)
|
|
|
|
|
|
|
|
|
|
def register(self, class_):
|
|
|
|
|
def decorator(function):
|
|
|
|
|
self.implementations[class_] = function
|
|
|
|
|
return function
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
def __call__(self, obj, *args, **kwargs):
|
|
|
|
|
for class_ in type(obj).mro():
|
|
|
|
|
implementation = self.implementations.get(class_)
|
|
|
|
|
if implementation:
|
|
|
|
|
return implementation(obj, *args, **kwargs)
|
2011-07-07 19:33:17 +04:00
|
|
|
|
raise NotImplementedError('No implementation for %r' % type(obj))
|
2011-08-05 13:16:44 +04:00
|
|
|
|
|
|
|
|
|
def get_url_attribute(element, key):
|
|
|
|
|
return urljoin(element.base_url, element.get(key))
|
|
|
|
|
|