Skip to content

Commit 40fe588

Browse files
committed
-
1 parent fadd677 commit 40fe588

File tree

9 files changed

+58
-58
lines changed

9 files changed

+58
-58
lines changed

source_py2/python_toolbox/cute_profile/profile_handling.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import marshal
77
import pathlib
88
import abc
9-
import os.path
109
import pstats
1110

1211
from python_toolbox.third_party import envelopes
@@ -118,5 +117,5 @@ def get_profile_handler(profile_handler_string):
118117
[0]):
119118
return EmailProfileHandler(*profile_handler_string.split('\n'))
120119
else:
121-
assert os.path.isdir(profile_handler_string)
120+
assert pathlib.Path(profile_handler_string).is_dir()
122121
return FolderProfileHandler(profile_handler_string)

source_py2/python_toolbox/import_tools.py

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

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

163164
else: # '.zip' not in path
164-
short_name = os.path.splitext(os.path.split(path)[1])[0]
165+
short_name = str(path.parts[-1][:-len(path.suffix)])
166+
165167
if name is None: name = short_name
166-
path_to_dir = os.path.dirname(path)
167168
my_file = None
168169
try:
169170
(my_file, pathname, description) = \
170-
imp.find_module(short_name, [path_to_dir])
171+
imp.find_module(short_name, [path.parent])
171172
module = imp.load_module(name, my_file, pathname, description)
172173
finally:
173174
if my_file is not None:

source_py2/python_toolbox/misc_tools/misc_tools.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from __future__ import division
77

88
import operator
9-
import os.path
9+
import pathlib
1010
import re
1111
import math
1212
import types
@@ -234,14 +234,14 @@ def find_clear_place_on_circle(circle_points, circle_size=1):
234234
def add_extension_if_plain(path, extension):
235235
'''Add `extenstion` to a file path if it doesn't have an extenstion.'''
236236

237+
path = pathlib.Path(path)
238+
237239
if extension:
238240
assert extension.startswith('.')
239-
240-
(without_extension, existing_extension) = os.path.splitext(path)
241-
if existing_extension:
241+
if path.suffix:
242+
return pathlib.Path(str(path)[:-len(path.suffix) + extension])
243+
else:
242244
return path
243-
else: # not existing_extension
244-
return without_extension + extension
245245

246246

247247
def general_sum(things, start=None):

source_py2/python_toolbox/os_tools.py

Lines changed: 5 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)
@@ -27,6 +28,7 @@ def start_file(path):
2728
"Your operating system `%s` isn't supported by "
2829
"`start_file`." % sys.platform)
2930

31+
3032
_is_windows = (os.name == 'nt')
3133
null_path = pathlib.Path(os.path.devnull)
3234
path_type = pathlib.WindowsPath if _is_windows else pathlib.PosixPath

source_py2/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_py2/python_toolbox/path_tools.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
'''Defines various tools related to file-system paths.'''
55

66
import sys
7-
import os.path
7+
import pathlib
88
import glob
99
import types
1010

1111

1212
def list_sub_folders(path):
1313
'''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 = filter(os.path.isdir, files_and_folders)
17-
return folders
14+
path = pathlib.Path(path)
15+
assert path.is_dir()
16+
return list(filter(pathlib.Path.is_dir, path.glob('*')))
1817

1918

2019
def get_path_of_package(package):
2120
'''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
21+
path = pathlib.Path(package.__file__)
22+
assert '__init__' in path.name
23+
return path.parent
2624

2725

2826
def get_root_path_of_module(module):
@@ -38,15 +36,15 @@ def get_root_path_of_module(module):
3836
module_name = module.__name__
3937
root_module_name = module_name.split('.', 1)[0]
4038
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:
39+
path_of_root_module = pathlib.Path(root_module.__file__)
40+
if '__init__' in path_of_root_module.name:
4441
# It's a package.
45-
result = os.path.abspath(os.path.join(dir_path, '..'))
42+
result = path_of_root_module.parent.parent.absolute()
4643
else:
4744
# It's a one-file module, not a package.
48-
result = os.path.abspath(dir_path)
45+
result = path_of_root_module.parent.absolute()
4946

50-
assert result in map(os.path.abspath, sys.path)
47+
assert result in list(map(pathlib.Path.absolute,
48+
map(pathlib.Path, sys.path)))
5149
return result
5250

source_py2/python_toolbox/zip_tools.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
import contextlib
88
import zipfile as zip_module
9-
import os.path
109
import cStringIO as string_io_module
1110
import re
11+
import pathlib
1212
import fnmatch
1313

1414

15-
def zip_folder(folder, zip_path, ignored_patterns=()):
15+
def zip_folder(source_folder, zip_path, ignored_patterns=()):
1616
'''
1717
Zip `folder` into a zip file specified by `zip_path`.
1818
@@ -25,32 +25,35 @@ def zip_folder(folder, zip_path, ignored_patterns=()):
2525
2626
Any empty sub-folders will be ignored.
2727
'''
28-
assert os.path.isdir(folder)
29-
source_folder = os.path.realpath(folder)
28+
zip_path = pathlib.Path(zip_path)
29+
source_folder = pathlib.Path(source_folder).absolute()
30+
assert source_folder.is_dir()
3031

3132
ignored_re_patterns = [re.compile(fnmatch.translate(ignored_pattern)) for
3233
ignored_pattern in ignored_patterns]
3334

34-
zip_name = os.path.splitext(os.path.split(zip_path)[1])[0]
35-
source_folder_name = os.path.split(source_folder)[1]
35+
zip_name = zip_path.name[:-len(zip_path.suffix)]
36+
37+
internal_pure_path = pathlib.PurePath(source_folder.name)
3638

3739
with zip_module.ZipFile(zip_path, 'w', zip_module.ZIP_DEFLATED) \
3840
as zip_file:
3941

4042
for root, subfolders, files in os.walk(source_folder):
43+
root = pathlib.Path(root)
44+
subfolders = map(pathlib.Path, subfolders)
45+
files = map(pathlib.Path, files)
4146

4247
for file_path in files:
4348

44-
if any(ignored_re_pattern.match(os.path.join(root, file_path))
49+
if any(ignored_re_pattern.match(root / file_path)
4550
for ignored_re_pattern in ignored_re_patterns):
4651
continue
4752

48-
absolute_file_path = os.path.join(root, file_path)
53+
absolute_file_path = root / file_path
4954

50-
destination_file_path = os.path.join(
51-
source_folder_name,
52-
absolute_file_path[(len(source_folder) + len(os.sep)):]
53-
)
55+
destination_file_path = internal_pure_path / \
56+
absolute_file_path.name
5457

5558
zip_file.write(absolute_file_path, destination_file_path)
5659

source_py2/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

source_py2/test_python_toolbox/test_cute_profile/test_cute_profile.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
'''Testing module for `python_toolbox.cute_profile`.'''
55

6-
import os.path
76
import dummy_threading
87
import time
98

0 commit comments

Comments
 (0)