1818 from pathlib2 import Path
1919
2020 FileNotFoundError = IOError , OSError
21+ NotADirectoryError = IOError , OSError
2122 __metaclass__ = type
2223
2324
@@ -45,14 +46,12 @@ def find_spec(*args, **kwargs):
4546 find_module = find_spec
4647
4748
48- @install
49- class MetadataPathFinder (NullFinder ):
49+ class MetadataPathBaseFinder (NullFinder ):
5050 """A degenerate finder for distribution packages on the file system.
5151
5252 This finder supplies only a find_distributions() method for versions
5353 of Python that do not have a PathFinder find_distributions().
5454 """
55- search_template = r'{pattern}(-.*)?\.(dist|egg)-info'
5655
5756 def find_distributions (self , name = None , path = None ):
5857 """Return an iterable of all Distribution instances capable of
@@ -76,21 +75,34 @@ def _search_paths(cls, pattern, paths):
7675 for path in map (Path , paths )
7776 )
7877
78+ @classmethod
79+ def _predicate (cls , pattern , root , item ):
80+ return re .match (pattern , str (item .name ), flags = re .IGNORECASE )
81+
7982 @classmethod
8083 def _search_path (cls , root , pattern ):
8184 if not root .is_dir ():
8285 return ()
8386 normalized = pattern .replace ('-' , '_' )
87+ matcher = cls .search_template .format (pattern = normalized )
88+ return (item for item in root .iterdir ()
89+ if cls ._predicate (matcher , root , item ))
90+
91+
92+ @install
93+ class MetadataPathFinder (MetadataPathBaseFinder ):
94+ search_template = r'{pattern}(-.*)?\.(dist|egg)-info'
95+
96+
97+ @install
98+ class MetadataPathEggInfoFileFinder (MetadataPathBaseFinder ):
99+ search_template = r'{pattern}(-.*)?\.egg-info'
100+
101+ @classmethod
102+ def _predicate (cls , pattern , root , item ):
84103 return (
85- item
86- for item in root .iterdir ()
87- if item .is_dir ()
88- and re .match (
89- cls .search_template .format (pattern = normalized ),
90- str (item .name ),
91- flags = re .IGNORECASE ,
92- )
93- )
104+ (root / item ).is_file () and
105+ re .match (pattern , str (item .name ), flags = re .IGNORECASE ))
94106
95107
96108class PathDistribution (Distribution ):
@@ -99,7 +111,7 @@ def __init__(self, path):
99111 self ._path = path
100112
101113 def read_text (self , filename ):
102- with suppress (FileNotFoundError ):
114+ with suppress (FileNotFoundError , NotADirectoryError ):
103115 with self ._path .joinpath (filename ).open (encoding = 'utf-8' ) as fp :
104116 return fp .read ()
105117 return None
0 commit comments