]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """Convert a NT pathname to a file URL and vice versa."""\r |
2 | \r | |
3 | def url2pathname(url):\r | |
4 | """OS-specific conversion from a relative URL of the 'file' scheme\r | |
5 | to a file system path; not recommended for general use."""\r | |
6 | # e.g.\r | |
7 | # ///C|/foo/bar/spam.foo\r | |
8 | # becomes\r | |
9 | # C:\foo\bar\spam.foo\r | |
10 | import string, urllib\r | |
11 | # Windows itself uses ":" even in URLs.\r | |
12 | url = url.replace(':', '|')\r | |
13 | if not '|' in url:\r | |
14 | # No drive specifier, just convert slashes\r | |
15 | if url[:4] == '////':\r | |
16 | # path is something like ////host/path/on/remote/host\r | |
17 | # convert this to \\host\path\on\remote\host\r | |
18 | # (notice halving of slashes at the start of the path)\r | |
19 | url = url[2:]\r | |
20 | components = url.split('/')\r | |
21 | # make sure not to convert quoted slashes :-)\r | |
22 | return urllib.unquote('\\'.join(components))\r | |
23 | comp = url.split('|')\r | |
24 | if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:\r | |
25 | error = 'Bad URL: ' + url\r | |
26 | raise IOError, error\r | |
27 | drive = comp[0][-1].upper()\r | |
28 | path = drive + ':'\r | |
29 | components = comp[1].split('/')\r | |
30 | for comp in components:\r | |
31 | if comp:\r | |
32 | path = path + '\\' + urllib.unquote(comp)\r | |
33 | # Issue #11474: url like '/C|/' should convert into 'C:\\'\r | |
34 | if path.endswith(':') and url.endswith('/'):\r | |
35 | path += '\\'\r | |
36 | return path\r | |
37 | \r | |
38 | def pathname2url(p):\r | |
39 | """OS-specific conversion from a file system path to a relative URL\r | |
40 | of the 'file' scheme; not recommended for general use."""\r | |
41 | # e.g.\r | |
42 | # C:\foo\bar\spam.foo\r | |
43 | # becomes\r | |
44 | # ///C|/foo/bar/spam.foo\r | |
45 | import urllib\r | |
46 | if not ':' in p:\r | |
47 | # No drive specifier, just convert slashes and quote the name\r | |
48 | if p[:2] == '\\\\':\r | |
49 | # path is something like \\host\path\on\remote\host\r | |
50 | # convert this to ////host/path/on/remote/host\r | |
51 | # (notice doubling of slashes at the start of the path)\r | |
52 | p = '\\\\' + p\r | |
53 | components = p.split('\\')\r | |
54 | return urllib.quote('/'.join(components))\r | |
55 | comp = p.split(':')\r | |
56 | if len(comp) != 2 or len(comp[0]) > 1:\r | |
57 | error = 'Bad path: ' + p\r | |
58 | raise IOError, error\r | |
59 | \r | |
60 | drive = urllib.quote(comp[0].upper())\r | |
61 | components = comp[1].split('\\')\r | |
62 | path = '///' + drive + ':'\r | |
63 | for comp in components:\r | |
64 | if comp:\r | |
65 | path = path + '/' + urllib.quote(comp)\r | |
66 | return path\r |