diff --git a/.gitignore b/.gitignore index 94418d5..4b2738c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ venv/ *.bak __pycache__/ *.pyc +*.egg-info +build/ +dist/ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..7402276 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include LICENSE +include file_sorter/categories.ini diff --git a/README.md b/README.md index 0035a3b..0bcfca6 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,17 @@ $ file-sorter --directory /path/to/your/directory Show all categories and extensions: ```bash -$ file-sorter --show +$ file-sorter-config --show +``` + +Edit pattern name: + +```bash +$ file-sorter-config --edit-name pattern_name new_name +``` + +Edit extensions for pattern name: + +```bash +$ file-sorter-config --edit-ext pattern_name ``` diff --git a/file_sorter/__init__.py b/file_sorter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/categories.ini b/file_sorter/categories.ini similarity index 100% rename from categories.ini rename to file_sorter/categories.ini diff --git a/config.py b/file_sorter/config.py similarity index 67% rename from config.py rename to file_sorter/config.py index cc6edc2..b5bafb0 100644 --- a/config.py +++ b/file_sorter/config.py @@ -15,6 +15,8 @@ def read_config(self, filename=None): if filename: self.filename = filename + print(self.filename) + self.config.read(self.filename) def get_categories(self): @@ -22,7 +24,7 @@ def get_categories(self): for category in self.categories: self.extensions[category] = self.config[category]['extensions'] - + return self.extensions def get_path_patterns(self): @@ -39,9 +41,7 @@ def print_path_patterns(self): 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) + self.add_path_pattern(pattern_name) # copy all items from old section self.config._sections[new_name] = self.config._sections[pattern_name] @@ -57,6 +57,13 @@ def edit_path_pattern_extensions(self, pattern_name, extensions): self.rewrite_config() + def add_path_pattern(self, pattern_name): + # add new section + if pattern_name not in self.categories: + self.config.add_section(pattern_name) + + self.rewrite_config() + def rewrite_config(self): # rewrite config f = open(self.filename, 'w') @@ -66,13 +73,41 @@ def rewrite_config(self): # re-read config self.read_config() + +def edit_extensions(config, pattern_name): + 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() + + 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') + help='Edit category 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') + help='Edit category extensions,\n usage: file-sorter-config --edit-ext category_name') + parser.add_argument('--add', action='store', nargs=1,\ + help='Add new category,\n usage: file-sorter-config --add category_name') + args = parser.parse_args() @@ -92,27 +127,14 @@ def main(): 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) + edit_extensions(config, pattern_name) - extensions = '|'.join(extensions) + if args.add: + pattern_name = args.add[0] - config.edit_path_pattern_extensions(pattern_name, extensions) + config.add_path_pattern(pattern_name) + edit_extensions(config, pattern_name) - # 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/file_sorter.py similarity index 98% rename from file-sorter.py rename to file_sorter/file_sorter.py index ea77a55..22ee99c 100644 --- a/file-sorter.py +++ b/file_sorter/file_sorter.py @@ -1,6 +1,6 @@ import os from argparse import ArgumentParser as ap -from config import Config +from file_sorter.config import Config import re diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e0a64cd --- /dev/null +++ b/setup.py @@ -0,0 +1,38 @@ +import os +from setuptools import setup + +classifiers_list = [ + "Development Status :: 4 - Beta", + "Environment :: Plugins", + "Operating System :: Ubuntu", + "Programming Language :: Python 3.6.9", + "Topic :: System", + "Topic :: Utilities", + { + "alpha": "Development Status :: 3 - Alpha", + "beta": "Development Status :: 4 - Beta", + "stable": "Development Status :: 5 - Production/Stable" + }["alpha"] +] + +README = os.path.join(os.path.dirname(__file__), "README.md") + +setup( + name="file-sorter", + version="0.1.0", + description="File sorter is a simple tool for automation sorting your files by their extensions.", + long_description=open(README).read(), + author="Dobryakov David", + author_email="kantegory@etersoft.ru", + url="https://github.com/kantegory/file_sorter", + license="ISC", + packages=['file_sorter'], + entry_points={ + "console_scripts": [ + "file-sorter = file_sorter.file_sorter:main", + "file-sorter-config = file_sorter.config:main", + ] + }, + classifiers=classifiers_list, + include_package_data=True +)