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

Functions

def url2pathname
 
def pathname2url
 

Detailed Description

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

Function Documentation

def nturl2path.pathname2url (   p)

Definition at line 35 of file nturl2path.py.

References dospath.join(), urllib.quote(), dospath.split(), and string.upper().

35 
36 def pathname2url(p):
37  r"""Convert a DOS path name to a file url.
38 
39  C:\foo\bar\spam.foo
40 
41  becomes
42 
43  ///C|/foo/bar/spam.foo
44  """
45 
46  import urllib
47  if not ':' in p:
48  # No drive specifier, just convert slashes and quote the name
49  if p[:2] == '\\\\':
50  # path is something like \\host\path\on\remote\host
51  # convert this to ////host/path/on/remote/host
52  # (notice doubling of slashes at the start of the path)
53  p = '\\\\' + p
54  components = p.split('\\')
55  return urllib.quote('/'.join(components))
56  comp = p.split(':')
57  if len(comp) != 2 or len(comp[0]) > 1:
58  error = 'Bad path: ' + p
59  raise IOError, error
60 
61  drive = urllib.quote(comp[0].upper())
62  components = comp[1].split('\\')
63  path = '///' + drive + '|'
64  for comp in components:
65  if comp:
66  path = path + '/' + urllib.quote(comp)
67  return path
def nturl2path.url2pathname (   url)

Definition at line 3 of file nturl2path.py.

References dospath.join(), dospath.split(), urllib.unquote(), and string.upper().

3 
4 def url2pathname(url):
5  r"""Convert a URL to a DOS path.
6 
7  ///C|/foo/bar/spam.foo
8 
9  becomes
10 
11  C:\foo\bar\spam.foo
12  """
13  import string, urllib
14  if not '|' in url:
15  # No drive specifier, just convert slashes
16  if url[:4] == '////':
17  # path is something like ////host/path/on/remote/host
18  # convert this to \\host\path\on\remote\host
19  # (notice halving of slashes at the start of the path)
20  url = url[2:]
21  components = url.split('/')
22  # make sure not to convert quoted slashes :-)
23  return urllib.unquote('\\'.join(components))
24  comp = url.split('|')
25  if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
26  error = 'Bad URL: ' + url
27  raise IOError, error
28  drive = comp[0][-1].upper()
29  components = comp[1].split('/')
30  path = drive + ':'
31  for comp in components:
32  if comp:
33  path = path + '\\' + urllib.unquote(comp)
34  return path