|
8 | 8 | import urlparse
|
9 | 9 | import re
|
10 | 10 | import copy
|
11 |
| -import pprint |
| 11 | +import urllib |
12 | 12 | from StringIO import StringIO
|
13 |
| -import pathlib2 |
14 | 13 |
|
15 | 14 | from . import validate
|
16 | 15 | from .aslist import aslist
|
|
37 | 36 | DocumentOrStrType = TypeVar(
|
38 | 37 | 'DocumentOrStrType', CommentedSeq, CommentedMap, unicode)
|
39 | 38 |
|
| 39 | +def file_uri(path): # type: (unicode) -> unicode |
| 40 | + if path.startswith("file://"): |
| 41 | + return path |
| 42 | + urlpath = urllib.pathname2url(str(path)) |
| 43 | + if urlpath.startswith("//"): |
| 44 | + return "file:%s" % urlpath |
| 45 | + else: |
| 46 | + return "file://%s" % urlpath |
| 47 | + |
| 48 | +def uri_file_path(url): # type: (unicode) -> unicode |
| 49 | + split = urlparse.urlsplit(url) |
| 50 | + if split.scheme == "file": |
| 51 | + urllib.url2pathname(str(split.path)) |
| 52 | + else: |
| 53 | + raise ValueError("Not a file URI") |
40 | 54 |
|
41 | 55 | class NormDict(CommentedMap):
|
42 | 56 |
|
@@ -110,7 +124,7 @@ def fetch_text(self, url):
|
110 | 124 | return resp.text
|
111 | 125 | elif scheme == 'file':
|
112 | 126 | try:
|
113 |
| - with open(urllib.url2pathname(str(urlparse.urlparse(url).path))) as fp: |
| 127 | + with open(urllib.url2pathname(str(path))) as fp: |
114 | 128 | read = fp.read()
|
115 | 129 | if hasattr(read, "decode"):
|
116 | 130 | return read.decode("utf-8")
|
@@ -139,7 +153,7 @@ def check_exists(self, url): # type: (unicode) -> bool
|
139 | 153 | return False
|
140 | 154 | return True
|
141 | 155 | elif scheme == 'file':
|
142 |
| - return os.path.exists(urllib.url2pathname(str(urlparse.urlparse(url).path))) |
| 156 | + return os.path.exists(urllib.url2pathname(str(path))) |
143 | 157 | else:
|
144 | 158 | raise ValueError('Unsupported scheme in url: %s' % url)
|
145 | 159 |
|
@@ -380,10 +394,10 @@ def resolve_ref(self,
|
380 | 394 | mixin = None # type: Dict[unicode, Any]
|
381 | 395 |
|
382 | 396 | if not base_url:
|
383 |
| - base_url = pathlib2.Path(os.getcwd()).as_uri() + '/' |
| 397 | + base_url = file_uri(os.getcwd()) + "/" |
384 | 398 |
|
385 | 399 | if isinstance(ref, (str, unicode)) and os.sep == "\\":
|
386 |
| - # Convert Windows paths |
| 400 | + # Convert Windows path separator in ref |
387 | 401 | ref = ref.replace("\\", "/")
|
388 | 402 |
|
389 | 403 | sl = SourceLine(obj, None, ValueError)
|
|
0 commit comments