You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

macurl2path.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Macintosh-specific module for conversion between pathnames and URLs.
  2. Do not import directly; use urllib instead."""
  3. import urllib
  4. import os
  5. __all__ = ["url2pathname","pathname2url"]
  6. def url2pathname(pathname):
  7. "Convert /-delimited pathname to mac pathname"
  8. #
  9. # XXXX The .. handling should be fixed...
  10. #
  11. tp = urllib.splittype(pathname)[0]
  12. if tp and tp != 'file':
  13. raise RuntimeError, 'Cannot convert non-local URL to pathname'
  14. # Turn starting /// into /, an empty hostname means current host
  15. if pathname[:3] == '///':
  16. pathname = pathname[2:]
  17. elif pathname[:2] == '//':
  18. raise RuntimeError, 'Cannot convert non-local URL to pathname'
  19. components = pathname.split('/')
  20. # Remove . and embedded ..
  21. i = 0
  22. while i < len(components):
  23. if components[i] == '.':
  24. del components[i]
  25. elif components[i] == '..' and i > 0 and \
  26. components[i-1] not in ('', '..'):
  27. del components[i-1:i+1]
  28. i = i-1
  29. elif components[i] == '' and i > 0 and components[i-1] != '':
  30. del components[i]
  31. else:
  32. i = i+1
  33. if not components[0]:
  34. # Absolute unix path, don't start with colon
  35. rv = ':'.join(components[1:])
  36. else:
  37. # relative unix path, start with colon. First replace
  38. # leading .. by empty strings (giving ::file)
  39. i = 0
  40. while i < len(components) and components[i] == '..':
  41. components[i] = ''
  42. i = i + 1
  43. rv = ':' + ':'.join(components)
  44. # and finally unquote slashes and other funny characters
  45. return urllib.unquote(rv)
  46. def pathname2url(pathname):
  47. "convert mac pathname to /-delimited pathname"
  48. if '/' in pathname:
  49. raise RuntimeError, "Cannot convert pathname containing slashes"
  50. components = pathname.split(':')
  51. # Remove empty first and/or last component
  52. if components[0] == '':
  53. del components[0]
  54. if components[-1] == '':
  55. del components[-1]
  56. # Replace empty string ('::') by .. (will result in '/../' later)
  57. for i in range(len(components)):
  58. if components[i] == '':
  59. components[i] = '..'
  60. # Truncate names longer than 31 bytes
  61. components = map(_pncomp2url, components)
  62. if os.path.isabs(pathname):
  63. return '/' + '/'.join(components)
  64. else:
  65. return '/'.join(components)
  66. def _pncomp2url(component):
  67. component = urllib.quote(component[:31], safe='') # We want to quote slashes
  68. return component
  69. def test():
  70. for url in ["index.html",
  71. "bar/index.html",
  72. "/foo/bar/index.html",
  73. "/foo/bar/",
  74. "/"]:
  75. print `url`, '->', `url2pathname(url)`
  76. for path in ["drive:",
  77. "drive:dir:",
  78. "drive:dir:file",
  79. "drive:file",
  80. "file",
  81. ":file",
  82. ":dir:",
  83. ":dir:file"]:
  84. print `path`, '->', `pathname2url(path)`
  85. if __name__ == '__main__':
  86. test()