cmd/do_tasks.py

62 lines
1.6 KiB
Python
Raw Normal View History

2019-06-25 22:32:03 +00:00
'''
Execute the contents of all .task files forever.
'''
2019-12-10 21:02:22 +00:00
import argparse
2019-06-25 22:32:03 +00:00
import os
2019-12-10 21:02:22 +00:00
import send2trash
2019-06-25 22:32:03 +00:00
import sys
import time
2020-11-27 02:19:10 +00:00
from voussoirkit import backoff
bo = backoff.Linear(m=1, b=5, max=300)
2019-06-25 22:32:03 +00:00
2019-12-10 21:02:22 +00:00
def get_task_files():
return [f for f in os.listdir() if (os.path.isfile(f) and f.endswith('.task'))]
def do_tasks(task_files):
for task_file in task_files:
if not os.path.exists(task_file):
continue
with open(task_file, 'r', encoding='utf-8') as handle:
task_content = handle.read()
task_content = task_content.strip()
print('TASK:', task_content)
status = os.system(task_content)
if status == 0:
send2trash.send2trash(task_file)
def do_tasks_forever():
2019-06-25 22:32:03 +00:00
while True:
2019-12-10 21:02:22 +00:00
print(time.strftime('%H:%M:%S'), 'Looking for tasks.')
task_files = get_task_files()
2020-11-27 02:19:10 +00:00
if task_files:
bo.reset()
2019-12-10 21:02:22 +00:00
do_tasks(task_files)
try:
2020-11-27 02:19:10 +00:00
time.sleep(bo.next())
2019-12-10 21:02:22 +00:00
except KeyboardInterrupt:
2019-06-25 22:32:03 +00:00
break
2019-12-10 21:02:22 +00:00
def do_tasks_argparse(args):
if args.task_files:
return do_tasks(args.task_files)
if args.only_once:
return do_tasks(get_task_files())
return do_tasks_forever()
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('task_files', nargs='*', default=None)
parser.add_argument('--once', dest='only_once', action='store_true')
parser.set_defaults(func=do_tasks_argparse)
args = parser.parse_args(argv)
return args.func(args)
2019-06-25 22:32:03 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))