Compare commits

..

No commits in common. "c00ef3df9f653a644f702db5cfcf0db6e58f15fe" and "f87b7661ee79884e3697e6107d78521de56d4799" have entirely different histories.

2 changed files with 3 additions and 60 deletions

View File

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

View File

@ -1,39 +0,0 @@
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")