-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
*~ | ||
*.swp | ||
|
||
venv/ | ||
*.bak | ||
__pycache__/ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters