Back to index

python3.2  3.2.2
Functions
nturl2path Namespace Reference

Functions

def url2pathname
def pathname2url

Detailed Description

Convert a NT pathname to a file URL and vice versa.

Function Documentation

OS-specific conversion from a file system path to a relative URL
of the 'file' scheme; not recommended for general use.

Definition at line 38 of file nturl2path.py.

00038 
00039 def pathname2url(p):
00040     """OS-specific conversion from a file system path to a relative URL
00041     of the 'file' scheme; not recommended for general use."""
00042     # e.g.
00043     # C:\foo\bar\spam.foo
00044     # becomes
00045     # ///C|/foo/bar/spam.foo
00046     import urllib.parse
00047     if not ':' in p:
00048         # No drive specifier, just convert slashes and quote the name
00049         if p[:2] == '\\\\':
00050         # path is something like \\host\path\on\remote\host
00051         # convert this to ////host/path/on/remote/host
00052         # (notice doubling of slashes at the start of the path)
00053             p = '\\\\' + p
00054         components = p.split('\\')
00055         return urllib.parse.quote('/'.join(components))
00056     comp = p.split(':')
00057     if len(comp) != 2 or len(comp[0]) > 1:
00058         error = 'Bad path: ' + p
00059         raise IOError(error)
00060 
00061     drive = urllib.parse.quote(comp[0].upper())
00062     components = comp[1].split('\\')
00063     path = '///' + drive + ':'
00064     for comp in components:
00065         if comp:
00066             path = path + '/' + urllib.parse.quote(comp)
00067     return path

Here is the call graph for this function:

Here is the caller graph for this function:

def nturl2path.url2pathname (   url)
OS-specific conversion from a relative URL of the 'file' scheme
to a file system path; not recommended for general use.

Definition at line 3 of file nturl2path.py.

00003 
00004 def url2pathname(url):
00005     """OS-specific conversion from a relative URL of the 'file' scheme
00006     to a file system path; not recommended for general use."""
00007     # e.g.
00008     # ///C|/foo/bar/spam.foo
00009     # becomes
00010     # C:\foo\bar\spam.foo
00011     import string, urllib.parse
00012     # Windows itself uses ":" even in URLs.
00013     url = url.replace(':', '|')
00014     if not '|' in url:
00015         # No drive specifier, just convert slashes
00016         if url[:4] == '////':
00017             # path is something like ////host/path/on/remote/host
00018             # convert this to \\host\path\on\remote\host
00019             # (notice halving of slashes at the start of the path)
00020             url = url[2:]
00021         components = url.split('/')
00022         # make sure not to convert quoted slashes :-)
00023         return urllib.parse.unquote('\\'.join(components))
00024     comp = url.split('|')
00025     if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
00026         error = 'Bad URL: ' + url
00027         raise IOError(error)
00028     drive = comp[0][-1].upper()
00029     components = comp[1].split('/')
00030     path = drive + ':'
00031     for comp in components:
00032         if comp:
00033             path = path + '\\' + urllib.parse.unquote(comp)
00034     # Issue #11474 - handing url such as |c/|
00035     if path.endswith(':') and url.endswith('/'):
00036         path += '\\'
00037     return path

Here is the call graph for this function: