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 5252import stat
5353import sys
5454import tempfile
55+ import urllib .parse
5556from collections import defaultdict , deque
5657from operator import methodcaller
5758from typing import (
6566 List ,
6667 Optional ,
6768 Sequence ,
69+ Type ,
6870 TypeVar ,
6971 Union
7072 )
@@ -832,6 +834,25 @@ def iterchildren(
832834 if file .is_dir ():
833835 yield from file .iterchildren (exclude_dirs , match )
834836
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+
835856
836857class PosixPathPlus (PathPlus , pathlib .PurePosixPath ):
837858 """
Original file line number Diff line number Diff line change 1515import sys
1616from tempfile import TemporaryDirectory
1717from textwrap import dedent
18+ from typing import Type
1819
1920# 3rd party
2021import pytest
@@ -880,3 +881,24 @@ def test_sort_paths():
880881 PathPlus ("foo.txt" ),
881882 ]
882883 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