Skip to content

Commit 14f6a82

Browse files
committed
-
1 parent ae3ea65 commit 14f6a82

File tree

6 files changed

+39
-41
lines changed

6 files changed

+39
-41
lines changed

source_py3/python_toolbox/import_tools.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def import_all(package, exclude='__init__', silent_fail=False):
3535

3636
names = {}
3737
for path in paths:
38-
name = os.path.splitext(os.path.split(path)[1])[0]
38+
name = str(path.parts[-1][:-len(path.suffix)])
3939
if name == exclude:
4040
continue
4141
full_name = package.__name__ + '.' + name
@@ -157,19 +157,20 @@ def import_by_path(path, name=None, keep_in_sys_modules=True):
157157
"hammer", will be used, which might cause problems in some cases. (Like
158158
when using multiprocessing.)
159159
'''
160+
path = pathlib.Path(path)
160161
if '.zip' in path:
161162
if name is not None:
162163
raise NotImplementedError
163164
module = _import_by_path_from_zip(path)
164165

165166
else: # '.zip' not in path
166-
short_name = os.path.splitext(os.path.split(path)[1])[0]
167+
short_name = str(path.parts[-1][:-len(path.suffix)])
168+
167169
if name is None: name = short_name
168-
path_to_dir = os.path.dirname(path)
169170
my_file = None
170171
try:
171172
(my_file, pathname, description) = \
172-
imp.find_module(short_name, [path_to_dir])
173+
imp.find_module(short_name, [path.parent])
173174
module = imp.load_module(name, my_file, pathname, description)
174175
finally:
175176
if my_file is not None:

source_py3/python_toolbox/misc_tools/misc_tools.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'''This module defines miscellaneous tools.'''
55

66
import operator
7-
import os.path
7+
import pathlib
88
import re
99
import math
1010
import types
@@ -225,14 +225,14 @@ def find_clear_place_on_circle(circle_points, circle_size=1):
225225
def add_extension_if_plain(path, extension):
226226
'''Add `extenstion` to a file path if it doesn't have an extenstion.'''
227227

228+
path = pathlib.Path(path)
229+
228230
if extension:
229231
assert extension.startswith('.')
230-
231-
(without_extension, existing_extension) = os.path.splitext(path)
232-
if existing_extension:
232+
if path.suffix:
233+
return pathlib.Path(str(path)[:-len(path.suffix) + extension])
234+
else:
233235
return path
234-
else: # not existing_extension
235-
return without_extension + extension
236236

237237

238238
def general_sum(things, start=None):

source_py3/python_toolbox/os_tools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
def start_file(path):
1313
'''Open a file by launching the program that handles its kind.'''
14-
assert os.path.exists(path)
14+
path = pathlib.Path(path)
15+
assert path.exists()
1516

1617
if sys.platform.startswith('linux'): # Linux:
17-
subprocess.check_call(['xdg-open', path])
18+
subprocess.check_call(['xdg-open', str(path)])
1819

1920
elif sys.platform == 'darwin': # Mac:
20-
subprocess.check_call(['open', '--', path])
21+
subprocess.check_call(['open', '--', str(path)])
2122

2223
elif sys.platform in ('win32', 'cygwin'): # Windows:
2324
os.startfile(path)

source_py3/python_toolbox/package_finder.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414
import types
1515
import pkgutil
16+
import pathlib
1617

1718
from 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

109108
def 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

115114
def 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

source_py3/python_toolbox/path_tools.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55

66
import sys
77
import os.path
8+
import pathlib
89
import glob
910
import types
1011

1112

1213
def list_sub_folders(path):
1314
'''List all the immediate sub-folders of the folder at `path`.'''
14-
assert os.path.isdir(path)
15-
files_and_folders = glob.glob(os.path.join(path, '*'))
16-
folders = list(filter(os.path.isdir, files_and_folders))
17-
return folders
15+
path = pathlib.Path(path)
16+
assert path.is_dir()
17+
return list(filter(pathlib.Path.is_dir, path.glob('*')))
1818

1919

2020
def get_path_of_package(package):
2121
'''Get the path of a Python package, i.e. where its modules would be.'''
22-
path = package.__file__
23-
dir_path, file_name = os.path.split(path)
24-
assert '__init__' in file_name
25-
return dir_path
22+
path = pathlib.Path(package.__file__)
23+
assert '__init__' in path.name
24+
return path.parent
2625

2726

2827
def get_root_path_of_module(module):
@@ -38,14 +37,13 @@ def get_root_path_of_module(module):
3837
module_name = module.__name__
3938
root_module_name = module_name.split('.', 1)[0]
4039
root_module = sys.modules[root_module_name]
41-
path_of_root_module = root_module.__file__
42-
dir_path, file_name = os.path.split(path_of_root_module)
43-
if '__init__' in file_name:
40+
path_of_root_module = pathlib.Path(root_module.__file__)
41+
if '__init__' in path_of_root_module.name:
4442
# It's a package.
45-
result = os.path.abspath(os.path.join(dir_path, '..'))
43+
result = path_of_root_module.parent.parent.absolute()
4644
else:
4745
# It's a one-file module, not a package.
48-
result = os.path.abspath(dir_path)
46+
result = path_of_root_module.parent.absolute()
4947

5048
assert result in list(map(os.path.abspath, sys.path))
5149
return result

source_py3/test_python_toolbox/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
'''Testing package for `python_toolbox`.'''
55

66
import sys
7-
import os.path
87
import pathlib
98

109
import nose

0 commit comments

Comments
 (0)