summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJack Jansen <jack.jansen@cwi.nl>1995-12-21 15:43:53 (GMT)
committerJack Jansen <jack.jansen@cwi.nl>1995-12-21 15:43:53 (GMT)
commite8ea21b0fe21df7b5889ff851751c6614810550e (patch)
tree600f9bfc58441753f93d0279eca1e65a62f9ceea /Lib
parentb67897516787e09db442a24ecdb8db86c12fceb4 (diff)
downloadcpython-e8ea21b0fe21df7b5889ff851751c6614810550e.zip
cpython-e8ea21b0fe21df7b5889ff851751c6614810550e.tar.gz
cpython-e8ea21b0fe21df7b5889ff851751c6614810550e.tar.bz2
Added pathname2url and url2pathname methods (only correct for unix and
mac, so far)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/urllib.py39
1 files changed, 32 insertions, 7 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index b06f6bf..a0ecaac 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -20,22 +20,47 @@ import regex
import os
-__version__ = '1.2'
+__version__ = '1.2' # XXXX Should I update this number? -- jack
# Helper for non-unix systems
if os.name == 'mac':
- def _fixpath(pathname):
+ def url2pathname(pathname):
+ "Convert /-delimited pathname to mac pathname"
+ #
+ # XXXX The .. handling should be fixed...
+ #
+ tp = splittype(pathname)[0]
+ if tp and tp <> 'file':
+ raise RuntimeError, 'Cannot convert non-local URL to pathname'
components = string.split(pathname, '/')
+ if '..' in components or '.' in components or '' in components[1:-1]:
+ raise RuntimeError, 'Cannot convert URL containing ., .. or // to pathname'
if not components[0]:
# Absolute unix path, don't start with colon
return string.join(components[1:], ':')
else:
# relative unix path, start with colon
return ':' + string.join(components, ':')
+
+ def pathname2url(pathname):
+ "convert mac pathname to /-delimited pathname"
+ if '/' in pathname:
+ raise RuntimeError, "Cannot convert pathname containing slashes"
+ components = string.split(pathname, ':')
+ if '' in components[1:-1]:
+ raise RuntimeError, "Cannot convert pathname containing ::"
+ # Truncate names longer than 31 bytes
+ components = map(lambda x: x[:31], components)
+
+ if os.path.isabs(pathname):
+ return '/' + string.join(components, '/')
+ else:
+ return string.join(components, '/')
else:
- def _fixpath(pathname):
+ def url2pathname(pathname):
+ return pathname
+ def pathname2url(pathname):
return pathname
-
# This really consists of two pieces:
# (1) a class which handles opening of all sorts of URLs
@@ -144,7 +169,7 @@ class URLopener:
try:
fp = self.open_local_file(url1)
del fp
- return splithost(url1)[1], None
+ return url2pathname(splithost(url1)[1]), None
except IOError, msg:
pass
fp = self.open(url)
@@ -238,12 +263,12 @@ class URLopener:
# Use local file
def open_local_file(self, url):
host, file = splithost(url)
- if not host: return addinfo(open(_fixpath(file), 'r'), noheaders())
+ if not host: return addinfo(open(url2pathname(file), 'r'), noheaders())
host, port = splitport(host)
if not port and socket.gethostbyname(host) in (
localhost(), thishost()):
file = unquote(file)
- return addinfo(open(_fixpath(file), 'r'), noheaders())
+ return addinfo(open(url2pathname(file), 'r'), noheaders())
raise IOError, ('local file error', 'not on local host')
# Use FTP protocol