示例#1
0
 def test_path_abs(self):
     self.assertEqual(abs(Path('foo')), Path('/foo'))
示例#2
0
 def test_path_join(self):
     self.assertEqual(Path('/foo') + Path('/bar'), Path('/foo/bar'))
示例#3
0
 def test_path_comparison(self):
     self.assertEqual(Path('/foo'), (unicode(''), unicode('foo')))
     self.assertEqual(Path('/foo'), [unicode(''), unicode('foo')])
     self.assertEqual(Path('/foo'), unicode('/foo'))
示例#4
0
 class MockOb(object):
     path = Path()
示例#5
0
 def test_path_path(self):
     self.assertEquals(Path(Path('/foo')), [unicode(''), unicode('foo')])
示例#6
0
class URL(object):
    path = Path()
    query = QueryString()
    params = QueryString(assignment="=", separator=";")

    def __init__(self,
                 url=None,
                 scheme=None,
                 user=None,
                 password=None,
                 host=None,
                 port=None,
                 path=None,
                 params=None,
                 query=None,
                 fragment=None):
        """Construct a URL object.
        
        If arguments other than url are provided, they are used as defaults.  The parse result of the real URL overrides these.
        """

        self.scheme = scheme
        self.user = user
        self.password = password
        self.host = host
        self.port = port
        self.path = path
        self.params = params
        self.query = query
        self.fragment = fragment

        if url is None:
            return

        if (url[0] == '/' or url[1] == ':'):
            url = "file://" + url

        result = urlparse(url)

        self.scheme = result.scheme or self.scheme

        result = urlparse(url.replace(self.scheme + '://', 'http://'))

        self.user = result.username or self.user
        self.password = result.password or self.password
        self.host = result.hostname or self.host
        self.port = result.port or self.port
        self.path = result.path or self.path
        self.params = result.params or self.params
        self.query = result.query or self.query
        self.fragment = result.fragment or self.fragment

    def render(self, safe=False):
        parts = []

        parts.append((self.scheme + "://") if self.scheme else "")
        parts.append(self.user or "")
        parts.append((":" + self.password) if self.user else (
            "@" if self.user else ""))
        parts.append(self.host or "")
        parts.append((":" + str(self.port)) if self.port else "")
        parts.append(unicode(self.path) or "/")
        parts.append((";" + unicode(self.params)) if self.params else "")
        parts.append(("?" + unicode(self.query)) if self.query else "")
        parts.append(("#" +
                      quote_plus(self.fragment)) if self.fragment else "")

        return "".join(parts)

    def __repr__(self):
        return native(self.render(True))

    def __str__(self):
        return bytestring(self.render())

    def __unicode__(self):
        return self.render()