[datafeed] introduce a host mapping so dev instance may transparently redirect request to another host than the actual's one
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Feb 2011 18:06:29 +0100
changeset 6963 5774d4ba4306
parent 6962 220e32f058be
child 6964 4813efcee2c6
[datafeed] introduce a host mapping so dev instance may transparently redirect request to another host than the actual's one
sobjects/parsers.py
--- a/sobjects/parsers.py	Wed Feb 09 18:06:27 2011 +0100
+++ b/sobjects/parsers.py	Wed Feb 09 18:06:29 2011 +0100
@@ -19,6 +19,7 @@
 
 import urllib2
 import StringIO
+import os.path as osp
 from cookielib import CookieJar
 from datetime import datetime, timedelta
 
@@ -193,6 +194,10 @@
         if not url.startswith('http'):
             stream = StringIO.StringIO(url)
         else:
+            for mappedurl in HOST_MAPPING:
+                if url.startswith(mappedurl):
+                    url = url.replace(mappedurl, HOST_MAPPING[mappedurl], 1)
+                    break
             self.source.info('GET %s', url)
             stream = _OPENER.open(url)
         return _entity_etree(etree.parse(stream).getroot())
@@ -334,3 +339,13 @@
         else:
             rql = 'SET %s, Y eid IN (%s), NOT X %s Y' % (rql, eidstr, rtype)
         self._cw.execute(rql, {'x': entity.eid})
+
+def registration_callback(vreg):
+    global HOST_MAPPING
+    vreg.register_all(globals().values(), __name__)
+    host_mapping_file = osp.join(vreg.config.apphome, 'hostmapping.py')
+    if osp.exists(host_mapping_file):
+        HOST_MAPPING = eval(file(host_mapping_file).read())
+        vreg.info('using host mapping %s from %s', HOST_MAPPING, host_mapping_file)
+    else:
+        HOST_MAPPING = {}