From 50a20e9c717e03acfe7d50693111c612233171b9 Mon Sep 17 00:00:00 2001 From: Kerem Kayacan Date: Sat, 20 Apr 2019 15:47:53 +0300 Subject: [PATCH] send2trash complete --- README.md | 31 ++++++++++++++++++++++++++++++- src/config.json | 8 ++++++++ src/main.py | 37 +++++++++++++++++++++++++------------ 3 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 src/config.json diff --git a/README.md b/README.md index 1cc70ff..f7423f8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ -# folder-actions +# Folder Actions Python CLI utility that triggers actions based on changes in watched folders + +This utility does not constantly watch folders. Once triggered, does its job and closes. It is up to user how to trigger. Using your operating system's functionalities, you can either schedule a periodic job, have it automatically triggered at system startup or even execute it manually if you like. + +## How to use +Folder Actions requires a configuration file named 'config.json' at same path with executable. Here is an example content for config.json: +``` +[ + { + "action": "send2trash", + "folder": "D:\\home\\tmp", + "parameter": "st_atime", + "days": 40 + } +] +``` +Main object is an array of objects which defines the action that Folder Actions will execute. + +## Configuration properties +action: Type of action that will be executed. This property is mandatory. Possible values: +* send2trash : Move files or folders to recycle bin/trash. This value requires other properties: folder, parameter, days + +folder: Directory to be watched. Use two backslashes for Windows paths as backslash (\\) is a special character. + +parameter: send2trash action will check some date from file metadata to decide the file is old enough. This property defines which date will be taken into consideration. Possible values: +* st_atime : Last access time +* st_mtime : Last modify time + +days: Action will execute if the file is older than given number of days. + diff --git a/src/config.json b/src/config.json new file mode 100644 index 0000000..a5592ed --- /dev/null +++ b/src/config.json @@ -0,0 +1,8 @@ +[ + { + "action": "send2trash", + "folder": "D:\\home\\tmp", + "parameter": "st_atime", + "days": 40 + } +] \ No newline at end of file diff --git a/src/main.py b/src/main.py index b854424..c526099 100644 --- a/src/main.py +++ b/src/main.py @@ -1,24 +1,37 @@ from datetime import datetime -from os import scandir +import os from send2trash import send2trash +import json + + +def get_config(): + with open(os.path.join(os.path.dirname(__file__), 'config.json'), 'r') as f: + datastore = json.load(f) + return datastore def convert_date(timestamp): d = datetime.utcfromtimestamp(timestamp) formated_date = d.strftime('%d.%m.%Y %H:%M:%S') return formated_date -def get_files(): - dir_entries = scandir('D:\\home\\tmp') +def remove(path, parameter, days): + dir_entries = os.scandir(path) for entry in dir_entries: info = entry.stat() - print(f'{entry.path}') - print(f'Last Modified: {convert_date(info.st_mtime)}') - print(f'Last Accessed: {convert_date(info.st_atime)}') - delta = (datetime.utcnow() - datetime.utcfromtimestamp(info.st_atime)).days - print(f'Days passed: {delta}') - if delta > 35: + #print(f'{entry.path}') + #print(f'Last Modified: {convert_date(info.st_mtime)}') + #print(f'Last Accessed: {convert_date(info.st_atime)}') + if parameter == "st_atime": + key_time = info.st_atime + elif parameter == "st_mtime": + key_time = info.st_mtime + delta = (datetime.utcnow() - datetime.utcfromtimestamp(key_time)).days + #print(f'Days passed: {delta}') + if delta > days: send2trash(entry.path) - print(f'DELETE') - print(f'') + #print(f'DELETE') -get_files() \ No newline at end of file +config = get_config() +for action in config: + if action["action"] == "send2trash": + remove(action["folder"], action["parameter"], action["days"]) \ No newline at end of file