[pypy-svn] r38718 - in pypy/dist/pypy/translator/js/lib: . test

fijal at codespeak.net fijal at codespeak.net
Tue Feb 13 17:12:22 CET 2007


Author: fijal
Date: Tue Feb 13 17:12:19 2007
New Revision: 38718

Added:
   pypy/dist/pypy/translator/js/lib/test/test_url.py
   pypy/dist/pypy/translator/js/lib/url.py
Log:
Add proper URL handling


Added: pypy/dist/pypy/translator/js/lib/test/test_url.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/lib/test/test_url.py	Tue Feb 13 17:12:19 2007
@@ -0,0 +1,10 @@
+
+from pypy.translator.js.lib.url import parse_url
+
+def test_url():
+    assert parse_url("path") == (["path"], {})
+    assert parse_url("a/b/c/d") == (["a", "b", "c", "d"], {})
+    assert parse_url("/a/b") == (["a", "b"], {})
+    assert parse_url("/a/b/c/") == (["a", "b", "c"], {})
+    assert parse_url("a/b?q=a&c=z") == (["a","b"], {"q":"a", "c":"z"})
+

Added: pypy/dist/pypy/translator/js/lib/url.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/lib/url.py	Tue Feb 13 17:12:19 2007
@@ -0,0 +1,35 @@
+
+""" Some support files for mapping urls, mostly bindings
+for existing cgi stuff
+"""
+
+import cgi
+import urllib
+
+class URL(object):
+    def __init__(self, path, vars):
+        self.path = path
+        self.vars = vars
+
+    def __eq__(self, other):
+        if isinstance(other, URL):
+            return self.path == other.path and self.vars == other.vars
+        if isinstance(other, tuple):
+            if len(other) != 2:
+                return False
+            return self.path, self.vars == other
+        return False
+
+    def __ne__(self, other):
+        return not self == other
+
+def parse_url(path):
+    """ Parse a/b/c?q=a into ('a', 'b', 'c') {'q':'a'}
+    """
+    if '?' in path:
+        path, var_str = path.split("?")
+        vars = cgi.parse_qs(var_str)
+    else:
+        vars = {}
+    parts = [urllib.unquote(i) for i in path.split("/") if i]
+    return URL(parts, vars)



More information about the Pypy-commit mailing list