[server] unormalize source logger's name to avoid encoding errors
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 08 Sep 2015 06:48:48 +0200
changeset 10832 2350424585f8
parent 10831 f1b5a5ea0da0
child 10833 f585add0fed9
[server] unormalize source logger's name to avoid encoding errors
server/sources/__init__.py
server/test/unittest_datafeed.py
--- a/server/sources/__init__.py	Tue Sep 22 14:20:53 2015 +0200
+++ b/server/sources/__init__.py	Tue Sep 08 06:48:48 2015 +0200
@@ -27,6 +27,7 @@
 from six import text_type
 
 from logilab.common import configuration
+from logilab.common.textutils import unormalize
 from logilab.common.deprecation import deprecated
 
 from yams.schema import role_name
@@ -108,7 +109,9 @@
         self.public_config['use-cwuri-as-url'] = self.use_cwuri_as_url
         self.remove_sensitive_information(self.public_config)
         self.uri = source_config.pop('uri')
-        set_log_methods(self, getLogger('cubicweb.sources.'+self.uri))
+        # unormalize to avoid non-ascii characters in logger's name, this will cause decoding error
+        # on logging
+        set_log_methods(self, getLogger('cubicweb.sources.' + unormalize(unicode(self.uri))))
         source_config.pop('type')
         self.update_config(None, self.check_conf_dict(eid, source_config,
                                                       fail_if_unknown=False))
--- a/server/test/unittest_datafeed.py	Tue Sep 22 14:20:53 2015 +0200
+++ b/server/test/unittest_datafeed.py	Tue Sep 08 06:48:48 2015 +0200
@@ -1,3 +1,4 @@
+# coding: utf-8
 # copyright 2011-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
@@ -27,7 +28,7 @@
     def setup_database(self):
         with self.admin_access.repo_cnx() as cnx:
             with self.base_parser(cnx):
-                cnx.create_entity('CWSource', name=u'myfeed', type=u'datafeed',
+                cnx.create_entity('CWSource', name=u'ô myfeed', type=u'datafeed',
                                   parser=u'testparser', url=u'ignored',
                                   config=u'synchronization-interval=1min')
                 cnx.commit()
@@ -47,21 +48,23 @@
                 entity.cw_edited.update(sourceparams['item'])
 
         with self.temporary_appobjects(AParser):
-            if 'myfeed' in self.repo.sources_by_uri:
-                yield self.repo.sources_by_uri['myfeed']._get_parser(session)
+            if u'ô myfeed' in self.repo.sources_by_uri:
+                yield self.repo.sources_by_uri[u'ô myfeed']._get_parser(session)
             else:
                 yield
 
     def test(self):
-        self.assertIn('myfeed', self.repo.sources_by_uri)
-        dfsource = self.repo.sources_by_uri['myfeed']
+        self.assertIn(u'ô myfeed', self.repo.sources_by_uri)
+        dfsource = self.repo.sources_by_uri[u'ô myfeed']
         self.assertNotIn('use_cwuri_as_url', dfsource.__dict__)
-        self.assertEqual({'type': u'datafeed', 'uri': u'myfeed', 'use-cwuri-as-url': True},
+        self.assertEqual({'type': u'datafeed', 'uri': u'ô myfeed', 'use-cwuri-as-url': True},
                          dfsource.public_config)
         self.assertEqual(dfsource.use_cwuri_as_url, True)
         self.assertEqual(dfsource.latest_retrieval, None)
         self.assertEqual(dfsource.synchro_interval, timedelta(seconds=60))
         self.assertFalse(dfsource.fresh())
+        # ensure source's logger name has been unormalized
+        self.assertEqual(dfsource.info.__self__.name, 'cubicweb.sources.o myfeed')
 
         with self.repo.internal_cnx() as cnx:
             with self.base_parser(cnx):
@@ -77,16 +80,16 @@
                 self.assertEqual(entity.title, 'cubicweb.org')
                 self.assertEqual(entity.content, 'the cw web site')
                 self.assertEqual(entity.cwuri, 'http://www.cubicweb.org/')
-                self.assertEqual(entity.cw_source[0].name, 'myfeed')
+                self.assertEqual(entity.cw_source[0].name, u'ô myfeed')
                 self.assertEqual(entity.cw_metainformation(),
                                  {'type': 'Card',
-                                  'source': {'uri': 'myfeed', 'type': 'datafeed', 'use-cwuri-as-url': True},
+                                  'source': {'uri': u'ô myfeed', 'type': 'datafeed', 'use-cwuri-as-url': True},
                                   'extid': b'http://www.cubicweb.org/'}
                                  )
                 self.assertEqual(entity.absolute_url(), 'http://www.cubicweb.org/')
                 # test repo cache keys
                 self.assertEqual(self.repo._type_source_cache[entity.eid],
-                                 ('Card', b'http://www.cubicweb.org/', 'myfeed'))
+                                 ('Card', b'http://www.cubicweb.org/', u'ô myfeed'))
                 self.assertEqual(self.repo._extid_cache[b'http://www.cubicweb.org/'],
                                  entity.eid)
                 # test repull
@@ -100,19 +103,18 @@
                 self.assertEqual(stats['created'], set())
                 self.assertEqual(stats['updated'], set((entity.eid,)))
                 self.assertEqual(self.repo._type_source_cache[entity.eid],
-                                 ('Card', b'http://www.cubicweb.org/', 'myfeed'))
+                                 ('Card', b'http://www.cubicweb.org/', u'ô myfeed'))
                 self.assertEqual(self.repo._extid_cache[b'http://www.cubicweb.org/'],
                                  entity.eid)
 
                 self.assertEqual(dfsource.source_cwuris(cnx),
-                                 {b'http://www.cubicweb.org/': (entity.eid, 'Card')}
-                             )
+                                 {b'http://www.cubicweb.org/': (entity.eid, 'Card')})
                 self.assertTrue(dfsource.latest_retrieval)
                 self.assertTrue(dfsource.fresh())
 
         # test_rename_source
         with self.admin_access.repo_cnx() as cnx:
-            cnx.execute('SET S name "myrenamedfeed" WHERE S is CWSource, S name "myfeed"')
+            cnx.entity_from_eid(dfsource.eid).cw_set(name=u"myrenamedfeed")
             cnx.commit()
             entity = cnx.execute('Card X').get_entity(0, 0)
             self.assertEqual(entity.cwuri, 'http://www.cubicweb.org/')