aboutsummaryrefslogtreecommitdiffstats
path: root/wkhtmltopdf/utils.py
diff options
context:
space:
mode:
authorCharlie Denton <charlie@meshy.co.uk>2014-06-19 11:46:18 +0100
committerCharlie Denton <charlie@meshy.co.uk>2014-06-19 11:46:18 +0100
commit8e615cf00343d61e2bf3716c66888758aeb878ed (patch)
tree9f6f4ada901749eaeddb32ac75c0955a5327c2b5 /wkhtmltopdf/utils.py
parent0de693a16fa35525d3adb115bf8bf21c76581d0b (diff)
parent301bef0d6931582583a156b3195b8b13f54d2875 (diff)
downloaddjango-wkhtmltopdf-8e615cf00343d61e2bf3716c66888758aeb878ed.tar.gz
django-wkhtmltopdf-8e615cf00343d61e2bf3716c66888758aeb878ed.tar.bz2
django-wkhtmltopdf-8e615cf00343d61e2bf3716c66888758aeb878ed.zip
Merge pull request #48 from incuna/python3
Add Python3 support
Diffstat (limited to 'wkhtmltopdf/utils.py')
-rw-r--r--wkhtmltopdf/utils.py21
1 files changed, 14 insertions, 7 deletions
diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py
index 1b74a97..e34085d 100644
--- a/wkhtmltopdf/utils.py
+++ b/wkhtmltopdf/utils.py
@@ -5,10 +5,16 @@ from itertools import chain
import os
import re
import sys
-import urllib
-from urlparse import urljoin
+
+try:
+ from urllib.request import pathname2url
+ from urllib.parse import urljoin
+except ImportError: # Python2
+ from urllib import pathname2url
+ from urlparse import urljoin
from django.conf import settings
+from django.utils import six
from .subprocess import check_output
@@ -22,7 +28,7 @@ def _options_to_args(**options):
continue
flags.append('--' + name.replace('_', '-'))
if value is not True:
- flags.append(unicode(value))
+ flags.append(six.text_type(value))
return flags
@@ -56,7 +62,7 @@ def wkhtmltopdf(pages, output=None, **kwargs):
orientation='Landscape',
disable_javascript=True)
"""
- if isinstance(pages, basestring):
+ if isinstance(pages, six.string_types):
# Support a single page.
pages = [pages]
@@ -113,19 +119,20 @@ def http_quote(string):
valid ascii charset string you can use in, say, http headers and the
like.
"""
- if isinstance(string, unicode):
+ if isinstance(string, six.text_type):
try:
import unidecode
string = unidecode.unidecode(string)
except ImportError:
string = string.encode('ascii', 'replace')
# Wrap in double-quotes for ; , and the like
- return '"{0!s}"'.format(string.replace('\\', '\\\\').replace('"', '\\"'))
+ string = string.replace(b'\\', b'\\\\').replace(b'"', b'\\"')
+ return '"{0!s}"'.format(string.decode())
def pathname2fileurl(pathname):
"""Returns a file:// URL for pathname. Handles OS-specific conversions."""
- return urljoin('file:', urllib.pathname2url(pathname))
+ return urljoin('file:', pathname2url(pathname))
def make_absolute_paths(content):