[datafeed] use explicit encode/decode instead of casting to str
authorJulien Cristau <julien.cristau@logilab.fr>
Thu, 01 Oct 2015 12:34:49 +0200
changeset 10758 a34edc1057c0
parent 10757 f73a9a884534
child 10759 774b53f7dc3a
[datafeed] use explicit encode/decode instead of casting to str
server/sources/datafeed.py
server/sources/native.py
sobjects/cwxmlparser.py
--- a/server/sources/datafeed.py	Thu Oct 01 12:33:58 2015 +0200
+++ b/server/sources/datafeed.py	Thu Oct 01 12:34:49 2015 +0200
@@ -23,6 +23,7 @@
 from os.path import exists
 from datetime import datetime, timedelta
 
+from six import text_type
 from six.moves.urllib.parse import urlparse
 from six.moves.urllib.request import Request, build_opener, HTTPCookieProcessor
 from six.moves.urllib.error import HTTPError
@@ -390,11 +391,11 @@
         else:
             source = self.source
         sourceparams['parser'] = self
-        if isinstance(uri, unicode):
+        if isinstance(uri, text_type):
             uri = uri.encode('utf-8')
         try:
-            eid = cnx.repo.extid2eid(source, str(uri), etype, cnx,
-                                         sourceparams=sourceparams)
+            eid = cnx.repo.extid2eid(source, uri, etype, cnx,
+                                     sourceparams=sourceparams)
         except ValidationError as ex:
             if raise_on_error:
                 raise
--- a/server/sources/native.py	Thu Oct 01 12:33:58 2015 +0200
+++ b/server/sources/native.py	Thu Oct 01 12:34:49 2015 +0200
@@ -872,8 +872,8 @@
 
     def extid2eid(self, cnx, extid):
         """get eid from an external id. Return None if no record found."""
-        assert isinstance(extid, str)
-        args = {'x': b64encode(extid)}
+        assert isinstance(extid, binary_type)
+        args = {'x': b64encode(extid).decode('ascii')}
         cursor = self.doexec(cnx,
                              'SELECT eid FROM entities WHERE extid=%(x)s',
                              args)
@@ -913,8 +913,8 @@
         # begin by inserting eid/type/source/extid into the entities table
         if extid is not None:
             assert isinstance(extid, binary_type)
-            extid = b64encode(extid)
-        attrs = {'type': text_type(entity.cw_etype), 'eid': entity.eid, 'extid': extid and text_type(extid),
+            extid = b64encode(extid).decode('ascii')
+        attrs = {'type': text_type(entity.cw_etype), 'eid': entity.eid, 'extid': extid,
                  'asource': text_type(source.uri)}
         self._handle_insert_entity_sql(cnx, self.sqlgen.insert('entities', attrs), attrs)
         # insert core relations: is, is_instance_of and cw_source
--- a/sobjects/cwxmlparser.py	Thu Oct 01 12:33:58 2015 +0200
+++ b/sobjects/cwxmlparser.py	Thu Oct 01 12:34:49 2015 +0200
@@ -206,7 +206,7 @@
         * `rels` is for relations and structured as
            {role: {relation: [(related item, related rels)...]}
         """
-        entity = self.extid2entity(str(item['cwuri']), item['cwtype'],
+        entity = self.extid2entity(item['cwuri'].encode('ascii'), item['cwtype'],
                                    cwsource=item['cwsource'], item=item,
                                    raise_on_error=raise_on_error)
         if entity is None: