Skip to content

Commit

Permalink
send2trash complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerem Kayacan committed Apr 20, 2019
1 parent 1617c56 commit 50a20e9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

8 changes: 8 additions & 0 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"action": "send2trash",
"folder": "D:\\home\\tmp",
"parameter": "st_atime",
"days": 40
}
]
37 changes: 25 additions & 12 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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()
config = get_config()
for action in config:
if action["action"] == "send2trash":
remove(action["folder"], action["parameter"], action["days"])

0 comments on commit 50a20e9

Please sign in to comment.