72 lines
2.1 KiB
Python
Executable file
72 lines
2.1 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
try:
|
|
_data_home = os.environ['XDG_DATA_HOME']
|
|
except KeyError:
|
|
_data_home = pathlib.Path(os.path.expanduser('~'), '.local', 'share')
|
|
SHARE_DIR = pathlib.Path(_data_home, 'rt-auto-remind')
|
|
SHARE_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
class Reminder:
|
|
def __init__(self, key, min_days, max_days, search,
|
|
date_field='Due', body_file=None, action='correspond'):
|
|
if body_file is None:
|
|
body_file = pathlib.Path(SHARE_DIR, 'templates', key + '.txt')
|
|
self.key = key
|
|
self.min_days_diff = int(min_days)
|
|
self.max_days_diff = int(max_days)
|
|
self.search = search
|
|
self.date_field = date_field
|
|
self.body_file = body_file
|
|
self.action = action
|
|
|
|
def remind_cmd(self):
|
|
return [
|
|
'rt-auto-remind', '--{}'.format(self.action),
|
|
'--key', self.key,
|
|
self.date_field,
|
|
str(self.min_days_diff),
|
|
str(self.max_days_diff),
|
|
self.search,
|
|
str(self.body_file),
|
|
]
|
|
|
|
|
|
def parse_arguments(arglist):
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
'yaml_files', metavar='PATH',
|
|
type=pathlib.Path,
|
|
nargs='+',
|
|
help="YAML file(s) with configuration of reminders to send out",
|
|
)
|
|
return parser.parse_args(arglist)
|
|
|
|
def main(arglist=None, stdout=sys.stdout, stderr=sys.stderr):
|
|
args = parse_arguments(arglist)
|
|
failures = 0
|
|
for yaml_path in args.yaml_files:
|
|
with yaml_path.open() as yaml_file:
|
|
yaml_data = yaml.safe_load(yaml_file)
|
|
for key in yaml_data:
|
|
reminder = Reminder(key, **yaml_data[key])
|
|
try:
|
|
subprocess.run(reminder.remind_cmd(), check=True)
|
|
except subprocess.CalledProcessError as error:
|
|
print("warning: reminder {} exited {}".format(key, error.returncode))
|
|
failures += 1
|
|
if failures == 0:
|
|
return 0
|
|
else:
|
|
return min(10 + failures, 99)
|
|
|
|
if __name__ == '__main__':
|
|
exit(main())
|