File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 52
52
import stat
53
53
import sys
54
54
import tempfile
55
+ import urllib .parse
55
56
from collections import defaultdict , deque
56
57
from operator import methodcaller
57
58
from typing import (
65
66
List ,
66
67
Optional ,
67
68
Sequence ,
69
+ Type ,
68
70
TypeVar ,
69
71
Union
70
72
)
@@ -832,6 +834,25 @@ def iterchildren(
832
834
if file .is_dir ():
833
835
yield from file .iterchildren (exclude_dirs , match )
834
836
837
+ @classmethod
838
+ def from_uri (cls : Type [_PP ], uri : str ) -> _PP :
839
+ """
840
+ Construct a :class:`~.PathPlus` from a ``file`` URI returned by :meth:`pathlib.PurePath.as_uri`.
841
+
842
+ .. versionadded:: 2.9.0
843
+
844
+ :param uri:
845
+ """
846
+
847
+ parseresult = urllib .parse .urlparse (uri )
848
+
849
+ if parseresult .scheme != "file" :
850
+ raise ValueError (f"Unsupported URI scheme { parseresult .scheme !r} " )
851
+ if parseresult .netloc or parseresult .params or parseresult .query or parseresult .fragment :
852
+ raise ValueError ("Malformed file URI" )
853
+
854
+ return cls (urllib .parse .unquote_to_bytes (parseresult .path ).decode ("UTF-8" ))
855
+
835
856
836
857
class PosixPathPlus (PathPlus , pathlib .PurePosixPath ):
837
858
"""
Original file line number Diff line number Diff line change 15
15
import sys
16
16
from tempfile import TemporaryDirectory
17
17
from textwrap import dedent
18
+ from typing import Type
18
19
19
20
# 3rd party
20
21
import pytest
@@ -880,3 +881,24 @@ def test_sort_paths():
880
881
PathPlus ("foo.txt" ),
881
882
]
882
883
assert sort_paths (* paths ) == expected
884
+
885
+
886
+ if platform .system () == "Windows" :
887
+ _from_uri_paths = [
888
+ "c:/" ,
889
+ "c:/users/domdf/☃.txt" ,
890
+ "c:/a/b.c" ,
891
+ "c:/a/b%#c" ,
892
+ "c:/a/bé" ,
893
+ "//some/share/" ,
894
+ "//some/share/a/b.c" ,
895
+ "//some/share/a/b%#cé"
896
+ ]
897
+ else :
898
+ _from_uri_paths = ['/' , "/home/domdf/☃.txt" , "/a/b.c" , "/a/b%#c" ]
899
+
900
+
901
+ @pytest .mark .parametrize ("path" , _from_uri_paths )
902
+ @pytest .mark .parametrize ("left_type" , [pathlib .PurePath , pathlib .Path , PathPlus ])
903
+ def test_pathplus_from_uri (path : str , left_type : Type ):
904
+ assert PathPlus .from_uri (left_type (path ).as_uri ()).as_posix () == path
You can’t perform that action at this time.
0 commit comments