mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
py3 porting
This commit is contained in:
parent
eb37632940
commit
2b8ac505e2
@ -1,16 +1,15 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
# License: GPLv3 Copyright: 2010, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
import copy
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
import ssl
|
||||||
__docformat__ = 'restructuredtext en'
|
|
||||||
|
|
||||||
import copy, ssl
|
|
||||||
|
|
||||||
from mechanize import Browser as B, HTTPSHandler
|
from mechanize import Browser as B, HTTPSHandler
|
||||||
|
|
||||||
from polyglot import http_client
|
from polyglot import http_client
|
||||||
from polyglot.http_cookie import CookieJar, Cookie
|
from polyglot.http_cookie import Cookie, CookieJar
|
||||||
|
|
||||||
|
|
||||||
class ModernHTTPSHandler(HTTPSHandler):
|
class ModernHTTPSHandler(HTTPSHandler):
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
__license__ = 'GPL 3'
|
#!/usr/bin/env python2
|
||||||
__copyright__ = '2010, sengian <sengian1@gmail.com>'
|
# vim:fileencoding=utf-8
|
||||||
__docformat__ = 'restructuredtext en'
|
# License: GPLv3 Copyright: 2010, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from polyglot.builtins import codepoint_to_chr, map, range, filter
|
from polyglot.builtins import codepoint_to_chr, map, range, filter
|
||||||
@ -24,7 +25,7 @@ def ascii_pat(for_binary=False):
|
|||||||
if ans is None:
|
if ans is None:
|
||||||
chars = set(range(32)) - {9, 10, 13}
|
chars = set(range(32)) - {9, 10, 13}
|
||||||
chars.add(127)
|
chars.add(127)
|
||||||
pat = u'|'.join(map(codepoint_to_chr, chars))
|
pat = '|'.join(map(codepoint_to_chr, chars))
|
||||||
if for_binary:
|
if for_binary:
|
||||||
pat = pat.encode('ascii')
|
pat = pat.encode('ascii')
|
||||||
ans = re.compile(pat)
|
ans = re.compile(pat)
|
||||||
@ -38,14 +39,14 @@ def clean_ascii_chars(txt, charlist=None):
|
|||||||
This is all control chars except \t, \n and \r
|
This is all control chars except \t, \n and \r
|
||||||
'''
|
'''
|
||||||
is_binary = isinstance(txt, bytes)
|
is_binary = isinstance(txt, bytes)
|
||||||
empty = b'' if is_binary else u''
|
empty = b'' if is_binary else ''
|
||||||
if not txt:
|
if not txt:
|
||||||
return empty
|
return empty
|
||||||
|
|
||||||
if charlist is None:
|
if charlist is None:
|
||||||
pat = ascii_pat(is_binary)
|
pat = ascii_pat(is_binary)
|
||||||
else:
|
else:
|
||||||
pat = u'|'.join(map(codepoint_to_chr, charlist))
|
pat = '|'.join(map(codepoint_to_chr, charlist))
|
||||||
if is_binary:
|
if is_binary:
|
||||||
pat = pat.encode('utf-8')
|
pat = pat.encode('utf-8')
|
||||||
return pat.sub(empty, txt)
|
return pat.sub(empty, txt)
|
||||||
@ -57,15 +58,15 @@ def allowed(x):
|
|||||||
|
|
||||||
|
|
||||||
def py_clean_xml_chars(unicode_string):
|
def py_clean_xml_chars(unicode_string):
|
||||||
return u''.join(filter(allowed, unicode_string))
|
return ''.join(filter(allowed, unicode_string))
|
||||||
|
|
||||||
|
|
||||||
clean_xml_chars = native_clean_xml_chars or py_clean_xml_chars
|
clean_xml_chars = native_clean_xml_chars or py_clean_xml_chars
|
||||||
|
|
||||||
|
|
||||||
def test_clean_xml_chars():
|
def test_clean_xml_chars():
|
||||||
raw = u'asd\x02a\U00010437x\ud801b\udffe\ud802'
|
raw = 'asd\x02a\U00010437x\ud801b\udffe\ud802'
|
||||||
if native_clean_xml_chars(raw) != u'asda\U00010437xb':
|
if native_clean_xml_chars(raw) != 'asda\U00010437xb':
|
||||||
raise ValueError('Failed to XML clean: %r' % raw)
|
raise ValueError('Failed to XML clean: %r' % raw)
|
||||||
|
|
||||||
|
|
||||||
@ -75,7 +76,7 @@ def test_clean_xml_chars():
|
|||||||
# @param text The HTML (or XML) source text.
|
# @param text The HTML (or XML) source text.
|
||||||
# @return The plain text, as a Unicode string, if necessary.
|
# @return The plain text, as a Unicode string, if necessary.
|
||||||
|
|
||||||
def unescape(text, rm=False, rchar=u''):
|
def unescape(text, rm=False, rchar=''):
|
||||||
def fixup(m, rm=rm, rchar=rchar):
|
def fixup(m, rm=rm, rchar=rchar):
|
||||||
text = m.group(0)
|
text = m.group(0)
|
||||||
if text[:2] == "&#":
|
if text[:2] == "&#":
|
||||||
|
@ -6,7 +6,7 @@ Created on 13 Jan 2011
|
|||||||
|
|
||||||
@author: charles
|
@author: charles
|
||||||
'''
|
'''
|
||||||
from __future__ import print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
@ -838,7 +838,7 @@ class BuiltinFormatsSizes(BuiltinFormatterFunction):
|
|||||||
def evaluate(self, formatter, kwargs, mi, locals):
|
def evaluate(self, formatter, kwargs, mi, locals):
|
||||||
fmt_data = mi.get('format_metadata', {})
|
fmt_data = mi.get('format_metadata', {})
|
||||||
try:
|
try:
|
||||||
return ','.join(k.upper()+':'+str(v['size']) for k,v in iteritems(fmt_data))
|
return ','.join(k.upper()+':'+unicode_type(v['size']) for k,v in iteritems(fmt_data))
|
||||||
except:
|
except:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
@ -857,7 +857,7 @@ class BuiltinFormatsPaths(BuiltinFormatterFunction):
|
|||||||
def evaluate(self, formatter, kwargs, mi, locals):
|
def evaluate(self, formatter, kwargs, mi, locals):
|
||||||
fmt_data = mi.get('format_metadata', {})
|
fmt_data = mi.get('format_metadata', {})
|
||||||
try:
|
try:
|
||||||
return ','.join(k.upper()+':'+str(v['path']) for k,v in iteritems(fmt_data))
|
return ','.join(k.upper()+':'+unicode_type(v['path']) for k,v in iteritems(fmt_data))
|
||||||
except:
|
except:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
@ -1088,7 +1088,7 @@ class BuiltinBooksize(BuiltinFormatterFunction):
|
|||||||
try:
|
try:
|
||||||
v = mi._proxy_metadata.book_size
|
v = mi._proxy_metadata.book_size
|
||||||
if v is not None:
|
if v is not None:
|
||||||
return str(mi._proxy_metadata.book_size)
|
return unicode_type(mi._proxy_metadata.book_size)
|
||||||
return ''
|
return ''
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
from __future__ import with_statement
|
# License: GPLv3 Copyright: 2010, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
|
||||||
__docformat__ = 'restructuredtext en'
|
|
||||||
|
|
||||||
from calibre.constants import iswindows, islinux, isbsd
|
from calibre.constants import iswindows, islinux, isbsd
|
||||||
from calibre.utils.config_base import tweaks
|
from calibre.utils.config_base import tweaks
|
||||||
|
Loading…
x
Reference in New Issue
Block a user