-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathurl.py
More file actions
165 lines (131 loc) · 5.52 KB
/
Copy pathurl.py
File metadata and controls
165 lines (131 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# This file is part of CairoSVG
# Copyright © 2010-2018 Kozea
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with CairoSVG. If not, see <http://www.gnu.org/licenses/>.
"""
Utils dealing with URLs.
"""
import os.path
import re
from pathlib import Path
from urllib.parse import urljoin, urlparse
from urllib.request import Request, urlopen
from . import VERSION
HTTP_HEADERS = {'User-Agent': 'CairoSVG {}'.format(VERSION)}
URL = re.compile(r'url\((.+)\)')
def normalize_url(url):
"""Normalize ``url`` for underlying NT/Unix operating systems.
The input ``url`` may look like the following:
- C:\\Directory\\zzz.svg
- file://C:\\Directory\\zzz.svg
- zzz.svg
The output ``url`` on NT systems would look like below:
- file:///C:/Directory/zzz.svg
"""
if url and os.name == 'nt' and not url.startswith('data:'):
# Match input ``url`` like the following:
# - C:\\Directory\\zzz.svg
# - Blah.svg
if not url.startswith('file:') and os.path.isabs(url):
url = os.path.abspath(url)
if '#' in url:
url, part = url.rsplit('#', 1)
else:
part = None
url = Path(url).resolve().as_uri()
if part is not None:
url = url + '#' + part
# Match input ``url`` like the following:
# - file://C:\\Directory\\zzz.svg
elif re.match(
'^file://[a-z]:', url,
re.IGNORECASE | re.MULTILINE | re.DOTALL):
url = url.replace('//', '///')
url = url.replace('\\', '/')
return url
def nt_compatible_path(path):
"""Provide compatible NT file paths for ``os.path`` functions
``os.path`` expects NT paths with no ``/`` at the beginning. For
example, ``/C:/Directory/zzz.svg`` would fail ``os.path.isfile()``,
``os.path.isdir()`` etc. where the expected input for `os.path`
functions is ``/C:/Directory/zzz.svg``.
Currently ``nt_compatible_path`` performs some basic checks and
eliminates the unwanted ``/`` at the beginning.
"""
if os.name == 'nt' and re.match(
'^/[a-z]:/', path, re.IGNORECASE | re.MULTILINE | re.DOTALL):
return re.sub('^/', '', path, re.IGNORECASE | re.MULTILINE | re.DOTALL)
else:
return path
def fetch(url, resource_type):
"""Fetch the content of ``url``.
``resource_type`` is the mimetype of the resource (currently one of
image/*, image/svg+xml, text/css).
"""
return urlopen(Request(url, headers=HTTP_HEADERS)).read()
def parse_url(url, base=None):
"""Parse an URL.
The URL can be surrounded by a ``url()`` string. If ``base`` is not `None`,
the "folder" part of it is prepended to the URL.
"""
return urlparse(_parse_url(url, base) or '')
def _parse_url(url, base):
if url:
match = URL.search(url)
if match:
url = match.group(1)
if base:
parsed_base = urlparse(base)
parsed_url = urlparse(url)
if parsed_base.scheme in ('', 'file'):
if parsed_url.scheme in ('', 'file'):
parsed_base_path = nt_compatible_path(parsed_base.path)
parsed_url_path = nt_compatible_path(parsed_url.path)
# We are sure that `url` and `base` are both file-like URLs
if os.path.isfile(parsed_base_path):
if parsed_url_path:
# Take the "folder" part of `base`, as
# `os.path.join` doesn't strip the file name
url = os.path.join(
os.path.dirname(parsed_base_path),
parsed_url_path)
else:
url = parsed_base_path
elif os.path.isdir(parsed_base_path):
if parsed_url_path:
url = os.path.join(
parsed_base_path, parsed_url_path)
else:
url = ''
else:
url = ''
if parsed_url.fragment:
url = '{}#{}'.format(url, parsed_url.fragment)
elif parsed_url.scheme in ('', parsed_base.scheme):
# `urljoin` automatically uses the "folder" part of `base`
url = urljoin(base, url)
url = normalize_url(url.strip('\'"'))
return url
def read_url(url, url_fetcher, resource_type):
"""Get bytes in a parsed ``url`` using ``url_fetcher``.
If ``url_fetcher`` is None a default (no limitations) URLFetcher is used.
"""
parsed_url = urlparse(url) if isinstance(url, str) else url
str_url = url if isinstance(url, str) else url.geturl()
if parsed_url.scheme:
url = parsed_url.geturl()
else:
url = 'file://{}'.format(os.path.abspath(str_url))
url = normalize_url(url)
return url_fetcher(url, resource_type)