2012-02-17 21:49:58 +04:00
|
|
|
# coding: utf8
|
2012-03-22 02:19:27 +04:00
|
|
|
"""
|
|
|
|
weasyprint.compat
|
|
|
|
-----------------
|
2012-02-17 21:49:58 +04:00
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
Workarounds for compatibility with Python 2 and 3 in the same code base.
|
2012-02-17 21:49:58 +04:00
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
:copyright: Copyright 2011-2012 Simon Sapin and contributors, see AUTHORS.
|
|
|
|
:license: BSD, see LICENSE for details.
|
2012-02-17 21:49:58 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import division, unicode_literals
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import email
|
|
|
|
|
|
|
|
|
2012-02-29 15:23:25 +04:00
|
|
|
if sys.version_info[0] >= 3:
|
2012-02-17 21:49:58 +04:00
|
|
|
# Python 3
|
2012-05-15 15:40:36 +04:00
|
|
|
from urllib.parse import (
|
2012-06-25 18:09:40 +04:00
|
|
|
urljoin, urlsplit, quote, unquote, unquote_to_bytes, parse_qs,
|
|
|
|
urlencode)
|
2012-03-24 16:39:31 +04:00
|
|
|
from urllib.request import urlopen, Request, pathname2url
|
2012-02-17 21:49:58 +04:00
|
|
|
from array import array
|
|
|
|
|
2012-06-02 10:04:24 +04:00
|
|
|
unicode = str
|
2012-02-22 18:52:49 +04:00
|
|
|
basestring = str
|
2012-02-17 21:49:58 +04:00
|
|
|
xrange = range
|
|
|
|
iteritems = dict.items
|
2012-06-20 14:53:22 +04:00
|
|
|
izip = zip
|
2012-02-17 21:49:58 +04:00
|
|
|
|
2012-02-23 15:50:08 +04:00
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
def urlopen_contenttype(url):
|
|
|
|
"""Return (file_obj, mime_type, encoding)"""
|
|
|
|
result = urlopen(url)
|
|
|
|
info = result.info()
|
|
|
|
mime_type = info.get_content_type()
|
|
|
|
charset = info.get_param('charset')
|
|
|
|
return result, mime_type, charset
|
|
|
|
|
2012-02-23 15:50:08 +04:00
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
def parse_email(data):
|
|
|
|
if isinstance(data, bytes):
|
|
|
|
data = data.decode('utf8')
|
|
|
|
return email.message_from_string(data)
|
|
|
|
|
2012-06-05 15:52:57 +04:00
|
|
|
|
|
|
|
def ints_from_bytes(byte_string):
|
|
|
|
"""Return a list of ints from a byte string"""
|
|
|
|
return list(byte_string)
|
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
else:
|
|
|
|
# Python 2
|
2012-06-23 14:33:51 +04:00
|
|
|
from urlparse import urljoin, urlsplit, parse_qs
|
2012-02-17 21:49:58 +04:00
|
|
|
from urllib2 import urlopen, Request
|
2012-06-25 18:09:40 +04:00
|
|
|
from urllib import pathname2url, quote, unquote, urlencode
|
2012-02-17 21:49:58 +04:00
|
|
|
from array import array as _array
|
2012-06-20 14:53:22 +04:00
|
|
|
from itertools import izip
|
2012-02-17 21:49:58 +04:00
|
|
|
|
2012-06-02 10:04:24 +04:00
|
|
|
unicode = unicode
|
2012-02-22 18:52:49 +04:00
|
|
|
basestring = basestring
|
2012-02-17 21:49:58 +04:00
|
|
|
xrange = xrange
|
|
|
|
iteritems = dict.iteritems
|
|
|
|
|
2012-02-23 15:50:08 +04:00
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
def array(typecode, initializer):
|
|
|
|
return _array(typecode.encode('ascii'), initializer)
|
|
|
|
|
2012-02-23 15:50:08 +04:00
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
def urlopen_contenttype(url):
|
|
|
|
"""Return (file_obj, mime_type, encoding)"""
|
|
|
|
result = urlopen(url)
|
|
|
|
info = result.info()
|
|
|
|
mime_type = info.gettype()
|
|
|
|
charset = info.getparam('charset')
|
2012-06-15 00:08:34 +04:00
|
|
|
return result, mime_type, charset
|
2012-02-17 21:49:58 +04:00
|
|
|
|
2012-02-23 15:50:08 +04:00
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
def unquote_to_bytes(data):
|
|
|
|
if isinstance(data, unicode):
|
|
|
|
data = data.encode('ascii')
|
|
|
|
return unquote(data)
|
|
|
|
|
2012-02-23 15:50:08 +04:00
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
def parse_email(data):
|
|
|
|
if isinstance(data, unicode):
|
|
|
|
data = data.encode('utf8')
|
|
|
|
return email.message_from_string(data)
|
2012-06-05 15:52:57 +04:00
|
|
|
|
|
|
|
|
|
|
|
def ints_from_bytes(byte_string):
|
|
|
|
"""Return a list of ints from a byte string"""
|
|
|
|
return map(ord, byte_string)
|