Skip to content

Commit 81c108e

Browse files
committed
-
1 parent b9d57b3 commit 81c108e

File tree

5 files changed

+27
-76
lines changed

5 files changed

+27
-76
lines changed

source_py2/python_toolbox/temp_file_tools.py

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,35 @@
55

66
import tempfile
77
import shutil
8-
import os
98
import pathlib
109

11-
from python_toolbox import address_tools
12-
from python_toolbox import os_tools
13-
from python_toolbox.context_management import ContextManager
10+
from python_toolbox import context_management
1411

1512

16-
class TemporaryFolder(ContextManager, os_tools.path_type):
13+
@context_management.ContextManagerType
14+
def create_temp_folder(suffix='', prefix=tempfile.template):
1715
'''
1816
Context manager that creates a temporary folder and deletes it after usage.
1917
2018
After the suite finishes, the temporary folder and all its files and
2119
subfolders will be deleted.
2220
23-
The `TemporaryFolder` object is also a `pathlib.Path` object, so all
24-
operations that can be done on paths, can be done on it seamlessly.
25-
2621
Example:
2722
28-
with TemporaryFolder() as temporary_folder:
23+
with create_temp_folder() as temp_folder:
2924
3025
# We have a temporary folder!
31-
assert temporary_folder.is_dir()
26+
assert temp_folder.is_dir()
3227
3328
# We can create files in it:
34-
with (temporary_folder / 'my_file').open('w') as my_file:
35-
my_file.write('whatever')
29+
(temp_folder / 'my_file').open('w')
3630
3731
# The suite is finished, now it's all cleaned:
38-
assert not temporary_folder.exists()
32+
assert not temp_folder.exists()
3933
4034
Use the `suffix` and `prefix` string arguments to dictate a suffix and/or a
4135
prefix to the temporary folder's name in the filesystem.
4236
'''
43-
44-
_was_entered = False
45-
46-
def __init__(self, prefix=tempfile.template, suffix=''):
47-
self.temporary_folder_prefix = prefix
48-
self.temporary_folder_suffix = suffix
49-
self.path = None
50-
self._closed = False
51-
self.__set_path(str(os_tools.null_path))
52-
self._init()
53-
54-
55-
def __enter__(self):
56-
assert not self._closed
57-
self.__set_path(
58-
tempfile.mkdtemp(prefix=self.temporary_folder_prefix,
59-
suffix=self.temporary_folder_suffix,)
60-
)
61-
62-
assert self.is_dir()
63-
self._was_entered = True
64-
return self
65-
66-
67-
def __exit__(self, exc_type, exc_value, exc_traceback):
68-
assert not self._closed
69-
shutil.rmtree(str(self))
70-
self._closed = True
71-
72-
def __set_path(self, path):
73-
self._drv, self._root, self._parts = self._parse_args((path,))
74-
self._str = \
75-
self._format_parsed_parts(self._drv, self._root, self._parts)
76-
77-
78-
def __repr__(self):
79-
return '<%s: %s>' % (
80-
address_tools.describe(type(self), shorten=True),
81-
str(self) if self._was_entered else '(Not created yet)',
82-
)
37+
temp_folder = pathlib.Path(tempfile.mkdtemp(suffix=suffix, prefix=prefix))
38+
yield temp_folder
39+
shutil.rmtree(str(temp_folder))

source_py2/test_python_toolbox/test_cute_profile/test_cute_profile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ def test_polite_wrapper():
227227
def test_folder_handler():
228228
with temp_value_setting.TempValueSetter((cute_profile.profile_handling,
229229
'threading'), dummy_threading):
230-
with temp_file_tools.TemporaryFolder(suffix='_python_toolbox_testing')\
231-
as temp_folder:
230+
with temp_file_tools.create_temp_folder(
231+
suffix='_python_toolbox_testing') as temp_folder:
232232
f = cute_profile.profile_ready(profile_handler=temp_folder)(func)
233233

234234
f(1, 2)

source_py2/test_python_toolbox/test_import_tools/test_exists/test_zip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def test_zip():
3131
zip_string = pkg_resources.resource_string(resources_package,
3232
'archive_with_module.zip')
3333

34-
with temp_file_tools.TemporaryFolder(prefix='test_python_toolbox_') \
35-
as temp_folder:
34+
with temp_file_tools.create_temp_folder(
35+
prefix='test_python_toolbox_') as temp_folder:
3636

