1313import os
1414import types
1515import pkgutil
16+ import pathlib
1617
1718from python_toolbox import dict_tools
1819
@@ -49,9 +50,9 @@ def get_packages_and_modules_filenames(root, recursive=False):
4950
5051 if isinstance (root , types .ModuleType ):
5152 root_module = root
52- root_path = os . path . dirname (root_module . __file__ )
53- elif isinstance (root , str ):
54- root_path = os . path . abspath (root )
53+ root_path = pathlib . Path (root_module ). parent
54+ elif isinstance (root , ( str , pathlib . PurePath ) ):
55+ root_path = pathlib . Path (root ). absolute ( )
5556 # Not making `root_module`, it might not be imported.
5657
5758 ######################################################
@@ -60,7 +61,7 @@ def get_packages_and_modules_filenames(root, recursive=False):
6061
6162 for entry in os .listdir (root_path ):
6263
63- full_path = os . path . join ( root_path , entry )
64+ full_path = root_path / entry
6465
6566 if is_module (full_path ):
6667 result .append (entry )
@@ -73,14 +74,13 @@ def get_packages_and_modules_filenames(root, recursive=False):
7374 full_path ,
7475 recursive = True
7576 )
76- result += [os .path .join (entry , thing ) for thing in
77- inner_results ]
77+ result += [entry / thing for thing in inner_results ]
7878
7979 ### Filtering out duplicate filenames for the same module: ################
8080 # #
8181
8282 filename_to_module_name = {
83- filename : os . path . splitext (filename )[ 0 ] for filename in result
83+ filename : filename [: - len (filename . suffix ) ] for filename in result
8484 }
8585 module_name_to_filenames = \
8686 dict_tools .reverse_with_set_values (filename_to_module_name )
@@ -92,7 +92,7 @@ def get_packages_and_modules_filenames(root, recursive=False):
9292 filenames_by_priority = sorted (
9393 filenames ,
9494 key = lambda filename :
95- _extensions_by_priority .index (os . path . splitext ( filename )[ 1 ] ),
95+ _extensions_by_priority .index (filename . suffix ),
9696 )
9797 redundant_filenames = filenames_by_priority [1 :]
9898 for redundant_filename in redundant_filenames :
@@ -102,18 +102,17 @@ def get_packages_and_modules_filenames(root, recursive=False):
102102 ### Done filtering duplicate filenames for the same module. ###############
103103
104104
105- return [os .path .join (os .path .dirname (full_path ), entry ) for entry in
106- result ]
105+ return [root_path / entry for entry in result ]
107106
108107
109108def is_package (path ):
110109 '''Is the given path a Python package?'''
111- return os . path . isdir (path ) and \
112- glob . glob ( os . path .join ( path , '__init__.*' ))
110+ path = pathlib . Path (path )
111+ return path . is_dir () and list ( path .glob ( '__init__.*' ))
113112
114113
115114def is_module (path ):
116115 '''Is the given path a Python single-file module?'''
117- extension = os . path . splitext (path )[ 1 ]
118- return extension . lower () in ['.py' , '.pyc' , '.pyo' , '.pyw' ]
116+ path = pathlib . Path (path )
117+ return path . suffix . lower () in ['.py' , '.pyc' , '.pyo' , '.pyw' , '.pyd ' ]
119118
0 commit comments