From 9e22d4c5f55d2356481887e93a93196adb55c25e Mon Sep 17 00:00:00 2001 From: cnsworder Date: Tue, 21 Feb 2017 10:58:43 +0800 Subject: [PATCH 1/6] Add config file to define formats --- classifier/classifier.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/classifier/classifier.py b/classifier/classifier.py index 1bb6292..09093c7 100644 --- a/classifier/classifier.py +++ b/classifier/classifier.py @@ -4,6 +4,7 @@ import os import six import sys +import yaml from six.moves import getcwd @@ -92,20 +93,34 @@ 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', + if args.config: + conf_file_name = os.path.expanduser(args.config) + else: + conf_file_name = os.getenv("HOME") + "/.config/.classify.conf" + + if os.path.exists(conf_file_name): + with open(conf_file_name, "r") as conf_file: + formats = yaml.load(conf_file) + else: + 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'] - } + 'Books': ['.mobi', '.epub', '.chm'], + 'DEBPackages': ['.deb'], + 'Programs': ['.exe', '.msi'], + 'RPMPackages': ['.rpm'] + } + with open(conf_file_name, 'w') as conf_file: + yaml.safe_dump(formats, conf_file) if bool(args.specific_folder) ^ bool(args.specific_types): print( @@ -126,7 +141,7 @@ 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 From a708351e323e7617ed88f1b88fb704b879384bdd Mon Sep 17 00:00:00 2001 From: cnsworder Date: Tue, 21 Feb 2017 11:01:33 +0800 Subject: [PATCH 2/6] Modify setup.py install_requirest pyyaml --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 999736e..6b6cd1f 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,7 @@ """, install_requires=[ 'arrow', + 'pyyaml', 'six>=1.10.0', ], zip_safe=False From d99efc8a482da5c120ddadb781594d55ea9cddb3 Mon Sep 17 00:00:00 2001 From: cnsworder Date: Tue, 21 Feb 2017 12:34:14 +0800 Subject: [PATCH 3/6] Remove check isfile --- classifier/classifier.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/classifier/classifier.py b/classifier/classifier.py index 09093c7..135034c 100644 --- a/classifier/classifier.py +++ b/classifier/classifier.py @@ -22,11 +22,9 @@ 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): From fda7ddb9f8c653bb84609421ff67cf64d0cd2461 Mon Sep 17 00:00:00 2001 From: cnsworder Date: Mon, 20 Mar 2017 22:42:08 +0800 Subject: [PATCH 4/6] Modify: exception yaml.YAMLError --- classifier/classifier.py | 52 +++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/classifier/classifier.py b/classifier/classifier.py index 135034c..4371299 100644 --- a/classifier/classifier.py +++ b/classifier/classifier.py @@ -17,6 +17,18 @@ 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) @@ -72,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) @@ -99,26 +129,14 @@ def main(): if args.config: conf_file_name = os.path.expanduser(args.config) else: - conf_file_name = os.getenv("HOME") + "/.config/.classify.conf" + conf_file_name = os.getenv("HOME") + "/.config/classifier" if os.path.exists(conf_file_name): - with open(conf_file_name, "r") as conf_file: - formats = yaml.load(conf_file) + formats = _load_config(conf_file_name) else: - 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'] - } - with open(conf_file_name, 'w') as conf_file: - yaml.safe_dump(formats, conf_file) + formats = default_formats + + _save_config(conf_file_name, formats) if bool(args.specific_folder) ^ bool(args.specific_types): print( From 26db13c10b382775bfdb68714c91a0a7c857e7b4 Mon Sep 17 00:00:00 2001 From: cnsworder Date: Mon, 20 Mar 2017 23:07:26 +0800 Subject: [PATCH 5/6] Add chinese language README --- README.md | 1 + README_zh.md | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 README_zh.md diff --git a/README.md b/README.md index 854cf24..e8a22c9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000..5f9b562 --- /dev/null +++ b/README_zh.md @@ -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` 设置的整理目录. 例如:
+`classifier -d /home/source'`
+在 `/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. From 3b0ad72445d3b09586e9c472f17c0ca0461d61fb Mon Sep 17 00:00:00 2001 From: cnsworder Date: Mon, 20 Mar 2017 23:10:40 +0800 Subject: [PATCH 6/6] Modify DD-MM-YYYY to YYYY-MM-DD --- classifier/classifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classifier/classifier.py b/classifier/classifier.py index 4371299..abc34ec 100644 --- a/classifier/classifier.py +++ b/classifier/classifier.py @@ -162,7 +162,7 @@ def main(): 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)