File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
source_py3/python_toolbox Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments