automate: move working_dir to be set in constructor

This commit is contained in:
Iain Learmonth 2022-11-28 18:55:10 +00:00
parent efdaad977a
commit 109851745b
7 changed files with 59 additions and 43 deletions

View file

@ -1,27 +1,28 @@
from abc import ABCMeta, abstractmethod
import os
from typing import Tuple, Optional, Any
from typing import Tuple, Any, Optional
import jinja2
from app import app
class BaseAutomation(metaclass=ABCMeta):
class BaseAutomation():
short_name: str = "base"
description: str = "Abstract base automation."
frequency: int
working_dir: Optional[str]
"""
The short name of the automation provider. This is used as an opaque token throughout
the portal system.
"""
@abstractmethod
def automate(self, working_dir: str, full: bool = False) -> Tuple[bool, str]:
def __init__(self, working_dir: Optional[str] = None):
super().__init__()
self.working_dir = working_dir
def automate(self, full: bool = False) -> Tuple[bool, str]:
raise NotImplementedError()
def tmpl_write(self, filename: str, template: str, working_dir: str, **kwargs: Any) -> None:
def tmpl_write(self, filename: str, template: str, **kwargs: Any) -> None:
"""
Write a Jinja2 template to the working directory for use by an automation module.
@ -31,6 +32,8 @@ class BaseAutomation(metaclass=ABCMeta):
:param kwargs: variables for use with the template
:return: None
"""
if not self.working_dir:
raise RuntimeError("No working directory specified.")
tmpl = jinja2.Template(template)
with open(os.path.join(working_dir, filename), 'w', encoding="utf-8") as tfconf:
with open(os.path.join(self.working_dir, filename), 'w', encoding="utf-8") as tfconf:
tfconf.write(tmpl.render(**kwargs))