From de3a9b16b51c23c7c2716a09f6972da54ce43895 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Fri, 14 Jul 2023 05:01:24 +0500 Subject: [PATCH 01/12] Introduced console prompt for the choice of function. Implemented a way to differentiate files from folders. Implemented a fix for the import line for test files. Implemented a way to validate prefixes. Implemented a way to find the end of prefix and the end of the exercise number. --- main.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 00eae00..db137e3 100644 --- a/main.py +++ b/main.py @@ -1,33 +1,107 @@ # This is a project for an automated file renamer # TODO: implement GUI # TODO: make the empty-prefixed calls identity operations for those base directories +# TODO: edit the test files to correctly refer to their main files +# TODO: implement other file management features import os +# Rules for prefixes: +# 1. File names are obligated to start with a prefix if there is one. +# 2. Prefixes can't contain any underscores ('_') or digits. +# 3. Prefixes are obligated to be terminated with an underscore + +def validate_prefix(prefix): + return ''.join((char for char in prefix if char not in '_0123456789')) + + +def main_file_name(directory, prefix=None): + res = os.path.basename(directory) + for i in reversed(range(len(res))): + if res[i] == '_': + res = res[:i+1] + prefix + 'f_' + res[i+1:] + return res + + +def find_end_of_prefix(file): + for i in range(len(file)): + if file[i].isdigit(): + return -1 # No prefix found + elif file[i] == '_': + return i + return -1 # No prefix found + + +def find_end_of_exercise_number(file): + prefix_end = find_end_of_prefix(file) + 1 + for i in range(prefix_end, len(file)): + if file[i].isalpha(): + return prefix_end + elif file[i] == '_': + return i + return prefix_end + + def renamer(prefix, base_dir): + prefix = validate_prefix(prefix) for directory in os.listdir(base_dir): - if os.path.isdir(base_dir + '/' + directory): - for file in os.listdir(base_dir + '/' + directory): + new_dir_name = base_dir + '/' + prefix + '_' + directory + directory = base_dir + '/' + directory + if os.path.isdir(directory): + for file in os.listdir(directory): + # TODO: find current prefix end and edit that + prefix_end = find_end_of_prefix(file) + 1 if prefix != '': - new_file_name = base_dir + '/' + directory + '/' + prefix + '_' + file - os.rename(base_dir + '/' + directory + '/' + file, new_file_name) # Adding a prefix + new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] + os.rename(directory + '/' + file, new_file_name) # Adding a prefix # TODO: probably worth it to find a good way to get rid of this 'if' block - if file[3].isalpha(): # Adding a corresponding number to the files with solutions + if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions tmp = '' for char in directory: if char.isdigit(): tmp += char if tmp != '': tmp += '_' - new_name = file[:3] + tmp + file[3:] # The 'cutting' point should not be at index 3, but after - # the prefix - os.rename(base_dir + '/' + directory + '/' + file, base_dir + '/' + directory + '/' + new_name) - new_dir_name = base_dir + '/' + prefix + '_' + directory - os.rename(base_dir + '/' + directory, new_dir_name) + new_name = file[:prefix_end] + tmp + file[prefix_end:] # The 'cutting' point should not be at index 3, but after + # the prefix + os.rename(directory + '/' + file, directory + '/' + new_name) + + os.rename(directory, new_dir_name) + +# TODO: unify this with the renamer function +def imp_adjustment(base_dir): + for directory in os.listdir(base_dir): + directory = base_dir + '/' + directory + if os.path.isdir(directory): + for file in os.listdir(directory): + # A check that makes sure that we are dealing with a test file + if 'test' in file: + file_data = '' + with open(directory + '/' + file, 'r') as f: + file_data = f.read() + + pos_1 = file_data.find('from') + 5 + pos_2 = file_data.find('import') - 1 + file_data = file_data[:pos_1] + main_file_name(directory) + file_data[pos_2:] + + with open(directory + '/' + file, 'w') as f: + f.write(file_data) + else: + for i in reversed(range(len(file))): + if file[i] == '_': + new_name = file[:i + 1] + 'f_' + file[i + 1:] + os.rename(directory + '/' + file, directory + '/' + new_name) + break if __name__ == '__main__': - prefix = input("Prefix: ") - base_dir = input("Base directory: ") - renamer(prefix, base_dir) + while True: + method = input("Method: ") + if method == 'renamer': + prefix = input("Prefix: ") + base_dir = input("Base directory: ") + renamer(prefix, base_dir) + elif method == 'import_adjustment': + base_dir = input("Base directory: ") + imp_adjustment(base_dir) From 949506292325050f65121e9fc3bc3b903a3d841a Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Fri, 14 Jul 2023 07:09:38 +0500 Subject: [PATCH 02/12] Some fixes --- main.py | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index db137e3..4123d42 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ # TODO: implement GUI # TODO: make the empty-prefixed calls identity operations for those base directories # TODO: edit the test files to correctly refer to their main files -# TODO: implement other file management features +# TODO: implement other file management features, like automated file creation import os @@ -13,15 +13,7 @@ # 3. Prefixes are obligated to be terminated with an underscore def validate_prefix(prefix): - return ''.join((char for char in prefix if char not in '_0123456789')) - - -def main_file_name(directory, prefix=None): - res = os.path.basename(directory) - for i in reversed(range(len(res))): - if res[i] == '_': - res = res[:i+1] + prefix + 'f_' + res[i+1:] - return res + return ''.join((char for char in prefix if char not in '_0123456789')) # Deleting unwanted characters def find_end_of_prefix(file): @@ -37,10 +29,20 @@ def find_end_of_exercise_number(file): prefix_end = find_end_of_prefix(file) + 1 for i in range(prefix_end, len(file)): if file[i].isalpha(): - return prefix_end + return prefix_end # No exercise number found elif file[i] == '_': return i - return prefix_end + return prefix_end # No exercise number found + + +def main_file_name(directory, prefix=''): + res = os.path.basename(directory) + pos = find_end_of_prefix(res) + 1 + res = res[:pos] + 'f_' + res[pos:] + # for i in reversed(range(len(res))): + # if res[i] == '_': + # res = res[:i+1] + prefix + 'f_' + res[i+1:] + return res def renamer(prefix, base_dir): @@ -52,9 +54,8 @@ def renamer(prefix, base_dir): for file in os.listdir(directory): # TODO: find current prefix end and edit that prefix_end = find_end_of_prefix(file) + 1 - if prefix != '': - new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] - os.rename(directory + '/' + file, new_file_name) # Adding a prefix + new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] + os.rename(directory + '/' + file, new_file_name) # Adding a prefix # TODO: probably worth it to find a good way to get rid of this 'if' block if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions tmp = '' @@ -69,11 +70,14 @@ def renamer(prefix, base_dir): os.rename(directory, new_dir_name) + # TODO: unify this with the renamer function def imp_adjustment(base_dir): for directory in os.listdir(base_dir): directory = base_dir + '/' + directory if os.path.isdir(directory): + if 'common' in directory: + continue for file in os.listdir(directory): # A check that makes sure that we are dealing with a test file if 'test' in file: @@ -87,12 +91,19 @@ def imp_adjustment(base_dir): with open(directory + '/' + file, 'w') as f: f.write(file_data) + else: - for i in reversed(range(len(file))): - if file[i] == '_': - new_name = file[:i + 1] + 'f_' + file[i + 1:] - os.rename(directory + '/' + file, directory + '/' + new_name) - break + end_of_exercise_number = find_end_of_exercise_number(file) + # In case that we have already marked the file as a file with 'f_', we don't do that again + if file[end_of_exercise_number:end_of_exercise_number+2] != 'f_': + new_name = file[:end_of_exercise_number] + 'f_' + file[end_of_exercise_number:] + os.rename(directory + '/' + file, directory + '/' + new_name) + # else: + # for i in reversed(range(len(file))): + # if file[i] == '_': + # new_name = file[:i + 1] + 'f_' + file[i + 1:] + # os.rename(directory + '/' + file, directory + '/' + new_name) + # break if __name__ == '__main__': From fb65b1759ff21a7ae5d1b4774b96bfce39da66b0 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Fri, 14 Jul 2023 08:51:11 +0500 Subject: [PATCH 03/12] Fixed some issues. Made sure that the current functions work and tested them on the problemsets. --- main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 4123d42..6f935a2 100644 --- a/main.py +++ b/main.py @@ -9,11 +9,11 @@ # Rules for prefixes: # 1. File names are obligated to start with a prefix if there is one. -# 2. Prefixes can't contain any underscores ('_') or digits. +# 2. Prefixes can't contain any whitespaces, underscores ('_') or digits. # 3. Prefixes are obligated to be terminated with an underscore def validate_prefix(prefix): - return ''.join((char for char in prefix if char not in '_0123456789')) # Deleting unwanted characters + return ''.join((char for char in prefix if char not in ' _0123456789')) # Deleting unwanted characters def find_end_of_prefix(file): @@ -48,7 +48,8 @@ def main_file_name(directory, prefix=''): def renamer(prefix, base_dir): prefix = validate_prefix(prefix) for directory in os.listdir(base_dir): - new_dir_name = base_dir + '/' + prefix + '_' + directory + prefix_end = find_end_of_prefix(directory) + 1 + new_dir_name = base_dir + '/' + prefix + '_' + directory[prefix_end:] directory = base_dir + '/' + directory if os.path.isdir(directory): for file in os.listdir(directory): @@ -59,7 +60,9 @@ def renamer(prefix, base_dir): # TODO: probably worth it to find a good way to get rid of this 'if' block if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions tmp = '' - for char in directory: + for char in os.path.basename(directory): + # TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the + # loop when we stumble upon an underscore if char.isdigit(): tmp += char if tmp != '': @@ -86,7 +89,7 @@ def imp_adjustment(base_dir): file_data = f.read() pos_1 = file_data.find('from') + 5 - pos_2 = file_data.find('import') - 1 + pos_2 = file_data.find('import', pos_1) - 1 file_data = file_data[:pos_1] + main_file_name(directory) + file_data[pos_2:] with open(directory + '/' + file, 'w') as f: From c63763acc6e042c39af47ddf71c1e2bb15658568 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Wed, 23 Aug 2023 14:33:39 +0500 Subject: [PATCH 04/12] Added a function for automated file creation --- main.py | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 6f935a2..6fb7445 100644 --- a/main.py +++ b/main.py @@ -19,20 +19,20 @@ def validate_prefix(prefix): def find_end_of_prefix(file): for i in range(len(file)): if file[i].isdigit(): - return -1 # No prefix found + return -1 # No prefix found elif file[i] == '_': return i - return -1 # No prefix found + return -1 # No prefix found def find_end_of_exercise_number(file): prefix_end = find_end_of_prefix(file) + 1 for i in range(prefix_end, len(file)): if file[i].isalpha(): - return prefix_end # No exercise number found + return prefix_end # No exercise number found elif file[i] == '_': return i - return prefix_end # No exercise number found + return prefix_end # No exercise number found def main_file_name(directory, prefix=''): @@ -53,12 +53,11 @@ def renamer(prefix, base_dir): directory = base_dir + '/' + directory if os.path.isdir(directory): for file in os.listdir(directory): - # TODO: find current prefix end and edit that prefix_end = find_end_of_prefix(file) + 1 new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] - os.rename(directory + '/' + file, new_file_name) # Adding a prefix + os.rename(directory + '/' + file, new_file_name) # Adding a prefix # TODO: probably worth it to find a good way to get rid of this 'if' block - if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions + if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions tmp = '' for char in os.path.basename(directory): # TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the @@ -67,8 +66,9 @@ def renamer(prefix, base_dir): tmp += char if tmp != '': tmp += '_' - new_name = file[:prefix_end] + tmp + file[prefix_end:] # The 'cutting' point should not be at index 3, but after - # the prefix + new_name = file[:prefix_end] + tmp + file[ + prefix_end:] # The 'cutting' point should not be at index 3, but after + # the prefix os.rename(directory + '/' + file, directory + '/' + new_name) os.rename(directory, new_dir_name) @@ -98,7 +98,7 @@ def imp_adjustment(base_dir): else: end_of_exercise_number = find_end_of_exercise_number(file) # In case that we have already marked the file as a file with 'f_', we don't do that again - if file[end_of_exercise_number:end_of_exercise_number+2] != 'f_': + if file[end_of_exercise_number:end_of_exercise_number + 2] != 'f_': new_name = file[:end_of_exercise_number] + 'f_' + file[end_of_exercise_number:] os.rename(directory + '/' + file, directory + '/' + new_name) # else: @@ -109,6 +109,35 @@ def imp_adjustment(base_dir): # break +# The function for transforming names of problems into appropriate file names +# TODO: currently, only supports Leetcode. Need to make it more general. +def get_file_name(problem_name): + trans_dict = str.maketrans(' ', '_', '.') + res = problem_name.translate(trans_dict).lower() + res = res[:res.find('_') + 1] + 'f_' + res[res.find('_') + 1:] + return res + + +# TODO: implement automated file creation +def file_creation(base_dir, prefix, problem_name): + prefix = validate_prefix(prefix) + + main_file_name = prefix + get_file_name(problem_name) + directory_name = os.path.join(base_dir, main_file_name.replace('f_', '')) + test_file_name = main_file_name[:find_end_of_exercise_number()] + '_test.py' + + if os.path.isdir(directory_name): + os.mkdir(base_dir + '/' + directory_name) + + with open(directory_name + '/' + main_file_name + '.py', 'x') as f: + pass + + with open(directory_name + '/' + test_file_name + '.py', 'w') as new_test_file,\ + open(base_dir + f'/{prefix}_common/{prefix}_test_template.py') as template: + for line in template: + new_test_file.write(line) + + if __name__ == '__main__': while True: method = input("Method: ") From 3ed1ca384ddc711feefeda487f284f6ee9205572 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Thu, 24 Aug 2023 15:50:19 +0500 Subject: [PATCH 05/12] Working version of the automated file manager. Basic functionality implemented. --- main.py | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 6fb7445..64fd168 100644 --- a/main.py +++ b/main.py @@ -45,7 +45,7 @@ def main_file_name(directory, prefix=''): return res -def renamer(prefix, base_dir): +def renamer(base_dir, prefix): prefix = validate_prefix(prefix) for directory in os.listdir(base_dir): prefix_end = find_end_of_prefix(directory) + 1 @@ -122,29 +122,42 @@ def get_file_name(problem_name): def file_creation(base_dir, prefix, problem_name): prefix = validate_prefix(prefix) - main_file_name = prefix + get_file_name(problem_name) + main_file_name = prefix + '_' + get_file_name(problem_name) directory_name = os.path.join(base_dir, main_file_name.replace('f_', '')) - test_file_name = main_file_name[:find_end_of_exercise_number()] + '_test.py' - - if os.path.isdir(directory_name): - os.mkdir(base_dir + '/' + directory_name) - - with open(directory_name + '/' + main_file_name + '.py', 'x') as f: + test_file_name = main_file_name[:find_end_of_exercise_number(main_file_name)] + '_test.py' + + if not os.path.isdir(directory_name): + os.mkdir(directory_name) + try: + with open(directory_name + '/' + main_file_name + '.py', 'x') as f: + pass + except FileExistsError: pass - with open(directory_name + '/' + test_file_name + '.py', 'w') as new_test_file,\ - open(base_dir + f'/{prefix}_common/{prefix}_test_template.py') as template: - for line in template: - new_test_file.write(line) + with open(directory_name + '/' + test_file_name + '.py', 'w') as new_test_file: + try: + with open(base_dir + f'/{prefix}_common/{prefix}_test_template.py', 'r') as template: + for line in template: + new_test_file.write(line) + except FileNotFoundError: + pass if __name__ == '__main__': + # TODO: consider generalising the handling of the method prompt using the function dictionary + # function_dict = {'renamer': renamer, 'import_adjustment': imp_adjustment, 'file_creation': file_creation} + while True: method = input("Method: ") if method == 'renamer': - prefix = input("Prefix: ") base_dir = input("Base directory: ") - renamer(prefix, base_dir) + prefix = input("Prefix: ") + renamer(base_dir, prefix) elif method == 'import_adjustment': base_dir = input("Base directory: ") imp_adjustment(base_dir) + elif method == 'file_creation': + base_dir = input("Base directory: ") + prefix = input("Prefix: ") + problem_name = input("Problem name: ") + file_creation(base_dir, prefix, problem_name) \ No newline at end of file From f77a8368600cdc88210c2f8ef2120d207ab3b6bb Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Wed, 6 Sep 2023 00:12:11 +0500 Subject: [PATCH 06/12] Starting to work on the GUI --- .gitignore | 3 + common.py | 169 ++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 186 +++++++---------------------------------------------- 3 files changed, 197 insertions(+), 161 deletions(-) create mode 100644 common.py diff --git a/.gitignore b/.gitignore index d730361..b1154b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # file-renamer project-specific testing_grounds/ +# configuration files +settings/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/common.py b/common.py new file mode 100644 index 0000000..dc0d855 --- /dev/null +++ b/common.py @@ -0,0 +1,169 @@ +# This is a project for an automated file renamer +# TODO: implement GUI +# TODO: make the empty-prefixed calls identity operations for those base directories +# TODO: edit the test files to correctly refer to their main files +# TODO: implement other file management features, like automated file creation + +import os + + +# Rules for prefixes: +# 1. File names are obligated to start with a prefix if there is one. +# 2. Prefixes can't contain any whitespaces, underscores ('_') or digits. +# 3. Prefixes are obligated to be terminated with an underscore + +def validate_prefix(prefix): + return ''.join((char for char in prefix if char not in ' _0123456789')) # Deleting unwanted characters + + +def find_end_of_prefix(file): + for i in range(len(file)): + if file[i].isdigit(): + return -1 # No prefix found + elif file[i] == '_': + return i + return -1 # No prefix found + + +def find_end_of_exercise_number(file): + prefix_end = find_end_of_prefix(file) + 1 + for i in range(prefix_end, len(file)): + if file[i].isalpha(): + return prefix_end # No exercise number found + elif file[i] == '_': + return i + return prefix_end # No exercise number found + + +def main_file_name(directory, prefix=''): + res = os.path.basename(directory) + pos = find_end_of_prefix(res) + 1 + res = res[:pos] + 'f_' + res[pos:] + # for i in reversed(range(len(res))): + # if res[i] == '_': + # res = res[:i+1] + prefix + 'f_' + res[i+1:] + return res + + +def renamer(base_dir, prefix): + prefix = validate_prefix(prefix) + for directory in os.listdir(base_dir): + prefix_end = find_end_of_prefix(directory) + 1 + new_dir_name = base_dir + '/' + prefix + '_' + directory[prefix_end:] + directory = base_dir + '/' + directory + if os.path.isdir(directory): + for file in os.listdir(directory): + prefix_end = find_end_of_prefix(file) + 1 + new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] + os.rename(directory + '/' + file, new_file_name) # Adding a prefix + # TODO: probably worth it to find a good way to get rid of this 'if' block + if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions + tmp = '' + for char in os.path.basename(directory): + # TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the + # loop when we stumble upon an underscore + if char.isdigit(): + tmp += char + if tmp != '': + tmp += '_' + new_name = file[:prefix_end] + tmp + file[ + prefix_end:] # The 'cutting' point should not be at index 3, but after + # the prefix + os.rename(directory + '/' + file, directory + '/' + new_name) + + os.rename(directory, new_dir_name) + + +# TODO: unify this with the renamer function +def imp_adjustment(base_dir): + for directory in os.listdir(base_dir): + directory = base_dir + '/' + directory + if os.path.isdir(directory): + if 'common' in directory: + continue + for file in os.listdir(directory): + # A check that makes sure that we are dealing with a test file + if 'test' in file: + file_data = '' + with open(directory + '/' + file, 'r') as f: + file_data = f.read() + + pos_1 = file_data.find('from') + 5 + pos_2 = file_data.find('import', pos_1) - 1 + file_data = file_data[:pos_1] + main_file_name(directory) + file_data[pos_2:] + + with open(directory + '/' + file, 'w') as f: + f.write(file_data) + + else: + end_of_exercise_number = find_end_of_exercise_number(file) + # In case that we have already marked the file as a file with 'f_', we don't do that again + if file[end_of_exercise_number:end_of_exercise_number + 2] != 'f_': + new_name = file[:end_of_exercise_number] + 'f_' + file[end_of_exercise_number:] + os.rename(directory + '/' + file, directory + '/' + new_name) + # else: + # for i in reversed(range(len(file))): + # if file[i] == '_': + # new_name = file[:i + 1] + 'f_' + file[i + 1:] + # os.rename(directory + '/' + file, directory + '/' + new_name) + # break + + +# The function for transforming names of problems into appropriate file names +# TODO: currently, only supports Leetcode. Need to make it more general. +def get_file_name(problem_name): + trans_dict = str.maketrans(' ', '_', '.') + res = problem_name.translate(trans_dict).lower() + res = res[:res.find('_') + 1] + 'f_' + res[res.find('_') + 1:] + return res + + +# TODO: implement automated file creation +def file_creation(base_dir, prefix, problem_name): + prefix = validate_prefix(prefix) + + main_file_name = prefix + '_' + get_file_name(problem_name) + directory_name = os.path.join(base_dir, main_file_name.replace('f_', '')) + test_file_name = main_file_name[:find_end_of_exercise_number(main_file_name)] + '_test' + + if not os.path.isdir(directory_name): + os.mkdir(directory_name) + + try: + with open(directory_name + '/' + main_file_name + '.py', 'x') as f: + pass + except FileExistsError: + pass + + with open(directory_name + '/' + test_file_name + '.py', 'w') as new_test_file: + try: + with open(base_dir + f'/{prefix}_common/{prefix}_test_template.py', 'r') as template: + for line in template: + new_test_file.write(line) + except FileNotFoundError: + pass + +def general_function_handler(method, base_dir, prefix, problem_name): + pass + + +if __name__ == '__main__': + # TODO: consider generalising the handling of the method prompt using the function dictionary + # function_dict = {'renamer': renamer, 'import_adjustment': imp_adjustment, 'file_creation': file_creation} + + # TODO: consider allowing users to save some parameters during execution + while True: + + method = str(input("Method: ")) + if method == 'renamer': + base_dir = str(input("Base directory: ")) + prefix = str(input("Prefix: ")) + renamer(base_dir, prefix) + elif method == 'import_adjustment': + base_dir = str(input("Base directory: ")) + imp_adjustment(base_dir) + elif method == 'file_creation': + base_dir = str(input("Base directory: ")) + prefix = str(input("Prefix: ")) + problem_name = str(input("Problem name: ")) + file_creation(base_dir, prefix, problem_name) \ No newline at end of file diff --git a/main.py b/main.py index 64fd168..2410d18 100644 --- a/main.py +++ b/main.py @@ -1,163 +1,27 @@ -# This is a project for an automated file renamer -# TODO: implement GUI -# TODO: make the empty-prefixed calls identity operations for those base directories -# TODO: edit the test files to correctly refer to their main files -# TODO: implement other file management features, like automated file creation - -import os - - -# Rules for prefixes: -# 1. File names are obligated to start with a prefix if there is one. -# 2. Prefixes can't contain any whitespaces, underscores ('_') or digits. -# 3. Prefixes are obligated to be terminated with an underscore - -def validate_prefix(prefix): - return ''.join((char for char in prefix if char not in ' _0123456789')) # Deleting unwanted characters - - -def find_end_of_prefix(file): - for i in range(len(file)): - if file[i].isdigit(): - return -1 # No prefix found - elif file[i] == '_': - return i - return -1 # No prefix found - - -def find_end_of_exercise_number(file): - prefix_end = find_end_of_prefix(file) + 1 - for i in range(prefix_end, len(file)): - if file[i].isalpha(): - return prefix_end # No exercise number found - elif file[i] == '_': - return i - return prefix_end # No exercise number found - - -def main_file_name(directory, prefix=''): - res = os.path.basename(directory) - pos = find_end_of_prefix(res) + 1 - res = res[:pos] + 'f_' + res[pos:] - # for i in reversed(range(len(res))): - # if res[i] == '_': - # res = res[:i+1] + prefix + 'f_' + res[i+1:] - return res - - -def renamer(base_dir, prefix): - prefix = validate_prefix(prefix) - for directory in os.listdir(base_dir): - prefix_end = find_end_of_prefix(directory) + 1 - new_dir_name = base_dir + '/' + prefix + '_' + directory[prefix_end:] - directory = base_dir + '/' + directory - if os.path.isdir(directory): - for file in os.listdir(directory): - prefix_end = find_end_of_prefix(file) + 1 - new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] - os.rename(directory + '/' + file, new_file_name) # Adding a prefix - # TODO: probably worth it to find a good way to get rid of this 'if' block - if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions - tmp = '' - for char in os.path.basename(directory): - # TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the - # loop when we stumble upon an underscore - if char.isdigit(): - tmp += char - if tmp != '': - tmp += '_' - new_name = file[:prefix_end] + tmp + file[ - prefix_end:] # The 'cutting' point should not be at index 3, but after - # the prefix - os.rename(directory + '/' + file, directory + '/' + new_name) - - os.rename(directory, new_dir_name) - - -# TODO: unify this with the renamer function -def imp_adjustment(base_dir): - for directory in os.listdir(base_dir): - directory = base_dir + '/' + directory - if os.path.isdir(directory): - if 'common' in directory: - continue - for file in os.listdir(directory): - # A check that makes sure that we are dealing with a test file - if 'test' in file: - file_data = '' - with open(directory + '/' + file, 'r') as f: - file_data = f.read() - - pos_1 = file_data.find('from') + 5 - pos_2 = file_data.find('import', pos_1) - 1 - file_data = file_data[:pos_1] + main_file_name(directory) + file_data[pos_2:] - - with open(directory + '/' + file, 'w') as f: - f.write(file_data) - - else: - end_of_exercise_number = find_end_of_exercise_number(file) - # In case that we have already marked the file as a file with 'f_', we don't do that again - if file[end_of_exercise_number:end_of_exercise_number + 2] != 'f_': - new_name = file[:end_of_exercise_number] + 'f_' + file[end_of_exercise_number:] - os.rename(directory + '/' + file, directory + '/' + new_name) - # else: - # for i in reversed(range(len(file))): - # if file[i] == '_': - # new_name = file[:i + 1] + 'f_' + file[i + 1:] - # os.rename(directory + '/' + file, directory + '/' + new_name) - # break - - -# The function for transforming names of problems into appropriate file names -# TODO: currently, only supports Leetcode. Need to make it more general. -def get_file_name(problem_name): - trans_dict = str.maketrans(' ', '_', '.') - res = problem_name.translate(trans_dict).lower() - res = res[:res.find('_') + 1] + 'f_' + res[res.find('_') + 1:] - return res - - -# TODO: implement automated file creation -def file_creation(base_dir, prefix, problem_name): - prefix = validate_prefix(prefix) - - main_file_name = prefix + '_' + get_file_name(problem_name) - directory_name = os.path.join(base_dir, main_file_name.replace('f_', '')) - test_file_name = main_file_name[:find_end_of_exercise_number(main_file_name)] + '_test.py' - - if not os.path.isdir(directory_name): - os.mkdir(directory_name) - try: - with open(directory_name + '/' + main_file_name + '.py', 'x') as f: - pass - except FileExistsError: - pass - - with open(directory_name + '/' + test_file_name + '.py', 'w') as new_test_file: - try: - with open(base_dir + f'/{prefix}_common/{prefix}_test_template.py', 'r') as template: - for line in template: - new_test_file.write(line) - except FileNotFoundError: - pass - +import tkinter as tk +from common import general_function_handler + +# Master section +root = tk.Tk() +root.title('File Manager') + +# Label section +method_label = tk.Label(root, text='Method: ') +base_dir_label = tk.Label(root, text='Base directory: ') +prefix_label = tk.Label(root, text='Prefix: ') +problem_name_label = tk.Label(root, text='Problem name: ') + +# Entry section +# TODO: make method_entry a drop-down menu +method_entry = tk.Entry(root) +base_dir_entry = tk.Entry(root) +prefix_entry = tk.Entry(root) +problem_name_entry = tk.Entry(root) + +# Button section +ok_button = tk.Button(root, text='Ok', + command=lambda: general_function_handler(method_entry.get(), base_dir_entry.get(), + prefix_entry.get(), problem_name_entry.get())) if __name__ == '__main__': - # TODO: consider generalising the handling of the method prompt using the function dictionary - # function_dict = {'renamer': renamer, 'import_adjustment': imp_adjustment, 'file_creation': file_creation} - - while True: - method = input("Method: ") - if method == 'renamer': - base_dir = input("Base directory: ") - prefix = input("Prefix: ") - renamer(base_dir, prefix) - elif method == 'import_adjustment': - base_dir = input("Base directory: ") - imp_adjustment(base_dir) - elif method == 'file_creation': - base_dir = input("Base directory: ") - prefix = input("Prefix: ") - problem_name = input("Problem name: ") - file_creation(base_dir, prefix, problem_name) \ No newline at end of file + root.mainloop() \ No newline at end of file From 40ba591dcc85b6e6f146cbcbc9557d65871ca731 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Mon, 11 Sep 2023 21:00:19 +0500 Subject: [PATCH 07/12] Added basic exception handling for empty necessary arguments. Added a button for asking for base directory. --- assets/folder_icon.png | Bin 0 -> 985 bytes common.py | 29 +++++++++++++++---- exceptions.pyi | 10 +++++++ main.py | 63 +++++++++++++++++++++++++++++++++++------ 4 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 assets/folder_icon.png create mode 100644 exceptions.pyi diff --git a/assets/folder_icon.png b/assets/folder_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..51e4a29c5d03aa02b8051c0863f9fb92f478ee0f GIT binary patch literal 985 zcmV;~119{5P)j?LPQW)+0emOmn*}dG4S+GG7B~R30ITOw-w4nT zT$VIG|McR=01ZGNu)$%ygFu_4w+`!!g#b8#+r|TI1g;okY8=<=oB(K#sqQVn@tEp% zh5_On3;zaMCH2L4jd>Bb006_ldP!q3UuRBK;xV`exLASf%!x|$fC8PsKumQD7?$*6 zt|w11iNNuvOAvn8H~c3NHqZg)N*#0RT3IEtpjS>cSSxssN#>3g9~d z-^^vUCSVA7RF(x24*w~f-O~cLf7w`zU4cwRXvv6Rx3C3o7@&K#_UWPa?$?*VKR`@PjuHdbr z#xDn+CZGW>;t9|+n*gmv0`w#xahK6iPJnwwjo+4lgk8r#IRV;>1h|%fgk8t`QUVMC z#%lcf1SA_rTrc(bI-%)HM>s$O_wDI!KyDu3+b!2I0jwysKENIa>?Jqx$r|S*jg~xW z^?=9tcPkF}SC(5L5&#%u+Hk*EG6|fQ^sa1)y!&_X@0RQM&MfXrA~43Z0NbMpn+h(> z>VBRZ_;)J~_g95kB^Cg8Lu;BI4oCTdT?0JCziY0dGtxRiNf=|60K0*G Date: Mon, 18 Sep 2023 05:37:39 +0500 Subject: [PATCH 08/12] Fixed wrong file extension Safety update --- common.py | 1 + exceptions.pyi => exceptions.py | 0 main.py | 2 ++ 3 files changed, 3 insertions(+) rename exceptions.pyi => exceptions.py (100%) diff --git a/common.py b/common.py index 84c8761..3c375d8 100644 --- a/common.py +++ b/common.py @@ -5,6 +5,7 @@ # TODO: implement other file management features, like automated file creation import os +# from exceptions import EmptyArgsException from exceptions import EmptyArgsException # Rules for prefixes: diff --git a/exceptions.pyi b/exceptions.py similarity index 100% rename from exceptions.pyi rename to exceptions.py diff --git a/main.py b/main.py index eaf8db2..06c63e3 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import tkinter as tk from tkinter import filedialog from common import general_function_handler +from PIL import Image # Master section @@ -63,6 +64,7 @@ def set_base_dir(): base_dir_entry.insert(0, filedialog.askdirectory()) return +original_image = Image.open(r'assets\folder_icon.png') directory_icon = tk.PhotoImage(file=r'assets\folder_icon.png') directory_button = tk.Button(base_dir_frame, image=directory_icon, command=lambda: set_base_dir()) directory_button.grid(row=0, column=2) From 32f45692417dd6214afc25254b4e3df06db449a7 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Mon, 16 Oct 2023 02:37:13 +0500 Subject: [PATCH 09/12] Introduced the preset feature - needs testing. --- .gitignore | 2 +- .idea/.gitignore | 3 - .idea/{file-renamer.iml => file-manager.iml} | 0 .idea/misc.xml | 2 +- .idea/modules.xml | 2 +- .idea/workspace.xml | 74 +++++++++++ common.py | 3 - main.py | 125 +++++++++++++------ 8 files changed, 162 insertions(+), 49 deletions(-) delete mode 100644 .idea/.gitignore rename .idea/{file-renamer.iml => file-manager.iml} (100%) create mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index b1154b6..82c9200 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -# file-renamer project-specific +# file-manager project-specific testing_grounds/ # configuration files diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/file-renamer.iml b/.idea/file-manager.iml similarity index 100% rename from .idea/file-renamer.iml rename to .idea/file-manager.iml diff --git a/.idea/misc.xml b/.idea/misc.xml index 959ac8d..1caff08 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 70f2d55..1a85d97 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..2eda1f6 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1697301999451 + + + + \ No newline at end of file diff --git a/common.py b/common.py index 3c375d8..0a8eb7d 100644 --- a/common.py +++ b/common.py @@ -1,8 +1,5 @@ # This is a project for an automated file renamer -# TODO: implement GUI # TODO: make the empty-prefixed calls identity operations for those base directories -# TODO: edit the test files to correctly refer to their main files -# TODO: implement other file management features, like automated file creation import os # from exceptions import EmptyArgsException diff --git a/main.py b/main.py index 06c63e3..85609cf 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ +import os.path import tkinter as tk from tkinter import filedialog from common import general_function_handler -from PIL import Image +from PIL import Image, ImageTk # Master section @@ -9,65 +10,109 @@ root.title('File Manager') root.minsize(400, 250) +# TODO: consider organising things into classes + # Frame section -# TODO: unify the frames into a single one and use a grid to align the labels and the entries -# TODO: set the frame sizes -method_frame = tk.Frame(root) -method_frame.pack(padx=50, pady=10) -for c in range(3): method_frame.columnconfigure(index=c, weight=1) -for r in range(1): method_frame.rowconfigure(index=r, weight=1) -base_dir_frame = tk.Frame(root) -base_dir_frame.pack(padx=50, pady=10) -for c in range(3): base_dir_frame.columnconfigure(index=c, weight=1) # Leaving one column for a button for file browsing -for r in range(1): base_dir_frame.rowconfigure(index=r, weight=1) -prefix_frame = tk.Frame(root) -prefix_frame.pack(padx=50, pady=10) -for c in range(3): prefix_frame.columnconfigure(index=c, weight=1) -for r in range(1): prefix_frame.rowconfigure(index=r, weight=1) -problem_name_frame = tk.Frame(root) -problem_name_frame.pack(padx=50, pady=10) -for c in range(3): problem_name_frame.columnconfigure(index=c, weight=1) -for r in range(1): problem_name_frame.rowconfigure(index=r, weight=1) +# TODO: make the font size bigger, and make the sizes of other elements depend on the font size +# # Main body frame +# main_body_frame = tk.Frame(root) +# main_body_frame.pack(padx=50, pady=10, fill='x') +# for r in range(2): main_body_frame.rowconfigure(index=r, weight=1) +# Label-and-entry frame +label_and_entry_frame = tk.Frame(root, relief='raised', borderwidth=5) # TODO: remove relief and borderwidth after done testing +# label_and_entry_frame.grid(row=0, column=0, sticky='nsew') +label_and_entry_frame.pack(padx=50, pady=10, fill='x') +for c in range(4): label_and_entry_frame.columnconfigure(index=c, weight=1) +for r in range(6): label_and_entry_frame.rowconfigure(index=r, weight=1) +label_and_entry_frame.columnconfigure(index=1, weight=2) +# # Preset frame +# preset_frame = tk.Frame(main_body_frame) +# preset_frame.grid(row=1, column=0, sticky='nsew') +# for c in range(4): +# bottom frame bottom_frame = tk.Frame(root) bottom_frame.pack(side='bottom', fill='x') # Label section -method_label = tk.Label(method_frame, text='Method: ') -method_label.grid(row=0, column=0) -base_dir_label = tk.Label(base_dir_frame, text='Base directory: ') -base_dir_label.grid(row=0, column=0) -prefix_label = tk.Label(prefix_frame, text='Prefix: ') -prefix_label.grid(row=0, column=0) -problem_name_label = tk.Label(problem_name_frame, text='Problem name: ') -problem_name_label.grid(row=0, column=0) +method_label = tk.Label(label_and_entry_frame, text='Method: ') +method_label.grid(row=0, column=0, sticky='E') +base_dir_label = tk.Label(label_and_entry_frame, text='Base directory: ') +base_dir_label.grid(row=1, column=0, sticky='E') +prefix_label = tk.Label(label_and_entry_frame, text='Prefix: ') +prefix_label.grid(row=2, column=0, sticky='E') +problem_name_label = tk.Label(label_and_entry_frame, text='Problem name: ') +problem_name_label.grid(row=3, column=0, sticky='E') +preset_name_label = tk.Label(label_and_entry_frame, text='Preset name: ') +preset_name_label.grid(row=5, column=0, sticky='E') # Entry section # TODO: make method_entry a drop-down menu -method_entry = tk.Entry(method_frame) -method_entry.grid(row=0, column=1) -base_dir_entry = tk.Entry(base_dir_frame) -base_dir_entry.grid(row=0, column=1) -prefix_entry = tk.Entry(prefix_frame) -prefix_entry.grid(row=0, column=1) -problem_name_entry = tk.Entry(problem_name_frame) -problem_name_entry.grid(row=0, column=1) +method_entry = tk.Entry(label_and_entry_frame) +method_entry.grid(row=0, column=1, sticky='EW') +base_dir_entry = tk.Entry(label_and_entry_frame) +base_dir_entry.grid(row=1, column=1, sticky='EW') +prefix_entry = tk.Entry(label_and_entry_frame) +prefix_entry.grid(row=2, column=1, sticky='EW') +problem_name_entry = tk.Entry(label_and_entry_frame) +problem_name_entry.grid(row=3, column=1, sticky='EW') +preset_name_entry = tk.Entry(label_and_entry_frame) +preset_name_entry.grid(row=5, column=1, sticky='EW') + +ENTRY_OBJECT_DICT = {'method': method_entry, 'base dir': base_dir_entry, 'prefix': prefix_entry, + 'problem name': problem_name_entry, 'preset name': preset_name_entry} # Button section -# TODO: introduce a file explorer button for easier base_dir selection ok_button = tk.Button(bottom_frame, text='Ok', command=lambda: general_function_handler(method_entry.get(), base_dir_entry.get(), prefix_entry.get(), problem_name_entry.get())) ok_button.pack() def set_base_dir(): - base_dir_entry.delete(0,tk.END) + base_dir_entry.delete(0, tk.END) base_dir_entry.insert(0, filedialog.askdirectory()) return original_image = Image.open(r'assets\folder_icon.png') -directory_icon = tk.PhotoImage(file=r'assets\folder_icon.png') -directory_button = tk.Button(base_dir_frame, image=directory_icon, command=lambda: set_base_dir()) -directory_button.grid(row=0, column=2) +directory_icon = ImageTk.PhotoImage(original_image.resize((16, 16))) +directory_button = tk.Button(label_and_entry_frame, image=directory_icon, command=lambda: set_base_dir()) +directory_button.grid(row=1, column=2, sticky='W') + + +def save_preset(): + param_dict = {} + for key, entry_object in ENTRY_OBJECT_DICT.items(): + param_dict[key] = entry_object.get() + if not os.path.isdir('./preset'): + os.makedirs('./preset') + with open(param_dict['Preset name']) as file: + for key, value in param_dict.items(): + if not key == 'Preset name': + file.write(key + ': ' + value + '\n') + +save_preset_button = tk.Button(label_and_entry_frame, text='save preset', command=save_preset) +save_preset_button.grid(row=5, column=2, sticky='W') + + +def choose_preset(): + filename = filedialog.askopenfilename() + param_dict = {} + for key in ENTRY_OBJECT_DICT.keys(): + param_dict[key] = None + with open(filename, 'r') as file: + param_dict['Preset name'] = os.path.basename(file.name) + for line in file: + if line.strip() == '': + continue + key, value = line.split(': ', 1) + param_dict[key] = value + for key, value in param_dict.items(): + if value is not None: + ENTRY_OBJECT_DICT[key].delete(0, tk.END) + ENTRY_OBJECT_DICT[key].insert(0, value) + +choose_preset_button = tk.Button(label_and_entry_frame, text='choose preset', command=choose_preset) +choose_preset_button.grid(row=5, column=3, sticky='W') + if __name__ == '__main__': saved_base_dir = '' From bdc35681a146e21280c8e98ed8180f4c58fa209f Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Thu, 19 Oct 2023 04:39:37 +0500 Subject: [PATCH 10/12] Reworked the renamed function. Both the UI changes from the previous update and the function change require further testing --- .idea/workspace.xml | 7 +---- common.py | 77 ++++++++++++++++++++++++++++++++------------- main.py | 3 +- 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2eda1f6..709bdbb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,12 +2,7 @@ - - - - - - + diff --git a/common.py b/common.py index 0a8eb7d..80dea97 100644 --- a/common.py +++ b/common.py @@ -33,7 +33,7 @@ def find_end_of_prefix(file): return -1 # No prefix found -def find_end_of_exercise_number(file): +def find_end_of_exercise_number_from_file(file): prefix_end = find_end_of_prefix(file) + 1 for i in range(prefix_end, len(file)): if file[i].isalpha(): @@ -43,6 +43,11 @@ def find_end_of_exercise_number(file): return prefix_end # No exercise number found +def find_end_of_exercise_number_from_directory(directory): + return os.path.basename(directory).split('_')[1] + + + def main_file_name(directory, prefix=''): res = os.path.basename(directory) pos = find_end_of_prefix(res) + 1 @@ -60,24 +65,52 @@ def renamer(base_dir, prefix, problem_name=None): new_dir_name = base_dir + '/' + prefix + '_' + directory[prefix_end:] directory = base_dir + '/' + directory if os.path.isdir(directory): - for file in os.listdir(directory): - prefix_end = find_end_of_prefix(file) + 1 - new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] - os.rename(directory + '/' + file, new_file_name) # Adding a prefix - # TODO: probably worth it to find a good way to get rid of this 'if' block - if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions - tmp = '' - for char in os.path.basename(directory): - # TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the - # loop when we stumble upon an underscore - if char.isdigit(): - tmp += char - if tmp != '': - tmp += '_' - new_name = file[:prefix_end] + tmp + file[ - prefix_end:] # The 'cutting' point should not be at index 3, but after - # the prefix - os.rename(directory + '/' + file, directory + '/' + new_name) + if 'common' not in os.path.basename(directory): + for file in os.listdir(directory): + prefix_end = find_end_of_prefix(file) + 1 + new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] + os.rename(directory + '/' + file, new_file_name) # Adding a prefix + # TODO: probably worth it to find a good way to get rid of this 'if' block + # Checking for if the character at the prefix_end is a letter prevents us from adding a + # problem number for no good reason + if file[prefix_end].isalpha(): # Adding a corresponding number to the files with solutions + # tmp = '' + # for char in os.path.basename(directory): + # # TODO: introduce a flag to check if we have stumbled upon a number, and use it to stop the + # # loop when we stumble upon an underscore + # if char.isdigit(): + # tmp += char + # if tmp != '': + # tmp += '_' + problem_number = find_end_of_exercise_number_from_directory(directory) + + # new_name = file[:prefix_end] + problem_number + file[ + # prefix_end:] # The 'cutting' point should not be at index 3, but after + # # the prefix + + if 'test' in os.path.basename(file): + file_data = '' + with open(directory + '/' + file, 'r') as f: + file_data = f.read() + + pos_1 = file_data.find('from') + 5 + pos_2 = file_data.find('import', pos_1) - 1 + file_data = file_data[:pos_1] + main_file_name(directory) + file_data[pos_2:] + + with open(directory + '/' + file, 'w') as f: + f.write(file_data) + + new_name = file[:prefix_end] + problem_number + file[ + prefix_end:] # The 'cutting' point should not be at index 3, but after + # the prefix + else: + new_name = file[:prefix_end] + problem_number + 'f_' + file[ + prefix_end:] # The 'cutting' point should not be at index 3, but after + # the prefix + os.rename(directory + '/' + file, directory + '/' + new_name) + + + os.rename(directory, new_dir_name) @@ -87,7 +120,7 @@ def imp_adjustment(base_dir, prefix=None, problem_name=None): for directory in os.listdir(base_dir): directory = base_dir + '/' + directory if os.path.isdir(directory): - if 'common' in directory: + if 'common' in os.path.basename(directory): continue for file in os.listdir(directory): # A check that makes sure that we are dealing with a test file @@ -104,7 +137,7 @@ def imp_adjustment(base_dir, prefix=None, problem_name=None): f.write(file_data) else: - end_of_exercise_number = find_end_of_exercise_number(file) + end_of_exercise_number = find_end_of_exercise_number_from_file(file) # In case that we have already marked the file as a file with 'f_', we don't do that again if file[end_of_exercise_number:end_of_exercise_number + 2] != 'f_': new_name = file[:end_of_exercise_number] + 'f_' + file[end_of_exercise_number:] @@ -137,7 +170,7 @@ def file_creation(base_dir, prefix, problem_name): main_file_name = prefix + '_' + get_file_name(problem_name) directory_name = os.path.join(base_dir, main_file_name.replace('f_', '')) - test_file_name = main_file_name[:find_end_of_exercise_number(main_file_name)] + '_test' + test_file_name = main_file_name[:find_end_of_exercise_number_from_file(main_file_name)] + '_test' if not os.path.isdir(directory_name): os.mkdir(directory_name) diff --git a/main.py b/main.py index 85609cf..1fe26b3 100644 --- a/main.py +++ b/main.py @@ -84,7 +84,8 @@ def save_preset(): param_dict[key] = entry_object.get() if not os.path.isdir('./preset'): os.makedirs('./preset') - with open(param_dict['Preset name']) as file: + # TODO: consider the case where the name is empty + with open(param_dict['Preset name'], 'w') as file: for key, value in param_dict.items(): if not key == 'Preset name': file.write(key + ': ' + value + '\n') From 77fea6b7a6c673e7a14d5f3ce606fcd9aa34b162 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Fri, 20 Oct 2023 01:21:30 +0500 Subject: [PATCH 11/12] Debugging done. New features confirmed to work. --- .gitignore | 1 + .idea/file-manager.iml | 5 +++++ .idea/workspace.xml | 25 +++++++++++++++++++++++++ common.py | 6 +++--- main.py | 5 +++-- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 82c9200..aeb5943 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # file-manager project-specific testing_grounds/ +preset/ # configuration files settings/ diff --git a/.idea/file-manager.iml b/.idea/file-manager.iml index 74d515a..5319422 100644 --- a/.idea/file-manager.iml +++ b/.idea/file-manager.iml @@ -3,6 +3,11 @@ + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 709bdbb..7f4ef1e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,8 @@ + + @@ -14,6 +16,14 @@ + + + + + + + + @@ -66,4 +76,19 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/common.py b/common.py index 80dea97..52cd595 100644 --- a/common.py +++ b/common.py @@ -47,7 +47,6 @@ def find_end_of_exercise_number_from_directory(directory): return os.path.basename(directory).split('_')[1] - def main_file_name(directory, prefix=''): res = os.path.basename(directory) pos = find_end_of_prefix(res) + 1 @@ -66,6 +65,7 @@ def renamer(base_dir, prefix, problem_name=None): directory = base_dir + '/' + directory if os.path.isdir(directory): if 'common' not in os.path.basename(directory): + # TODO: consider a more elegant solution using the split() method for file in os.listdir(directory): prefix_end = find_end_of_prefix(file) + 1 new_file_name = directory + '/' + prefix + '_' + file[prefix_end:] @@ -190,11 +190,11 @@ def file_creation(base_dir, prefix, problem_name): pass -methods_dict = {'renamer': renamer, 'import_adjustment': imp_adjustment, 'file_creation': file_creation} +METHODS_DICT = {'renamer': renamer, 'import_adjustment': imp_adjustment, 'file_creation': file_creation} def general_function_handler(method, base_dir, prefix, problem_name): - methods_dict[method](base_dir, prefix, problem_name) + METHODS_DICT[method](base_dir, prefix, problem_name) if __name__ == '__main__': diff --git a/main.py b/main.py index 1fe26b3..6c4ff0a 100644 --- a/main.py +++ b/main.py @@ -25,6 +25,7 @@ for c in range(4): label_and_entry_frame.columnconfigure(index=c, weight=1) for r in range(6): label_and_entry_frame.rowconfigure(index=r, weight=1) label_and_entry_frame.columnconfigure(index=1, weight=2) +label_and_entry_frame.rowconfigure(index=4, minsize=16) # # Preset frame # preset_frame = tk.Frame(main_body_frame) # preset_frame.grid(row=1, column=0, sticky='nsew') @@ -85,7 +86,7 @@ def save_preset(): if not os.path.isdir('./preset'): os.makedirs('./preset') # TODO: consider the case where the name is empty - with open(param_dict['Preset name'], 'w') as file: + with open('./preset/' + param_dict['preset name'] + '.txt', 'w') as file: for key, value in param_dict.items(): if not key == 'Preset name': file.write(key + ': ' + value + '\n') @@ -100,7 +101,7 @@ def choose_preset(): for key in ENTRY_OBJECT_DICT.keys(): param_dict[key] = None with open(filename, 'r') as file: - param_dict['Preset name'] = os.path.basename(file.name) + param_dict['preset name'] = os.path.basename(file.name) for line in file: if line.strip() == '': continue From 4e98af4bdfafbf695f7a69dd61643eed8f1b9191 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Mon, 23 Oct 2023 02:53:41 +0500 Subject: [PATCH 12/12] Made the 'save preset' button trim newline characters when saving presets --- .idea/workspace.xml | 4 +--- main.py | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7f4ef1e..a8d5688 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,7 @@ - - - diff --git a/main.py b/main.py index 6c4ff0a..ff64ecd 100644 --- a/main.py +++ b/main.py @@ -88,7 +88,7 @@ def save_preset(): # TODO: consider the case where the name is empty with open('./preset/' + param_dict['preset name'] + '.txt', 'w') as file: for key, value in param_dict.items(): - if not key == 'Preset name': + if not key == 'preset name': file.write(key + ': ' + value + '\n') save_preset_button = tk.Button(label_and_entry_frame, text='save preset', command=save_preset) @@ -106,7 +106,7 @@ def choose_preset(): if line.strip() == '': continue key, value = line.split(': ', 1) - param_dict[key] = value + param_dict[key] = value.replace('\n', '') for key, value in param_dict.items(): if value is not None: ENTRY_OBJECT_DICT[key].delete(0, tk.END)