|
3 | 3 | import re |
4 | 4 | import sys |
5 | 5 | from datetime import datetime |
| 6 | +from pathlib import Path |
6 | 7 |
|
7 | 8 | parent_dir = os.path.basename(os.getcwd()) |
8 | 9 |
|
|
15 | 16 | sys.exit(1) |
16 | 17 |
|
17 | 18 | for root, _, files in os.walk('.'): |
18 | | - for file in files: |
19 | | - if not (file.endswith('.java') or file.endswith('.cpp')): |
| 19 | + for cur_filename in files: |
| 20 | + if not (cur_filename.endswith('.java') or cur_filename.endswith('.cpp')): |
| 21 | + continue |
| 22 | + if cur_filename != 'Main.java' and cur_filename != 'main.cpp': |
20 | 23 | continue |
21 | 24 |
|
22 | | - today = datetime.now().strftime('%m/%d/%Y') |
23 | | - current_file_path = os.path.join(root, file) |
24 | | - |
25 | | - with open(current_file_path, 'r', encoding='utf-8') as f: |
| 25 | + cur_filepath = os.path.join(root, cur_filename) |
| 26 | + with open(cur_filepath, 'r', encoding='utf-8') as f: |
26 | 27 | content = f.read() |
27 | 28 |
|
28 | | - original_content = content |
29 | | - |
30 | | - base_name, extension = os.path.splitext(file) |
31 | | - title_suffix = parent_dir |
32 | | - title_suffix = title_suffix.replace('_in_cpp', '') |
33 | | - title_suffix = title_suffix.replace('_in_java', '') |
| 29 | + base_name = ( |
| 30 | + os.path.basename(os.path.dirname(cur_filepath)) |
| 31 | + .replace('_in_cpp', '') |
| 32 | + .replace('_in_java', '') |
| 33 | + ) |
| 34 | + new_filename = ( |
| 35 | + f'{Path(cur_filepath).stem}_{base_name}{Path(cur_filepath).suffix}' |
| 36 | + ) |
| 37 | + new_filepath = os.path.join(root, new_filename) |
34 | 38 |
|
35 | | - new_file_title = f'{base_name}_{title_suffix}{extension}' |
36 | | - |
37 | | - content = re.sub(r'(Title:\s*).*', rf'\g<1>{new_file_title}', content) |
| 39 | + original_content = content |
| 40 | + content = re.sub(r'(Title:\s*).*', rf'\g<1>{new_filename}', content) |
38 | 41 | content = re.sub(r'(ID:\s*).*', r'\g<1>1001', content) |
39 | 42 | content = re.sub(r'(Name:\s*).*', r'\g<1>Edwin Kofler', content) |
40 | | - content = re.sub(r'(Date:\s*).*', rf'\g<1>{today}', content) |
41 | | - |
| 43 | + content = re.sub( |
| 44 | + r'(Date:\s*).*', rf'\g<1>{datetime.now().strftime("%m/%d/%Y")}', content |
| 45 | + ) |
42 | 46 | if content != original_content: |
43 | | - with open(current_file_path, 'w', encoding='utf-8') as f: |
| 47 | + with open(cur_filepath, 'w', encoding='utf-8') as f: |
44 | 48 | f.write(content) |
45 | | - print(f'Updated content for: {current_file_path}') |
| 49 | + print(f'Updated content for: {cur_filepath}') |
46 | 50 |
|
47 | | - if new_file_title != file: |
48 | | - new_file_path = os.path.join(root, new_file_title) |
49 | | - os.rename(current_file_path, new_file_path) |
50 | | - print(f'Renamed: {current_file_path} -> {new_file_path}') |
| 51 | + if new_filename != cur_filename: |
| 52 | + os.rename(cur_filepath, new_filepath) |
| 53 | + print(f'Renamed: {cur_filepath} -> {new_filepath}') |
| 54 | + os.symlink(new_filename, cur_filepath) |
0 commit comments