Skip to content

Commit 11b87d4

Browse files
committed
-
1 parent 8c68949 commit 11b87d4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2009-2014 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Defines various tools related to temporary files.'''
5+
6+
import pathlib
7+
import re
8+
9+
10+
numbered_name_pattern = re.compile(
11+
r'''(?P<raw_name>.*) \((?P<number>[0-9]+)\)'''
12+
)
13+
14+
def _get_next_name(path):
15+
assert isinstance(path, pathlib.Path)
16+
parent = path.parent
17+
suffix = path.suffix
18+
suffixless_name = path.name[:-len(suffix)]
19+
assert parent + suffix + suffixless_name == path
20+
match = numbered_name_pattern.match(suffixless_name)
21+
if match:
22+
fixed_suffixless_name = '{} ({})'.format(
23+
match.group('raw_name'),
24+
int(match.group('number'))+1,
25+
)
26+
else:
27+
fixed_suffixless_name = '{} (1)'.format(suffixless_name,)
28+
fix
29+
return pathlib.Path(
30+
'{}{}{}'.format(parent, suffixless_name, suffix)
31+
)
32+
33+
34+
35+
def create_file_renaming_if_taken(path, binary=False):
36+
path = pathlib.Path(path)
37+
38+

0 commit comments

Comments
 (0)