1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 07:57:52 +03:00
WeasyPrint/weasy/utils.py

57 lines
1.9 KiB
Python
Raw Normal View History

# 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
import functools
class MultiFunction(object):
"""
A callable with different implementations depending on the type of the
first argument.
2011-07-01 20:20:30 +04:00
This object takes __name__, __module__ and __doc__ from base_function
if it is given, but does not use its body.
"""
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)
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))