summaryrefslogtreecommitdiff
path: root/odoo/tools/pycompat.py
diff options
context:
space:
mode:
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)