Skip to content

Commit e83ded0

Browse files
committed
-
1 parent b09461c commit e83ded0

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

source_py2/python_toolbox/import_tools.py

Lines changed: 3 additions & 2 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 = str(path.parts[-1][:-len(path.suffix)])
37+
name = str(path.name[:-len(path.suffix)] if path.suffix else path.name)
3838
if name == exclude:
3939
continue
4040
full_name = package.__name__ + '.' + name
@@ -162,7 +162,8 @@ def import_by_path(path, name=None, keep_in_sys_modules=True):
162162
module = _import_by_path_from_zip(path)
163163

164164
else: # '.zip' not in path
165-
short_name = str(path.parts[-1][:-len(path.suffix)])
165+
short_name = str(path.name[:-len(path.suffix)] if path.suffix
166+
else path.name)
166167

167168
if name is None: name = short_name
168169
my_file = None

source_py2/python_toolbox/misc_tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
is_magic_variable_name, get_actual_type, is_number, identity_function,
1010
do_nothing, OwnNameDiscoveringDescriptor, find_clear_place_on_circle,
1111
general_sum, general_product, is_legal_email_address, is_type, NonInstatiable,
12-
repeat_getattr
12+
repeat_getattr, add_extension_if_plain
1313
)
1414
from . import name_mangling
1515
from .proxy_property import ProxyProperty

source_py2/python_toolbox/misc_tools/misc_tools.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,14 @@ def find_clear_place_on_circle(circle_points, circle_size=1):
232232

233233

234234
def add_extension_if_plain(path, extension):
235-
'''Add `extenstion` to a file path if it doesn't have an extenstion.'''
235+
'''Add `extension` to a file path if it doesn't have an extension.'''
236236

237237
path = pathlib.Path(path)
238238

239-
if extension:
239+
if extension and not path.suffix:
240240
assert extension.startswith('.')
241-
if path.suffix:
242-
return pathlib.Path(str(path)[:-len(path.suffix) + extension])
243-
else:
241+
return pathlib.Path(str(path) + extension)
242+
244243
return path
245244

246245

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2009-2014 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Testing module for `find_clear_place_on_circle`.'''
5+
6+
import pathlib
7+
8+
import nose.tools
9+
10+
from python_toolbox import temp_file_tools
11+
12+
from python_toolbox.misc_tools import add_extension_if_plain
13+
14+
15+
def test():
16+
assert str(add_extension_if_plain(r'''c:\hello.zip''', '.ogg')) == \
17+
r'''c:\hello.zip'''
18+
assert str(add_extension_if_plain(r'''c:\hello''', '.ogg')) == \
19+
r'''c:\hello.ogg'''
20+
assert str(add_extension_if_plain(r'''c:\hello''', '.mkv')) == \
21+
r'''c:\hello.mkv'''

0 commit comments

Comments
 (0)