[Inteproxy-commits] r322 - in branches/streaming: . inteproxy

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Dec 23 10:50:40 CET 2011


Author: teichmann
Date: 2011-12-23 10:50:40 +0100 (Fri, 23 Dec 2011)
New Revision: 322

Modified:
   branches/streaming/ChangeLog
   branches/streaming/inteproxy/transcoder.py
Log:
Added a function to the transcoder to do url rewriting more than once.

Modified: branches/streaming/ChangeLog
===================================================================
--- branches/streaming/ChangeLog	2011-12-22 17:44:10 UTC (rev 321)
+++ branches/streaming/ChangeLog	2011-12-23 09:50:40 UTC (rev 322)
@@ -1,3 +1,10 @@
+2011-12-23	Sascha L. Teichmann	<sascha.teichmann at intevation.de>
+
+	* inteproxy/transcoder.py: Refactored a bit. Introduced
+	  function url_rewriter which returns a function which 
+	  can be used to do url rewriting for a given string.
+	  Useful when rewriting is called more than once.
+
 2011-06-16  Bjoern Schilberg <bjoern.schilberg at intevation.de>
 
 	* M server/doc/source/gettingStarted.rstr:

Modified: branches/streaming/inteproxy/transcoder.py
===================================================================
--- branches/streaming/inteproxy/transcoder.py	2011-12-22 17:44:10 UTC (rev 321)
+++ branches/streaming/inteproxy/transcoder.py	2011-12-23 09:50:40 UTC (rev 322)
@@ -284,6 +284,33 @@
                                 (scheme, netloc, path, query, fragment),
                                 rule)
 
+    def build_url_patterns(self):
+        """TODO: Document me!"""
+
+        return ["%s%s" % (host_regex.pattern, path_regex.pattern)
+                for host_regex, path_regex, classname in self.rules]
+
+    def build_url_regex(self):
+        """TODO: Document me!"""
+
+        return ("(?:http|https)://(?:%s)" %
+                "|".join("(" + pattern + ")"
+                         for pattern in self.build_url_patterns()))
+        
+    def url_rewriter(self, prefix, log_debug):
+        """TODO: Document me!"""
+
+        pattern = re.compile(self.build_url_regex())
+
+        def make_inteprox_url(match):
+            url = match.group(match.lastindex)
+            return prefix + url
+
+        def rewrite(data):
+            return pattern.sub(make_inteprox_url, data)
+
+        return rewrite
+
     def rewrite_urls(self, data, prefix, log_debug):
         """Prefix all known URLs in data with prefix.
 
@@ -301,15 +328,9 @@
         messages, usually the log_debug method of the
         InteProxyHTTPRequestHandler.
         """
-        url_patterns = []
-        for host_regex, path_regex, classname in self.rules:
-            url_patterns.append("%s%s"
-                                % (host_regex.pattern, path_regex.pattern))
-        regex = ("(?:http|https)://(?:"
-                 + "|".join("(" + pattern + ")"
-                            for pattern in url_patterns)
-                 + ")")
 
+        regex = self.build_url_regex()
+
         def make_inteprox_url(match):
             url = match.group(match.lastindex)
             return prefix + url



More information about the Inteproxy-commits mailing list