Skip to content

Commit

Permalink
Create package
Browse files Browse the repository at this point in the history
  • Loading branch information
kantegory committed Jul 21, 2020
1 parent 8cd9838 commit 832677b
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ venv/
*.bak
__pycache__/
*.pyc
*.egg-info
build/
dist/
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include LICENSE
include file_sorter/categories.ini
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Empty file added file_sorter/__init__.py
Empty file.
File renamed without changes.
70 changes: 46 additions & 24 deletions config.py → file_sorter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ def read_config(self, filename=None):
if filename:
self.filename = filename

print(self.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):
Expand All @@ -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]
Expand All @@ -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')
Expand All @@ -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()

Expand All @@ -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()
2 changes: 1 addition & 1 deletion file-sorter.py → file_sorter/file_sorter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from argparse import ArgumentParser as ap
from config import Config
from file_sorter.config import Config
import re


Expand Down
38 changes: 38 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
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
)

0 comments on commit 832677b

Please sign in to comment.