3737
temp_zip_path = temp_folder / 'archive_with_module.zip'
3838

source_py2/test_python_toolbox/test_temp_file_tools/test_temporary_folder.py renamed to source_py2/test_python_toolbox/test_temp_file_tools/test_create_temp_folder.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2009-2014 Ram Rachum.
22
# This program is distributed under the MIT license.
33

4-
'''Testing module for `temp_file_tools.TemporaryFolder`.'''
4+
'''Testing module for `temp_file_tools.create_temp_folder`.'''
55

66
import tempfile
77
import os.path
@@ -11,16 +11,16 @@
1111

1212
import python_toolbox
1313

14-
from python_toolbox.temp_file_tools import TemporaryFolder
14+
from python_toolbox.temp_file_tools import create_temp_folder
1515

1616

1717
def test_basic():
18-
with TemporaryFolder() as tf1:
18+
with create_temp_folder() as tf1:
1919
assert isinstance(tf1, pathlib.Path)
2020
assert tf1.exists()
2121
assert tf1.is_dir()
2222

23-
tf2 = TemporaryFolder()
23+
tf2 = create_temp_folder()
2424
with tf2 as tf2:
2525
assert isinstance(tf2, pathlib.Path)
2626
assert tf2.exists()
@@ -33,7 +33,7 @@ def test_basic():
3333
assert tf1.is_dir()
3434
file_path = (tf1 / 'my_file')
3535
with file_path.open('w') as my_file:
36-
my_file.write(u'Woo hoo!')
36+
my_file.write('Woo hoo!')
3737

3838
assert file_path.exists()
3939
assert file_path.is_file()
@@ -49,11 +49,11 @@ def test_basic():
4949

5050

5151
def test_without_pathlib():
52-
with TemporaryFolder() as tf1:
52+
with create_temp_folder() as tf1:
5353
assert os.path.exists(str(tf1))
5454
assert os.path.isdir(str(tf1))
5555

56-
tf2 = TemporaryFolder()
56+
tf2 = create_temp_folder()
5757
with tf2 as tf2:
5858
assert os.path.exists(str(tf2))
5959
assert os.path.isdir(str(tf2))
@@ -82,9 +82,3 @@ def test_without_pathlib():
8282

8383

8484

85-
def test_repr():
86-
tf = TemporaryFolder()
87-
assert '(Not created yet)' in repr(tf)
88-
with tf:
89-
assert '(Not created yet)' not in repr(tf)
90-

source_py2/test_python_toolbox/test_temp_value_setting/test_temp_working_directory_setter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class MyException(Exception):
1919

2020
def test():
2121
'''Test basic workings of `TempWorkingDirectorySetter`.'''
22-
with temp_file_tools.TemporaryFolder(prefix='test_python_toolbox_') \
23-
as temp_folder:
22+
with temp_file_tools.create_temp_folder(
23+
prefix='test_python_toolbox_') as temp_folder:
2424
old_cwd = os.getcwd()
2525
with TempWorkingDirectorySetter(temp_folder):
2626

@@ -44,8 +44,8 @@ def test_exception():
4444
'''Test `TempWorkingDirectorySetter` recovering from exception in suite.'''
4545
# Not using `assert_raises` here because getting the `with` suite in there
4646
# would be tricky.
47-
with temp_file_tools.TemporaryFolder(prefix='test_python_toolbox_') \
48-
as temp_folder:
47+
with temp_file_tools.create_temp_folder(
48+
prefix='test_python_toolbox_') as temp_folder:
4949
old_cwd = os.getcwd()
5050
try:
5151
with TempWorkingDirectorySetter(temp_folder):
@@ -77,8 +77,8 @@ def test_exception():
7777

7878
def test_as_decorator():
7979
'''Test `TempWorkingDirectorySetter` used as a decorator.'''
80-
with temp_file_tools.TemporaryFolder(prefix='test_python_toolbox_') \
81-
as temp_folder:
80+
with temp_file_tools.create_temp_folder(
81+
prefix='test_python_toolbox_') as temp_folder:
8282
old_cwd = os.getcwd()
8383
@TempWorkingDirectorySetter(temp_folder)
8484
def f():

0 commit comments

Comments
 (0)