Skip to content

Commit

Permalink
Add config utility
Browse files Browse the repository at this point in the history
  • Loading branch information
kantegory committed Jul 21, 2020
1 parent 8a37790 commit 8cd9838
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 48 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*~
*.swp

venv/
*.bak
__pycache__/
*.pyc
23 changes: 23 additions & 0 deletions categories.ini
Original file line number Diff line number Diff line change
@@ -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
118 changes: 118 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -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()
75 changes: 28 additions & 47 deletions file-sorter.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -85,20 +65,21 @@ 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()

sorter = FileSorter()

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__":
Expand Down

0 comments on commit 8cd9838

Please sign in to comment.