9
9
10
10
# Rules for prefixes:
11
11
# 1. File names are obligated to start with a prefix if there is one.
12
- # 2. Prefixes can't contain any underscores ('_') or digits.
12
+ # 2. Prefixes can't contain any whitespaces, underscores ('_') or digits.
13
13
# 3. Prefixes are obligated to be terminated with an underscore
14
14
15
15
def validate_prefix (prefix ):
16
- return '' .join ((char for char in prefix if char not in '_0123456789' )) # Deleting unwanted characters
16
+ return '' .join ((char for char in prefix if char not in ' _0123456789' )) # Deleting unwanted characters
17
17
18
18
19
19
def find_end_of_prefix (file ):
@@ -48,7 +48,8 @@ def main_file_name(directory, prefix=''):
48
48
def renamer (prefix , base_dir ):
49
49
prefix = validate_prefix (prefix )
50
50
for directory in os .listdir (base_dir ):
51
- new_dir_name = base_dir + '/' + prefix + '_' + directory
51
+ prefix_end = find_end_of_prefix (directory ) + 1
52
+ new_dir_name = base_dir + '/' + prefix + '_' + directory [prefix_end :]
52
53
directory = base_dir + '/' + directory
53
54
if os .path .isdir (directory ):
54
55
for file in os .listdir (directory ):
@@ -59,7 +60,9 @@ def renamer(prefix, base_dir):
59
60
# TODO: probably worth it to find a good way to get rid of this 'if' block
60
61
if file [prefix_end ].isalpha (): # Adding a corresponding number to the files with solutions
61
62
tmp = ''
62
- for char in directory :
63
+ for char in os .path .basename (directory ):
64
+ # TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the
65
+ # loop when we stumble upon an underscore
63
66
if char .isdigit ():
64
67
tmp += char
65
68
if tmp != '' :
@@ -86,7 +89,7 @@ def imp_adjustment(base_dir):
86
89
file_data = f .read ()
87
90
88
91
pos_1 = file_data .find ('from' ) + 5
89
- pos_2 = file_data .find ('import' ) - 1
92
+ pos_2 = file_data .find ('import' , pos_1 ) - 1
90
93
file_data = file_data [:pos_1 ] + main_file_name (directory ) + file_data [pos_2 :]
91
94
92
95
with open (directory + '/' + file , 'w' ) as f :
0 commit comments