Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
macurl2path Namespace Reference

Functions

def url2pathname
 
def pathname2url
 
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.pathname2url (   pathname)

Definition at line 51 of file macurl2path.py.

References dospath.join(), and urllib.quote().

51 
52 def pathname2url(pathname):
53  "convert mac pathname to /-delimited pathname"
54  if '/' in pathname:
55  raise RuntimeError, "Cannot convert pathname containing slashes"
56  components = pathname.split(':')
57  # Remove empty first and/or last component
58  if components[0] == '':
59  del components[0]
60  if components[-1] == '':
61  del components[-1]
62  # Replace empty string ('::') by .. (will result in '/../' later)
63  for i in range(len(components)):
64  if components[i] == '':
65  components[i] = '..'
66  # Truncate names longer than 31 bytes
67  components = map(_pncomp2url, components)
68 
69  if os.path.isabs(pathname):
70  return '/' + '/'.join(components)
71  else:
72  return '/'.join(components)
def macurl2path.test ( )

Definition at line 77 of file macurl2path.py.

References pathname2url(), and url2pathname().

77 
78 def test():
79  for url in ["index.html",
80  "bar/index.html",
81  "/foo/bar/index.html",
82  "/foo/bar/",
83  "/"]:
84  print `url`, '->', `url2pathname(url)`
85  for path in ["drive:",
86  "drive:dir:",
87  "drive:dir:file",
88  "drive:file",
89  "file",
90  ":file",
91  ":dir:",
92  ":dir:file"]:
93  print `path`, '->', `pathname2url(path)`
def macurl2path.url2pathname (   pathname)

Definition at line 10 of file macurl2path.py.

References dospath.join(), urllib.splittype(), and urllib.unquote().

10 
11 def url2pathname(pathname):
12  "Convert /-delimited pathname to mac pathname"
13  #
14  # XXXX The .. handling should be fixed...
15  #
16  tp = urllib.splittype(pathname)[0]
17  if tp and tp != 'file':
18  raise RuntimeError, 'Cannot convert non-local URL to pathname'
19  # Turn starting /// into /, an empty hostname means current host
20  if pathname[:3] == '///':
21  pathname = pathname[2:]
22  elif pathname[:2] == '//':
23  raise RuntimeError, 'Cannot convert non-local URL to pathname'
24  components = pathname.split('/')
25  # Remove . and embedded ..
26  i = 0
27  while i < len(components):
28  if components[i] == '.':
29  del components[i]
30  elif components[i] == '..' and i > 0 and \
31  components[i-1] not in ('', '..'):
32  del components[i-1:i+1]
33  i = i-1
34  elif components[i] == '' and i > 0 and components[i-1] != '':
35  del components[i]
36  else:
37  i = i+1
38  if not components[0]:
39  # Absolute unix path, don't start with colon
40  rv = ':'.join(components[1:])
41  else:
42  # relative unix path, start with colon. First replace
43  # leading .. by empty strings (giving ::file)
44  i = 0
45  while i < len(components) and components[i] == '..':
46  components[i] = ''
47  i = i + 1
48  rv = ':' + ':'.join(components)
49  # and finally unquote slashes and other funny characters
50  return urllib.unquote(rv)

Variable Documentation

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

Definition at line 8 of file macurl2path.py.