Back to index

python3.2  3.2.2
Functions | Variables
macurl2path Namespace Reference

Functions

def url2pathname
def pathname2url
def _pncomp2url
def test

Variables

list __all__ = ["url2pathname","pathname2url"]

Detailed Description

Macintosh-specific module for conversion between pathnames and URLs.

Do not import directly; use urllib instead.

Function Documentation

def macurl2path._pncomp2url (   component) [private]

Definition at line 75 of file macurl2path.py.

00075 
00076 def _pncomp2url(component):
00077     # We want to quote slashes
00078     return urllib.parse.quote(component[:31], safe='')

Here is the call graph for this function:

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

Definition at line 52 of file macurl2path.py.

00052 
00053 def pathname2url(pathname):
00054     """OS-specific conversion from a file system path to a relative URL
00055     of the 'file' scheme; not recommended for general use."""
00056     if '/' in pathname:
00057         raise RuntimeError("Cannot convert pathname containing slashes")
00058     components = pathname.split(':')
00059     # Remove empty first and/or last component
00060     if components[0] == '':
00061         del components[0]
00062     if components[-1] == '':
00063         del components[-1]
00064     # Replace empty string ('::') by .. (will result in '/../' later)
00065     for i in range(len(components)):
00066         if components[i] == '':
00067             components[i] = '..'
00068     # Truncate names longer than 31 bytes
00069     components = map(_pncomp2url, components)
00070 
00071     if os.path.isabs(pathname):
00072         return '/' + '/'.join(components)
00073     else:
00074         return '/'.join(components)

Here is the caller graph for this function:

Definition at line 79 of file macurl2path.py.

00079 
00080 def test():
00081     for url in ["index.html",
00082                 "bar/index.html",
00083                 "/foo/bar/index.html",
00084                 "/foo/bar/",
00085                 "/"]:
00086         print('%r -> %r' % (url, url2pathname(url)))
00087     for path in ["drive:",
00088                  "drive:dir:",
00089                  "drive:dir:file",
00090                  "drive:file",
00091                  "file",
00092                  ":file",
00093                  ":dir:",
00094                  ":dir:file"]:
00095         print('%r -> %r' % (path, pathname2url(path)))

Here is the call graph for this function:

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

Definition at line 10 of file macurl2path.py.

00010 
00011 def url2pathname(pathname):
00012     """OS-specific conversion from a relative URL of the 'file' scheme
00013     to a file system path; not recommended for general use."""
00014     #
00015     # XXXX The .. handling should be fixed...
00016     #
00017     tp = urllib.parse.splittype(pathname)[0]
00018     if tp and tp != 'file':
00019         raise RuntimeError('Cannot convert non-local URL to pathname')
00020     # Turn starting /// into /, an empty hostname means current host
00021     if pathname[:3] == '///':
00022         pathname = pathname[2:]
00023     elif pathname[:2] == '//':
00024         raise RuntimeError('Cannot convert non-local URL to pathname')
00025     components = pathname.split('/')
00026     # Remove . and embedded ..
00027     i = 0
00028     while i < len(components):
00029         if components[i] == '.':
00030             del components[i]
00031         elif components[i] == '..' and i > 0 and \
00032                                   components[i-1] not in ('', '..'):
00033             del components[i-1:i+1]
00034             i = i-1
00035         elif components[i] == '' and i > 0 and components[i-1] != '':
00036             del components[i]
00037         else:
00038             i = i+1
00039     if not components[0]:
00040         # Absolute unix path, don't start with colon
00041         rv = ':'.join(components[1:])
00042     else:
00043         # relative unix path, start with colon. First replace
00044         # leading .. by empty strings (giving ::file)
00045         i = 0
00046         while i < len(components) and components[i] == '..':
00047             components[i] = ''
00048             i = i + 1
00049         rv = ':' + ':'.join(components)
00050     # and finally unquote slashes and other funny characters
00051     return urllib.parse.unquote(rv)

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

list macurl2path.__all__ = ["url2pathname","pathname2url"]

Definition at line 8 of file macurl2path.py.