Skip to content

Commit 74fc6b3

Browse files
committed
-
1 parent b5dbae1 commit 74fc6b3

File tree

3 files changed

+71
-19
lines changed

3 files changed

+71
-19
lines changed

source_py3/python_toolbox/file_tools.py

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

66
import pathlib
77
import re
8-
import contextlib
98

109
from python_toolbox import context_management
1110

@@ -49,23 +48,18 @@ def create_file_renaming_if_taken(path, mode='x',
4948
errors=None, newline=None):
5049
assert 'x' in mode
5150
current_path = pathlib.Path(path)
52-
with contextlib.ExitStack() as exit_stack:
53-
for i in range(N_MAX_ATTEMPTS):
54-
try:
55-
file = exit_stack.enter_context(
56-
current_path.open(mode, buffering=buffering,
57-
encoding=encoding, errors=errors,
58-
newline=newline)
59-
)
60-
except FileExistsError:
61-
current_path = _get_next_name(current_path)
62-
else:
63-
return FileContainer(file)
64-
else:
65-
raise Exception("Exceeded {} tries, can't create file {}".format(
66-
N_MAX_ATTEMPTS,
67-
path
68-
))
51+
for i in range(N_MAX_ATTEMPTS):
52+
try:
53+
return current_path.open(mode, buffering=buffering,
54+
encoding=encoding, errors=errors,
55+
newline=newline)
56+
except FileExistsError:
57+
current_path = _get_next_name(current_path)
58+
else:
59+
raise Exception("Exceeded {} tries, can't create file {}".format(
60+
N_MAX_ATTEMPTS,
61+
path
62+
))
6963

7064

7165
def write_to_file_renaming_if_taken(path, data, mode='x',
@@ -74,5 +68,6 @@ def write_to_file_renaming_if_taken(path, data, mode='x',
7468
with create_file_renaming_if_taken(
7569
path, mode=mode, buffering=buffering, encoding=encoding, errors=errors,
7670
newline=newline) as file:
77-
file.write(data)
71+
72+
return file.write(data)
7873

source_py3/test_python_toolbox/test_file_tools/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2009-2014 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
import pathlib
5+
6+
import python_toolbox
7+
from python_toolbox import temp_file_tools
8+
9+
from python_toolbox import file_tools
10+
11+
12+
def test():
13+
with temp_file_tools.TemporaryFolder(prefix='test_python_toolbox_') as \
14+
temporary_folder:
15+
get_file_names_set = \
16+
lambda: set(path.name for path in temporary_folder.glob('*'))
17+
assert not get_file_names_set()
18+
19+
file_path = temporary_folder / 'meow.txt'
20+
string_to_write = "I'm a cat, hear me meow!"
21+
22+
assert file_tools.write_to_file_renaming_if_taken(
23+
file_path, string_to_write) == len(string_to_write)
24+
25+
with file_path.open('r') as file:
26+
assert file.read() == string_to_write
27+
28+
assert get_file_names_set() == {'meow.txt'}
29+
30+
31+
assert file_tools.write_to_file_renaming_if_taken(
32+
file_path, string_to_write) == len(string_to_write)
33+
assert file_tools.write_to_file_renaming_if_taken(
34+
file_path, string_to_write) == len(string_to_write)
35+
assert file_tools.write_to_file_renaming_if_taken(
36+
file_path, string_to_write) == len(string_to_write)
37+
38+
with (temporary_folder / 'meow (2).txt').open('r') as last_file_input:
39+
assert last_file_input.read() == string_to_write
40+
41+
assert get_file_names_set() == {'meow.txt', 'meow (1).txt',
42+
'meow (2).txt', 'meow (3).txt'}
43+
44+
with file_tools.create_file_renaming_if_taken(file_path) as last_file:
45+
assert not last_file.closed
46+
last_file.write(string_to_write[:5])
47+
last_file.write(string_to_write[5:])
48+
49+
assert last_file.closed
50+
51+
assert get_file_names_set() == {'meow.txt', 'meow (1).txt',
52+
'meow (2).txt', 'meow (3).txt',
53+
'meow (4).txt'}
54+
55+
with pathlib.Path(last_file.name).open('r') as last_file_input:
56+
assert last_file_input.read() == string_to_write
57+

0 commit comments

Comments
 (0)