Convert the given file URL to a local file system path. The 'file:' scheme prefix must be omitted unless *require_scheme* is set to true. The URL authority may be resolved with gethostbyname() if *resolve_host* is set to true.
(url, *, require_scheme=False, resolve_host=False)
| 1659 | # Code moved from the old urllib module |
| 1660 | |
| 1661 | def url2pathname(url, *, require_scheme=False, resolve_host=False): |
| 1662 | """Convert the given file URL to a local file system path. |
| 1663 | |
| 1664 | The 'file:' scheme prefix must be omitted unless *require_scheme* |
| 1665 | is set to true. |
| 1666 | |
| 1667 | The URL authority may be resolved with gethostbyname() if |
| 1668 | *resolve_host* is set to true. |
| 1669 | """ |
| 1670 | if not require_scheme: |
| 1671 | url = 'file:' + url |
| 1672 | scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment. |
| 1673 | if scheme != 'file': |
| 1674 | raise URLError("URL is missing a 'file:' scheme") |
| 1675 | if os.name == 'nt': |
| 1676 | if authority[1:2] == ':': |
| 1677 | # e.g. file://c:/file.txt |
| 1678 | url = authority + url |
| 1679 | elif not _is_local_authority(authority, resolve_host): |
| 1680 | # e.g. file://server/share/file.txt |
| 1681 | url = '//' + authority + url |
| 1682 | elif url[:3] == '///': |
| 1683 | # e.g. file://///server/share/file.txt |
| 1684 | url = url[1:] |
| 1685 | else: |
| 1686 | if url[:1] == '/' and url[2:3] in (':', '|'): |
| 1687 | # Skip past extra slash before DOS drive in URL path. |
| 1688 | url = url[1:] |
| 1689 | if url[1:2] == '|': |
| 1690 | # Older URLs use a pipe after a drive letter |
| 1691 | url = url[:1] + ':' + url[2:] |
| 1692 | url = url.replace('/', '\\') |
| 1693 | elif not _is_local_authority(authority, resolve_host): |
| 1694 | raise URLError("file:// scheme is supported only on localhost") |
| 1695 | encoding = sys.getfilesystemencoding() |
| 1696 | errors = sys.getfilesystemencodeerrors() |
| 1697 | return unquote(url, encoding=encoding, errors=errors) |
| 1698 | |
| 1699 | |
| 1700 | def pathname2url(pathname, *, add_scheme=False): |
no test coverage detected
searching dependent graphs…