Skip to content

Commit 7522a24

Browse files
authored
Make split_frag optional in converting fileuri. (#88)
* Make split_frag optional in converting fileuri.
1 parent 878a74f commit 7522a24

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

schema_salad/ref_resolver.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@
3636
DocumentOrStrType = TypeVar(
3737
'DocumentOrStrType', CommentedSeq, CommentedMap, unicode)
3838

39-
def file_uri(path): # type: (str) -> str
39+
def file_uri(path, split_frag=False): # type: (str, bool) -> str
4040
if path.startswith("file://"):
4141
return path
42-
pathsp = path.split("#", 2)
43-
frag = "#" + urllib.quote(str(pathsp[1])) if len(pathsp) == 2 else ""
44-
urlpath = urllib.pathname2url(str(pathsp[0]))
42+
if split_frag:
43+
pathsp = path.split("#", 2)
44+
frag = "#" + urllib.quote(str(pathsp[1])) if len(pathsp) == 2 else ""
45+
urlpath = urllib.pathname2url(str(pathsp[0]))
46+
else:
47+
urlpath = urllib.pathname2url(path)
48+
frag = ""
4549
if urlpath.startswith("//"):
4650
return "file:%s%s" % (urlpath, frag)
4751
else:

schema_salad/tests/test_examples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ def test_file_uri(self):
367367
# windows box should add an alternate test.
368368
self.assertEquals("file:///foo/bar%20baz/quux", schema_salad.ref_resolver.file_uri("/foo/bar baz/quux"))
369369
self.assertEquals("/foo/bar baz/quux", schema_salad.ref_resolver.uri_file_path("file:///foo/bar%20baz/quux"))
370-
self.assertEquals("file:///foo/bar%20baz/quux#zing%20zong", schema_salad.ref_resolver.file_uri("/foo/bar baz/quux#zing zong"))
370+
self.assertEquals("file:///foo/bar%20baz/quux%23zing%20zong", schema_salad.ref_resolver.file_uri("/foo/bar baz/quux#zing zong"))
371+
self.assertEquals("file:///foo/bar%20baz/quux#zing%20zong", schema_salad.ref_resolver.file_uri("/foo/bar baz/quux#zing zong", split_frag=True))
371372
self.assertEquals("/foo/bar baz/quux#zing zong", schema_salad.ref_resolver.uri_file_path("file:///foo/bar%20baz/quux#zing%20zong"))
372373

373374

0 commit comments

Comments
 (0)