rt-send-all-reminders: Make template search directory configurable.

This commit is contained in:
Brett Smith 2018-05-15 09:07:23 -04:00
parent e55397d0f6
commit 7282ba1199

View file

@ -16,10 +16,8 @@ SHARE_DIR = pathlib.Path(_data_home, 'rt-auto-remind')
SHARE_DIR.mkdir(parents=True, exist_ok=True) SHARE_DIR.mkdir(parents=True, exist_ok=True)
class Reminder: class Reminder:
def __init__(self, key, min_days, max_days, search, def __init__(self, key, min_days, max_days, search, body_file,
date_field='Due', body_file=None, action='correspond'): date_field='Due', action='correspond'):
if body_file is None:
body_file = pathlib.Path(SHARE_DIR, 'templates', key + '.txt')
self.key = key self.key = key
self.min_days_diff = int(min_days) self.min_days_diff = int(min_days)
self.max_days_diff = int(max_days) self.max_days_diff = int(max_days)
@ -42,6 +40,13 @@ class Reminder:
def parse_arguments(arglist): def parse_arguments(arglist):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument(
'--template-dir',
type=pathlib.Path,
default=SHARE_DIR / 'templates',
help="Directory to search for templates that aren't specified in the"
" YAML file (default `%(default)s`)",
)
parser.add_argument( parser.add_argument(
'yaml_files', metavar='PATH', 'yaml_files', metavar='PATH',
type=pathlib.Path, type=pathlib.Path,
@ -56,8 +61,10 @@ def main(arglist=None, stdout=sys.stdout, stderr=sys.stderr):
for yaml_path in args.yaml_files: for yaml_path in args.yaml_files:
with yaml_path.open() as yaml_file: with yaml_path.open() as yaml_file:
yaml_data = yaml.safe_load(yaml_file) yaml_data = yaml.safe_load(yaml_file)
for key in yaml_data: for key, reminder_kwargs in yaml_data.items():
reminder = Reminder(key, **yaml_data[key]) if 'body_file' not in reminder_kwargs:
reminder_kwargs['body_file'] = args.template_dir / (key + '.txt')
reminder = Reminder(key, **reminder_kwargs)
try: try:
subprocess.run(reminder.remind_cmd(), check=True) subprocess.run(reminder.remind_cmd(), check=True)
except subprocess.CalledProcessError as error: except subprocess.CalledProcessError as error: