File: url.py

package info (click to toggle)
zconfig 2.7.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 896 kB
  • ctags: 769
  • sloc: python: 5,196; xml: 329; perl: 37; makefile: 32; sh: 1
file content (67 lines) | stat: -rw-r--r-- 2,104 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
##############################################################################
#
# Copyright (c) 2002, 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""urlparse-like helpers that normalize file: URLs.

ZConfig and urllib2 expect file: URLs to consistently use the '//'
hostpart seperator; the functions here enforce this constraint.
"""

import urlparse as _urlparse

try:
    from urlparse import urlsplit
except ImportError:
    def urlsplit(url):
        # Check for the fragment here, since Python 2.1.3 didn't get
        # it right for things like "http://www.python.org#frag".
        if '#' in url:
            url, fragment = url.split('#', 1)
        else:
            fragment = ''
        parts = list(_urlparse.urlparse(url))
        parts[-1] = fragment
        param = parts.pop(3)
        if param:
            parts[2] += ";" + param
        return tuple(parts)


def urlnormalize(url):
    lc = url.lower()
    if lc.startswith("file:/") and not lc.startswith("file:///"):
        url = "file://" + url[5:]
    return url


def urlunsplit(parts):
    parts = list(parts)
    parts.insert(3, '')
    url = _urlparse.urlunparse(tuple(parts))
    if (parts[0] == "file"
        and url.startswith("file:/")
        and not url.startswith("file:///")):
        url = "file://" + url[5:]
    return url


def urldefrag(url):
    url, fragment = _urlparse.urldefrag(url)
    return urlnormalize(url), fragment


def urljoin(base, relurl):
    url = _urlparse.urljoin(base, relurl)
    if url.startswith("file:/") and not url.startswith("file:///"):
        url = "file://" + url[5:]
    return url