summaryrefslogtreecommitdiff
path: root/odoo/tools/pycompat.py
diff options
context:
space:
mode:
authorXavier Morel <xmo@odoo.com>2017-05-02 12:07:30 +0200
committerOlivier Dony <odo@openerp.com>2017-08-20 23:25:54 +0200
commit7dd062f83552fa8ff96224c29d60bafc001d73d3 (patch)
treed507e27e9d5daa91981e04b5aa81a802a9854a8d /odoo/tools/pycompat.py
parent3824b5dcc13678d3985ad45ac21dcf9ac9202c20 (diff)
[FIX] P3: text model types
* remove references to basestring & unicode (use relevant pycompat helpers) * remove some str calls (either entirely or replaced by relevant helper, either text or native) * use better API to avoid unnecessary conversions * remove some XML declarations in views
Diffstat (limited to 'odoo/tools/pycompat.py')
-rw-r--r--odoo/tools/pycompat.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/odoo/tools/pycompat.py b/odoo/tools/pycompat.py
index ff229f0562c..e4111668d24 100644
--- a/odoo/tools/pycompat.py
+++ b/odoo/tools/pycompat.py
@@ -10,6 +10,15 @@ if PY2:
unichr = unichr
text_type = unicode
string_types = (str, unicode)
+ def to_native(source, encoding='utf-8', falsy_empty=False):
+ if not source and falsy_empty:
+ return ''
+
+ if isinstance(source, text_type):
+ return source.encode(encoding)
+
+ return str(source)
+
integer_types = (int, long)
keys = lambda d: iter(d.iterkeys())
@@ -19,6 +28,11 @@ if PY2:
# noinspection PyUnresolvedReferences
from itertools import imap, izip, ifilter
+ def implements_to_string(cls):
+ cls.__unicode__ = cls.__str__
+ cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
+ return cls
+
def implements_iterator(cls):
cls.next = cls.__next__
del cls.__next__
@@ -30,6 +44,15 @@ else:
unichr = chr
text_type = str
string_types = (str,)
+ def to_native(source, encoding='utf-8', falsy_empty=False):
+ if not source and falsy_empty:
+ return ''
+
+ if isinstance(source, bytes):
+ return source.decode(encoding)
+
+ return str(source)
+
integer_types = (int,)
keys = lambda d: iter(d.keys())
@@ -40,6 +63,9 @@ else:
izip = zip
ifilter = filter
+ def implements_to_string(cls):
+ return cls
+
def implements_iterator(cls):
return cls
@@ -47,3 +73,20 @@ else:
if value.__traceback__ != tb:
raise value.with_traceback(tb)
raise value
+
+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)