[FIX] _extensions: p3 compatibility (6)
This commit is contained in:
parent
b22590c2da
commit
478fcb7bef
27
_extensions/odoo/pycompat.py
Normal file
27
_extensions/odoo/pycompat.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
|
||||||
|
PY2 = sys.version_info[0] == 2
|
||||||
|
|
||||||
|
if PY2:
|
||||||
|
text_type = unicode
|
||||||
|
else:
|
||||||
|
text_type = str
|
||||||
|
|
||||||
|
def to_text(source):
|
||||||
|
""" Generates a text value (an instance of text_type) from an arbitrary
|
||||||
|
source.
|
||||||
|
|
||||||
|
* False and None are converted to empty strings
|
||||||
|
* text is passed through
|
||||||
|
* bytes are decoded as UTF-8
|
||||||
|
* rest is textified via the current version's relevant data model method
|
||||||
|
"""
|
||||||
|
if source is None or source is False:
|
||||||
|
return u''
|
||||||
|
|
||||||
|
if isinstance(source, bytes):
|
||||||
|
return source.decode('utf-8')
|
||||||
|
|
||||||
|
return text_type(source)
|
||||||
|
|
@ -8,6 +8,8 @@ from docutils import nodes
|
|||||||
from sphinx import addnodes, util
|
from sphinx import addnodes, util
|
||||||
from sphinx.locale import admonitionlabels
|
from sphinx.locale import admonitionlabels
|
||||||
|
|
||||||
|
from . import pycompat
|
||||||
|
|
||||||
|
|
||||||
def _parents(node):
|
def _parents(node):
|
||||||
while node.parent:
|
while node.parent:
|
||||||
@ -59,7 +61,7 @@ class BootstrapTranslator(nodes.NodeVisitor, object):
|
|||||||
self.param_separator = ','
|
self.param_separator = ','
|
||||||
|
|
||||||
def encode(self, text):
|
def encode(self, text):
|
||||||
return unicode(text).translate({
|
return pycompat.to_text(text).translate({
|
||||||
ord('&'): u'&',
|
ord('&'): u'&',
|
||||||
ord('<'): u'<',
|
ord('<'): u'<',
|
||||||
ord('"'): u'"',
|
ord('"'): u'"',
|
||||||
@ -68,7 +70,7 @@ class BootstrapTranslator(nodes.NodeVisitor, object):
|
|||||||
})
|
})
|
||||||
|
|
||||||
def starttag(self, node, tagname, **attributes):
|
def starttag(self, node, tagname, **attributes):
|
||||||
tagname = unicode(tagname).lower()
|
tagname = pycompat.to_text(tagname).lower()
|
||||||
|
|
||||||
# extract generic attributes
|
# extract generic attributes
|
||||||
attrs = {name.lower(): value for name, value in attributes.items()}
|
attrs = {name.lower(): value for name, value in attributes.items()}
|
||||||
@ -103,7 +105,7 @@ class BootstrapTranslator(nodes.NodeVisitor, object):
|
|||||||
# only "space characters" SPACE, CHARACTER TABULATION, LINE FEED,
|
# only "space characters" SPACE, CHARACTER TABULATION, LINE FEED,
|
||||||
# FORM FEED and CARRIAGE RETURN should be collapsed, not al White_Space
|
# FORM FEED and CARRIAGE RETURN should be collapsed, not al White_Space
|
||||||
def attval(self, value, whitespace=re.compile(u'[ \t\n\f\r]')):
|
def attval(self, value, whitespace=re.compile(u'[ \t\n\f\r]')):
|
||||||
return self.encode(whitespace.sub(u' ', unicode(value)))
|
return self.encode(whitespace.sub(u' ', pycompat.to_text(value)))
|
||||||
|
|
||||||
def astext(self):
|
def astext(self):
|
||||||
return u''.join(self.body)
|
return u''.join(self.body)
|
||||||
|
Loading…
Reference in New Issue
Block a user