Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configure file used yaml #72

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Downloads
-sf --specific-folder Folder to move files with Specific Type
-d --directory The directory whose files you want to classify
-o --output Main directory to put organized folders
-c --config Configure file, Default is `~/.config/classifier`

###Example
######Classify specific file types
Expand Down
135 changes: 135 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Classifier
整理当前文件夹内的文件,把它们分类到 music, pdfs, images 等目录中。

## 安装
```sh
$ pip install classifier
```
#### 平台兼容性
* Python 2.7 / Python 3.4
* Linux / OSX / Windows


## 使用说明
* 到需要整理分类的目录中自行下面的指令
```sh
$ classifier
```
```sh
>> Scanning Files
>> Done!
```

## 示例
### 整理前:
```
Downloads
│   ├── project.docx
│   ├── 21 Guns.mp3
│   ├── Sultans of Swing.mp3
│   ├── report.pdf
│   ├── charts.pdf
│   ├── VacationPic.png
│   ├── CKEditor.zip
│   ├── Cats.jpg
│   └── archive.7z
```

###整理后:
```
Downloads
│   ├── Music
│   │   ├── 21 Guns.mp3
│   │   └── Sultans of Swing.mp3
| |
│   ├── Documents
│   │   ├── project.docx
│   │   ├── report.pdf
│   │   └── charts.pdf
| |
│   ├── Archives
│   │   ├── CKEditor.zip
│   │   └── archive.7z
| |
│   ├── Pictures
│   │   ├── VacationPic.png
│   │   └── Cats.jpg
```


##选项
`classifier [-dt] [-st SPECIFIC_TYPES [SPECIFIC_TYPES ...]] [-sf SPECIFIC_FOLDER] [-o OUTPUT]`

-h --help 显示帮助
-dt --date 按日期归类文件
-st --specific-types 将某个扩展名的文件归类
-sf --specific-folder 归类到的目录
-d --directory 需要整理的目录
-o --output 输出目录
-c --config 配置文件,默认是 `~/.config/classifier`

### 示例
###### 整理文件的扩展名以空格分割
`classifier -st .py .pyc -sf "Python Files"`

### 整理前:
```
Workspace
│   ├── views.py
│   ├── temp.pyc
│   ├── game.java
│   ├── index.html
│   └── script.py
```


###整理后:
```
Workspace
│   ├── Python Files
│   │   ├── views.py
│   │   ├── temp.pyc
| | └── script.py
| |
| ├── game.java
| └── index.html

```

### 示例
###### 按日期整理:
`classifier -dt`

### 示例
###### 将 `/home/source` 目录下的文件分类整理到 `/home/dest` 目录:
`classifier -d /home/source -o /home/dest`

`Note: ` 如果只是通过 `-d` 设置了需要整理的目录,没有通过 `-o` 设置输出目录, 输出目录会使用 `-d` 设置的整理目录. 例如:<br>
`classifier -d /home/source'` <br>
在 `/home/source` 下进行归类整理.



======

## The MIT License
> Copyright (c) 2015 Bhrigu Srivastava http://bhrigu123.github.io

> Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

> The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
69 changes: 50 additions & 19 deletions classifier/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import six
import sys
import yaml

from six.moves import getcwd

Expand All @@ -16,16 +17,26 @@
Documents - https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions
"""

default_formats = {
'Music' : ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff', '.wav', '.amr'],
'Videos': ['.flv', '.ogv', '.avi', '.mp4', '.mpg', '.mpeg', '.3gp', '.mkv', '.ts', '.webm', '.vob', '.wmv'],
'Pictures': ['.png', '.jpeg', '.gif', '.jpg', '.bmp', '.svg', '.webp', '.psd', '.tiff'],
'Archives': ['.rar', '.zip', '.7z', '.gz', '.bz2', '.tar', '.dmg', '.tgz', '.xz', '.iso', '.cpio'],
'Documents': ['.txt', '.pdf', '.doc', '.docx','.odf', '.xls', '.xlsv', '.xlsx',
'.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'],
'Books': ['.mobi', '.epub', '.chm'],
'DEBPackages': ['.deb'],
'Programs': ['.exe', '.msi'],
'RPMPackages': ['.rpm']
}

def moveto(file, from_folder, to_folder):
from_file = os.path.join(from_folder, file)
to_file = os.path.join(to_folder, file)

# to move only files, not folders
if os.path.isfile(from_file):
if not os.path.exists(to_folder):
os.makedirs(to_folder)
os.rename(from_file, to_file)
if not os.path.exists(to_folder):
os.makedirs(to_folder)
os.rename(from_file, to_file)


def classify(formats, output, directory):
Expand Down Expand Up @@ -73,6 +84,24 @@ def _format_arg(arg):
return arg


def _load_config(conf_file_name):
with open(conf_file_name, "r") as conf_file:
try:
formats = yaml.load(conf_file)
except yaml.YAMLError as exc:
print("Parser error, used default config")
return default_formats
return formats


def _save_config(conf_file_name, formats):
try:
with open(conf_file_name, 'w') as conf_file:
yaml.safe_dump(formats, conf_file)
except yaml.YAMLError as exc:
print("Save config exception.")


def main():
description = "Organize files in your directory instantly,by classifying them into different folders"
parser = argparse.ArgumentParser(description=description)
Expand All @@ -92,20 +121,22 @@ def main():
parser.add_argument("-dt", "--date", action='store_true',
help="Organize files by creation date")

parser.add_argument("-c", "--config", type=str,
help="Config file")

args = parser.parse_args()

formats = {
'Music' : ['.mp3', '.aac', '.flac', '.ogg', '.wma', '.m4a', '.aiff', '.wav', '.amr'],
'Videos': ['.flv', '.ogv', '.avi', '.mp4', '.mpg', '.mpeg', '.3gp', '.mkv', '.ts', '.webm', '.vob', '.wmv'],
'Pictures': ['.png', '.jpeg', '.gif', '.jpg', '.bmp', '.svg', '.webp', '.psd', '.tiff'],
'Archives': ['.rar', '.zip', '.7z', '.gz', '.bz2', '.tar', '.dmg', '.tgz', '.xz', '.iso', '.cpio'],
'Documents': ['.txt', '.pdf', '.doc', '.docx','.odf', '.xls', '.xlsv', '.xlsx',
'.ppt', '.pptx', '.ppsx', '.odp', '.odt', '.ods', '.md', '.json', '.csv'],
'Books': ['.mobi', '.epub', '.chm'],
'DEBPackages': ['.deb'],
'Programs': ['.exe', '.msi'],
'RPMPackages': ['.rpm']
}
if args.config:
conf_file_name = os.path.expanduser(args.config)
else:
conf_file_name = os.getenv("HOME") + "/.config/classifier"

if os.path.exists(conf_file_name):
formats = _load_config(conf_file_name)
else:
formats = default_formats

_save_config(conf_file_name, formats)

if bool(args.specific_folder) ^ bool(args.specific_types):
print(
Expand All @@ -126,12 +157,12 @@ def main():
else:
directory = _format_arg(args.directory)
if args.output is None:
''' if -d arg given without the -o arg, keeping the files of -d
''' if -d arg given without the -o arg, keeping the files of -d
in the -d path only after classifying '''
output = directory

if args.date:
classify_by_date('DD-MM-YYYY', output, directory)
classify_by_date('YYYY-MM-DD', output, directory)
else:
classify(formats, output, directory)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
""",
install_requires=[
'arrow',
'pyyaml',
'six>=1.10.0',
],
zip_safe=False
Expand Down