Compare commits

...

2 Commits

Author SHA1 Message Date
Ceda EI c00ef3df9f Add email module 2024-04-09 00:58:29 +05:30
Ceda EI 87c4f906b9 Add support for custom variables 2024-04-09 00:35:04 +05:30
2 changed files with 60 additions and 3 deletions

View File

@ -1,5 +1,7 @@
import sys
from typing import Optional
from importlib import import_module
from pathlib import Path
from typing import Any, Optional
import click
from rich.console import Console
@ -15,12 +17,25 @@ error_console = Console(stderr=True, style="red bold")
@click.option(
"-c", "--config", type=click.Path(exists=True, dir_okay=False, readable=True)
)
@click.option(
"-v", "--vars-file", type=click.Path(exists=True, dir_okay=False, readable=True)
)
@click.pass_context
def inni(ctx: click.Context, config: Optional[str]):
def inni(ctx: click.Context, config: Optional[str], vars_file: Optional[str]):
ctx.ensure_object(dict)
if config is None:
config = str(default_config())
ctx.obj["config"] = read_config(config)
if vars_file is not None:
vars_module: Path = Path(vars_file)
else:
vars_module: Path = default_config().parent / "vars.py"
if vars_module.exists():
module_dir = vars_module.parent
sys.path.append(str(module_dir))
ctx.obj["vars"] = import_module(vars_module.name.removesuffix(".py"))
sys.path.remove(str(module_dir))
def _login_out(ctx: click.Context, login: bool):
@ -47,8 +62,11 @@ def _login_out(ctx: click.Context, login: bool):
variables_to_prompt.update(
module.template_variables()["login" if login else "logout"]
)
variables_to_prompt.remove("vars")
responses = {}
responses: dict[str, Any] = {
"vars": ctx.obj.get("vars", {}),
}
for var in variables_to_prompt:
prompt = prompts.get(var, f"{var}:").strip() + " "
responses[var] = console.input("[blue]" + prompt)

39
inni/modules/email.py Normal file
View File

@ -0,0 +1,39 @@
import smtplib
from email.mime.text import MIMEText
from inni.modules.base import BaseModule
from inni.template import render_template
class Module(BaseModule):
login_template_keys = ("login_subject", "login_body")
logout_template_keys = ("logout_subject", "logout_body")
def setUp(self):
if self.config["ssl"]:
self.mailer = smtplib.SMTP_SSL(self.config["host"], self.config["port"])
else:
self.mailer = smtplib.SMTP(self.config["host"], self.config["port"])
def send_mail(self, sub, body):
msg = MIMEText(body)
msg["Subject"] = sub
from_ = msg["From"] = self.config.get("from", self.config["username"])
to = msg["To"] = ", ".join(self.config["to"])
with self.mailer as smtp:
smtp.login(self.config["username"], self.config["password"])
smtp.sendmail(from_, to, msg.as_string())
def login(self, responses):
body = render_template(self.config["login_body"], **responses)
subject = render_template(self.config["login_subject"], **responses)
with self.out.status("[green]Sending Email"):
self.send_mail(subject, body)
self.out.print("[green]✅ Email sent")
def logout(self, responses):
body = render_template(self.config["logout_body"], **responses)
subject = render_template(self.config["logout_subject"], **responses)
with self.out.status("[green]Sending Email"):
self.send_mail(subject, body)
self.out.print("[green]✅ Email sent")