From 8cd983866c0c7828e0129b6e4bb1fabe792e2d5c Mon Sep 17 00:00:00 2001 From: kantegory Date: Tue, 21 Jul 2020 20:52:45 +0300 Subject: [PATCH] Add config utility --- .gitignore | 5 ++- categories.ini | 23 ++++++++++ config.py | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ file-sorter.py | 75 ++++++++++++------------------- 4 files changed, 173 insertions(+), 48 deletions(-) create mode 100644 categories.ini create mode 100644 config.py diff --git a/.gitignore b/.gitignore index 70bb2a2..94418d5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ *~ *.swp - +venv/ +*.bak +__pycache__/ +*.pyc diff --git a/categories.ini b/categories.ini new file mode 100644 index 0000000..9eed3a1 --- /dev/null +++ b/categories.ini @@ -0,0 +1,23 @@ +[VIDEOS] +extensions = mp4|MP4|m4a|M4A|m4v|M4V|f4v|F4V|webm|WEBM|f4a|F4A|m4b|M4B|m4r|M4R|f4b|F4B|mov|MOV|3gp|3GP|3g2|3G2|3gpp|3GPP|3gpp2|3GPP2|wmv|WMV|wma|WMA|flv|FLV|avi|AVI + +[AUDIOS] +extensions = mp3|MP3|wav|WAV|flac|FLAC|aac|AAC + +[DOCUMENTS] +extensions = pdf|PDF|doc|DOC|docx|DOCX|odt|ODT|rtf|RTF|htm|HTM|html|HTML|xml|XML + +[SPREADSHEETS] +extensions = xls|XLS|xlsx|XLSX|ods|ODS + +[PRESENTATIONS] +extensions = ppt|PPT|pptx|PPTX + +[TXT-FILES] +extensions = txt|TXT + +[ARCHIVES] +extensions = zip|ZIP|7z|7Z|rar|RAR|gz|GZ|bz2|BZ2|xz|XZ + +[PICTURES] +extensions = jpg|JPG|gif|GIF|png|PNG|svg|SVG|webp|WEBP|tiff|TIFF|psd|PSD|eps|EPS|ai|AI|indd|INDD|raw|RAW|heic|HEIC diff --git a/config.py b/config.py new file mode 100644 index 0000000..cc6edc2 --- /dev/null +++ b/config.py @@ -0,0 +1,118 @@ +import re +import configparser +import os +from argparse import ArgumentParser as ap + +class Config: + def __init__(self): + self.config = configparser.ConfigParser() + self.filename = '{}/categories.ini'.format(os.path.dirname(os.path.abspath(__file__))) + self.extensions = {} + self.categories = [] + self.path_patterns = {} + + def read_config(self, filename=None): + if filename: + self.filename = filename + + self.config.read(self.filename) + + def get_categories(self): + self.categories = self.config.sections() + + for category in self.categories: + self.extensions[category] = self.config[category]['extensions'] + + return self.extensions + + def get_path_patterns(self): + for category in self.categories: + self.path_patterns[category] = re.compile('^.*\.({})$'.format(self.extensions[category])) + + return self.path_patterns + + def print_path_patterns(self): + for pattern in self.path_patterns: + extensions = self.extensions[pattern].split('|') + extensions = ['.{}'.format(e) for e in extensions] + extensions = ', '.join(extensions) + print('Category {}, extensions: {}'.format(pattern, extensions)) + + def edit_path_pattern_name(self, pattern_name, new_name): + # add new section + if new_name not in self.categories: + self.config.add_section(new_name) + + # copy all items from old section + self.config._sections[new_name] = self.config._sections[pattern_name] + + # delete old section + self.config._sections.pop(pattern_name) + + self.rewrite_config() + + def edit_path_pattern_extensions(self, pattern_name, extensions): + # put new extensions to section + self.config._sections[pattern_name]['extensions'] = extensions + + self.rewrite_config() + + def rewrite_config(self): + # rewrite config + f = open(self.filename, 'w') + self.config.write(f) + f.close() + + # re-read config + self.read_config() + +def main(): + parser = ap(description='File-sorter config utility') + parser.add_argument('--show', action='store_true', help='Showing all path patterns') + parser.add_argument('--edit-name', action='store', nargs=2,\ + help='Edit pattern name,\n usage: file-sorter config --edit-name old_name new_name') + parser.add_argument('--edit-ext', action='store', nargs=1,\ + help='Edit pattern extensions,\n usage: file-sorter config --edit-ext category_name') + + args = parser.parse_args() + + config = Config() + config.read_config() + config.get_categories() + config.get_path_patterns() + + if args.show: + config.print_path_patterns() + + if args.edit_name: + pattern_name, new_name = args.edit_name + + config.edit_path_pattern_name(pattern_name, new_name) + + if args.edit_ext: + pattern_name = args.edit_ext[0] + + extensions = [] + + while True: + ext = input('Write new extension (without ".", like: "PDF") for {} (or Enter for quit):\n'.format(pattern_name)) + + if not ext: + break + + extensions.append(ext) + + extensions = '|'.join(extensions) + + config.edit_path_pattern_extensions(pattern_name, extensions) + + # re-read config + config.read_config() + config.get_categories() + config.get_path_patterns() + + print('New config is: \n') + config.print_path_patterns() + +if __name__ == '__main__': + main() diff --git a/file-sorter.py b/file-sorter.py index 032c1c1..ea77a55 100644 --- a/file-sorter.py +++ b/file-sorter.py @@ -1,58 +1,38 @@ import os -import re from argparse import ArgumentParser as ap +from config import Config +import re + class FileSorter: def __init__(self): - self.extensions = { - 'pictures': 'jpg|JPG|gif|GIF|png|PNG|svg|SVG|webp|WEBP|tiff|TIFF|psd|PSD|eps|EPS|ai|AI|indd|INDD|raw|RAW|heic|HEIC', - 'videos': 'mp4|MP4|m4a|M4A|m4v|M4V|f4v|F4V|webm|WEBM|f4a|F4A|m4b|M4B|m4r|M4R|f4b|F4B|mov|MOV|3gp|3GP|3g2|3G2|3gpp|3GPP|3gpp2|3GPP2|wmv|WMV|wma|WMA|flv|FLV|avi|AVI', - 'audios': 'mp3|MP3|wav|WAV|flac|FLAC|aac|AAC', - 'documents': 'pdf|PDF|doc|DOC|docx|DOCX|odt|ODT|rtf|RTF|htm|HTM|html|HTML|xml|XML', - 'spreadsheets': 'xls|XLS|xlsx|XLSX|ods|ODS', - 'presentations': 'ppt|PPT|pptx|PPTX', - 'txt-files': 'txt|TXT', - 'archives': 'zip|ZIP|7z|7Z|rar|RAR|gz|GZ|bz2|BZ2|xz|XZ' - } - - self.path_patterns = { - 'pictures': re.compile('^.*\.({})$'.format(self.extensions['pictures'])), - 'videos': re.compile('^.*\.({})$'.format(self.extensions['videos'])), - 'audios': re.compile('^.*\.({})$'.format(self.extensions['audios'])), - 'documents': re.compile('^.*\.({})$'.format(self.extensions['documents'])), - 'spreadsheets': re.compile('^.*\.({})$'.format(self.extensions['spreadsheets'])), - 'presentations': re.compile('^.*\.({})$'.format(self.extensions['presentations'])), - 'txt-files': re.compile('^.*\.({})$'.format(self.extensions['txt-files'])), - 'archives': re.compile('^.*\.({})$'.format(self.extensions['archives'])) - } - - self.categorized_files = { - 'pictures': [], - 'videos': [], - 'audios': [], - 'documents': [], - 'spreadsheets': [], - 'presentations': [], - 'txt-files': [], - 'archives': [] - } + self.config = Config() + self.config.read_config() + + self.extensions = {} + self.path_patterns = {} + self.categorized_files = {} self.working_directory = '' + def configure(self): + self.extensions = self.config.get_categories() + self.path_patterns = self.config.get_path_patterns() + + for category in self.extensions: + self.categorized_files[category] = [] + def set_working_directory(self, directory): self.working_directory = directory - def get_path_patterns(self): - print(self.path_patterns) - for pattern in self.path_patterns: - extensions = self.extensions[pattern].split('|') - extensions = ['.{}'.format(e) for e in extensions] - extensions = ', '.join(extensions) - print('Category {}, extensions: {}'.format(pattern, extensions)) - def get_all_files_from_dir(self): - all_files = [file for file in os.listdir(self.working_directory)\ - if os.path.isfile(os.path.join(self.working_directory, file))] + all_files = [] + + files_from_dir = os.listdir(self.working_directory) + + for file in files_from_dir: + if os.path.isfile(os.path.join(self.working_directory, file)): + all_files.append(file) return all_files @@ -85,7 +65,6 @@ def sort_files(self): def main(): parser = ap(description='Sort all of your files in any directory') parser.add_argument('--directory', action='store', dest='directory', type=str, help='The name of the directory in which to sort the files') - parser.add_argument('--show', action='store_true', help='Showing all path_patterns') args = parser.parse_args() @@ -93,12 +72,14 @@ def main(): if args.directory: sorter.set_working_directory(args.directory) + + # configure sorter + sorter.configure() + all_files = sorter.get_all_files_from_dir() categorized_files = sorter.categorize_files(all_files) - sorter.sort_files() - if args.show: - sorter.get_path_patterns() + sorter.sort_files() if __name__ == "__main__":