8000 [FIX] _extensions: p3 compatibility (6) · AllenChen7/documentation-user@478fcb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 478fcb7

Browse files
committed
[FIX] _extensions: p3 compatibility (6)
1 parent b22590c commit 478fcb7

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

_extensions/odoo/pycompat.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
import sys
3+
4+
PY2 = sys.version_info[0] == 2
5+
6+
if PY2:
7+
text_type = unicode
8+
else:
9+
text_type = str
10+
11+
def to_text(source):
12+
""" Generates a text value (an instance of text_type) from an arbitrary
13+
source.
14+
15+
* False and None are converted to empty strings
16+
* text is passed through
17+
* bytes are decoded as UTF-8
18+
* rest is textified via the current version's relevant data model method
19+
"""
20+
if source is None or source is False:
21+
return u''
22+
23+
if isinstance(source, bytes):
24+
return source.decode('utf-8')
25+
26+
return text_type(source)
27+

_extensions/odoo/translator.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from sphinx import addnodes, util
99
from sphinx.locale import admonitionlabels
1010

11+
from . import pycompat
12+
1113

1214
def _parents(node):
1315
while node.parent:
@@ -59,7 +61,7 @@ def __init__(self, builder, document):
5961
self.param_separator = ','
6062

6163
def encode(self, text):
62-
return unicode(text).translate({
64+
return pycompat.to_text(text).translate({
6365
ord('&'): u'&',
6466
ord('<'): u'&lt;',
6567
ord('"'): u'&quot;',
@@ -68,7 +70,7 @@ def encode(self, text):
6870
})
6971

7072
def starttag(self, node, tagname, **attributes):
71-
tagname = unicode(tagname).lower()
73+
tagname = pycompat.to_text(tagname).lower()
7274

7375
# extract generic attributes
7476
attrs = {name.lower(): value for name, value in attributes.items()}
@@ -103,7 +105,7 @@ def starttag(self, node, tagname, **attributes):
103105
# only "space characters" SPACE, CHARACTER TABULATION, LINE FEED,
104106
# FORM FEED and CARRIAGE RETURN should be collapsed, not al White_Space
105107
def attval(self, value, whitespace=re.compile(u'[ \t\n\f\r]')):
106-
return self.encode(whitespace.sub(u' ', unicode(value)))
108+
return self.encode(whitespace.sub(u' ', pycompat.to_text(value)))
107109

108110
def astext(self):
109111
return u''.join(self.body)

0 commit comments

Comments
 (0)
0