File tree Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Original file line number Diff line number Diff line change 36
36
DocumentOrStrType = TypeVar (
37
37
'DocumentOrStrType' , CommentedSeq , CommentedMap , unicode )
38
38
39
- def file_uri (path ): # type: (unicode ) -> unicode
39
+ def file_uri (path ): # type: (str ) -> str
40
40
if path .startswith ("file://" ):
41
41
return path
42
- urlpath = urllib .pathname2url (str (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 ]))
43
45
if urlpath .startswith ("//" ):
44
- return "file:%s" % urlpath
46
+ return "file:%s%s " % ( urlpath , frag )
45
47
else :
46
- return "file://%s" % urlpath
48
+ return "file://%s%s " % ( urlpath , frag )
47
49
48
- def uri_file_path (url ): # type: (unicode ) -> unicode
50
+ def uri_file_path (url ): # type: (str ) -> str
49
51
split = urlparse .urlsplit (url )
50
52
if split .scheme == "file" :
51
- urllib .url2pathname (str (split .path ))
53
+ return urllib .url2pathname (str (split .path )) + ( "#" + urllib . unquote ( str ( split . fragment )) if split . fragment else "" )
52
54
else :
53
55
raise ValueError ("Not a file URI" )
54
56
Original file line number Diff line number Diff line change @@ -362,6 +362,14 @@ def test_fragment(self):
362
362
b , _ = ldr .resolve_ref (get_data ("tests/frag.yml#foo2" ))
363
363
self .assertEquals ({"id" : b ["id" ], "bar" :"b2" }, b )
364
364
365
+ def test_file_uri (self ):
366
+ # Note: this test probably won't pass on Windows. Someone with a
367
+ # windows box should add an alternate test.
368
+ self .assertEquals ("file:///foo/bar%20baz/quux" , schema_salad .ref_resolver .file_uri ("/foo/bar baz/quux" ))
369
+ 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" ))
371
+ self .assertEquals ("/foo/bar baz/quux#zing zong" , schema_salad .ref_resolver .uri_file_path ("file:///foo/bar%20baz/quux#zing%20zong" ))
372
+
365
373
366
374
if __name__ == '__main__' :
367
375
unittest .main ()
You can’t perform that action at this time.
0 commit comments