Skip to content

Commit fda0f09

Browse files
committed
-
1 parent bd0622b commit fda0f09

File tree

3 files changed

+97
-30
lines changed

3 files changed

+97
-30
lines changed

source_py2/python_toolbox/os_tools.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
import sys
88
import os.path
9+
import pathlib
910

1011

1112
def start_file(path):
@@ -24,4 +25,9 @@ def start_file(path):
2425
else:
2526
raise NotImplementedError(
2627
"Your operating system `%s` isn't supported by "
27-
"`start_file`." % sys.platform)
28+
"`start_file`." % sys.platform)
29+
30+
_is_windows = (os.name == 'nt')
31+
null_path = pathlib.Path('\\Device\\Null') if _is_windows \
32+
else pathlib.Path('/dev/null')
33+
path_type = pathlib.WindowsPath if _is_windows else pathlib.PosixPath

source_py2/python_toolbox/temp_file_tools.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,80 @@
33

44
'''Defines various tools related to temporary files.'''
55

6-
76
import tempfile
87
import shutil
98
import os
9+
import pathlib
1010

11+
from python_toolbox import address_tools
12+
from python_toolbox import os_tools
1113
from python_toolbox.context_management import ContextManager
1214

1315

14-
15-
class TemporaryFolder(ContextManager):
16+
class TemporaryFolder(ContextManager, os_tools.path_type):
1617
'''
1718
Context manager that creates a temporary folder and deletes it after usage.
1819
1920
After the suite finishes, the temporary folder and all its files and
2021
subfolders will be deleted.
2122
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+
2226
Example:
2327
24-
with TemporaryFolder() as temporary_folder_path:
28+
with TemporaryFolder() as temporary_folder:
2529
2630
# We have a temporary folder!
27-
assert os.path.isdir(temporary_folder_path)
31+
assert temporary_folder.is_dir()
2832
2933
# We can create files in it:
30-
open(os.path.join(temporary_folder_path, 'my_file'), 'w')
34+
with (temporary_folder / 'my_file').open('w') as my_file:
35+
my_file.write('whatever')
3136
3237
# The suite is finished, now it's all cleaned:
33-
assert not os.path.exists(temporary_folder_path)
38+
assert not temporary_folder.exists()
3439
3540
Use the `suffix` and `prefix` string arguments to dictate a suffix and/or a
3641
prefix to the temporary folder's name in the filesystem.
3742
'''
3843

39-
def __init__(self, suffix='', prefix=tempfile.template):
40-
self.suffix = suffix
41-
self.prefix = prefix
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
4249
self.path = None
4350
self._closed = False
51+
self.__set_path(str(os_tools.null_path))
52+
self._init()
4453

4554

4655
def __enter__(self):
4756
assert not self._closed
48-
self.path = tempfile.mkdtemp(suffix=self.suffix, prefix=self.prefix)
49-
assert os.path.isdir(self.path)
50-
return self.path
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
5165

5266

5367
def __exit__(self, exc_type, exc_value, exc_traceback):
5468
assert not self._closed
55-
shutil.rmtree(self.path)
69+
shutil.rmtree(str(self))
5670
self._closed = True
5771

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)
5876

59-
def __str__(self):
60-
return self.path or ''
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+
)

source_py2/test_python_toolbox/test_temp_file_tools/test_temporary_folder.py

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import tempfile
77
import os.path
8+
import pathlib
89

910
import nose.tools
1011

@@ -14,25 +15,56 @@
1415

1516

1617
def test_basic():
17-
'''Test the basic working of `TemporaryFolder`.'''
18-
with TemporaryFolder() as tf1path:
19-
assert isinstance(tf1path, str)
20-
assert os.path.exists(tf1path)
21-
assert os.path.isdir(tf1path)
18+
with TemporaryFolder() as tf1:
19+
assert isinstance(tf1, pathlib.Path)
20+
assert tf1.exists()
21+
assert tf1.is_dir()
2222

2323
tf2 = TemporaryFolder()
24-
with tf2 as tf2path:
25-
assert str(tf2) == tf2.path == tf2path
24+
with tf2 as tf2:
25+
assert isinstance(tf2, pathlib.Path)
26+
assert tf2.exists()
27+
assert tf2.is_dir()
28+
29+
assert not tf2.exists()
30+
assert not tf2.is_dir()
31+
32+
assert tf1.exists()
33+
assert tf1.is_dir()
34+
file_path = (tf1 / 'my_file')
35+
with file_path.open('w') as my_file:
36+
my_file.write(u'Woo hoo!')
37+
38+
assert file_path.exists()
39+
assert file_path.is_file()
40+
41+
with file_path.open('r') as my_file:
42+
assert my_file.read() == 'Woo hoo!'
43+
44+
assert not tf1.exists()
45+
assert not tf1.is_dir()
46+
47+
assert not file_path.exists()
48+
assert not file_path.is_file()
49+
50+
51+
def test_without_pathlib():
52+
with TemporaryFolder() as tf1:
53+
assert os.path.exists(str(tf1))
54+
assert os.path.isdir(str(tf1))
55+
56+
tf2 = TemporaryFolder()
57+
with tf2 as tf2:
2658
assert os.path.exists(str(tf2))
2759
assert os.path.isdir(str(tf2))
2860

2961
assert not os.path.exists(str(tf2))
30-
assert not os.path.isdir(tf2path)
62+
assert not os.path.isdir(str(tf2))
3163

32-
assert os.path.exists(tf1path)
33-
assert os.path.isdir(tf1path)
64+
assert os.path.exists(str(tf1))
65+
assert os.path.isdir(str(tf1))
3466

35-
file_path = os.path.join(tf1path, 'my_file')
67+
file_path = os.path.join(str(tf1), 'my_file')
3668
with open(file_path, 'w') as my_file:
3769
my_file.write('Woo hoo!')
3870

@@ -42,10 +74,17 @@ def test_basic():
4274
with open(file_path, 'r') as my_file:
4375
assert my_file.read() == 'Woo hoo!'
4476

45-
assert not os.path.exists(tf1path)
46-
assert not os.path.isdir(tf1path)
77+
assert not os.path.exists(str(tf1))
78+
assert not os.path.isdir(str(tf1))
4779

4880
assert not os.path.exists(file_path)
4981
assert not os.path.isdir(file_path)
5082

5183

84+
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+

0 commit comments

Comments
 (0)