introduce basic config file

This commit is contained in:
holger krekel 2023-12-08 18:29:05 +01:00
parent 652b9688d3
commit d8c8040f07
4 changed files with 88 additions and 1 deletions

View File

@ -4,9 +4,10 @@ build-backend = "setuptools.build_meta"
[project]
name = "chatmaild"
version = "0.1"
version = "0.2"
dependencies = [
"aiosmtpd",
"iniconfig",
]
[project.scripts]

View File

@ -0,0 +1,26 @@
from fnmatch import fnmatch
import iniconfig
class Config:
def __init__(self, mailname, section):
self.mailname = mailname
self.privacy_postal = section.get("privacy_postal")
self.privacy_mail = section.get("privacy_mail")
self.privacy_pdo = section.get("privacy_pdo")
self.privacy_supervisor = section.get("privacy_supervisor")
self.has_privacy_policy = self.privacy_mail != None
def read_config(inipath, mailname):
ini = iniconfig.IniConfig(inipath)
privacy = None
for section in ini:
if section.name.startswith("privacy:"):
domain = section["domain"]
if fnmatch(mailname, domain):
privacy = section
break
return Config(mailname, privacy or {})

View File

@ -0,0 +1,48 @@
from chatmaild.config import read_config
def test_read_config_no_privacy_policy(tmp_path, create_ini):
inipath = create_ini(
"""
[privacy:testrun]
domain = *.example.org
"""
)
config = read_config(inipath, "something.example.org")
assert config.mailname == "something.example.org"
assert not config.has_privacy_policy
def test_read_config(create_ini):
inipath = create_ini(
"""
[privacy:testrun]
domain = *.testrun.org
privacy_postal =
Postal Ltd
privacy_mail = privacy@merlinux.eu
privacy_pdo =
Postal PDO
You can contact him at *delta-privacy@merlinux.eu* (Keyword: DPO)
privacy_supervisor =
line1
line2 with space
"""
)
config = read_config(inipath, "something.testrun.org")
assert config.has_privacy_policy
assert config.mailname == "something.testrun.org"
assert config.privacy_postal == "Postal Ltd"
assert config.privacy_mail == "privacy@merlinux.eu"
lines = config.privacy_pdo.split("\n")
assert lines[0] == "Postal PDO"
assert lines[1].startswith("You can ")
lines = config.privacy_supervisor.split("\n")
assert lines[0] == "line1"
assert lines[1] == "line2 with space"

View File

@ -3,6 +3,7 @@ import io
import time
import random
import subprocess
import textwrap
import imaplib
import smtplib
import itertools
@ -402,3 +403,14 @@ class CMUser:
imap.login(self.addr, self.password)
self._imap = imap
return self._imap
@pytest.fixture
def create_ini(tmp_path):
def create_ini_func(source):
p = tmp_path.joinpath("chatmail.ini")
assert not p.exists(), p
p.write_text(textwrap.dedent(source))
return p
return create_ini_func