postrider 0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
postrider-0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 HorsemanWSGI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ recursive-include src *.py *.txt
2
+ recursive-include docs *.txt
postrider-0.1/PKG-INFO ADDED
@@ -0,0 +1,45 @@
1
+ Metadata-Version: 2.4
2
+ Name: postrider
3
+ Version: 0.1
4
+ Summary: Mailbox management for batched or delayed email delivery
5
+ Author-email: Souheil Chelfouh <trollfot@gmail.com>
6
+ Maintainer-email: Souheil Chelfouh <trollfot@gmail.com>
7
+ License: MIT
8
+ Project-URL: Source Code, https://github.com/HorsemanWSGI/postrider
9
+ Classifier: Environment :: Web Environment
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Topic :: Communications :: Email
15
+ Requires-Python: >=3.9
16
+ Description-Content-Type: text/x-rst
17
+ License-File: LICENSE
18
+ Requires-Dist: dynaconf
19
+ Requires-Dist: minicli
20
+ Provides-Extra: test
21
+ Requires-Dist: pytest; extra == "test"
22
+ Dynamic: license-file
23
+
24
+ postrider
25
+ *********
26
+
27
+ Mailbox-based e-mail delivery/queuing system.
28
+
29
+
30
+ Running fake server
31
+ -------------------
32
+
33
+ $ sudo python -m smtpd -n -c DebuggingServer localhost:25
34
+
35
+
36
+ Starting the worker
37
+ -------------------
38
+
39
+ $ postrider sender post.toml
40
+
41
+
42
+ Adding a test mail
43
+ ------------------
44
+
45
+ $ postrider testmail post.toml BOXNAME
@@ -0,0 +1,22 @@
1
+ postrider
2
+ *********
3
+
4
+ Mailbox-based e-mail delivery/queuing system.
5
+
6
+
7
+ Running fake server
8
+ -------------------
9
+
10
+ $ sudo python -m smtpd -n -c DebuggingServer localhost:25
11
+
12
+
13
+ Starting the worker
14
+ -------------------
15
+
16
+ $ postrider sender post.toml
17
+
18
+
19
+ Adding a test mail
20
+ ------------------
21
+
22
+ $ postrider testmail post.toml BOXNAME
@@ -0,0 +1 @@
1
+ Souheil Chelfouh
@@ -0,0 +1,30 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "postrider"
7
+ version = "0.1"
8
+ license = {text = "MIT"}
9
+ authors = [{name = "Souheil Chelfouh", email = "trollfot@gmail.com"}]
10
+ maintainers = [{name = "Souheil Chelfouh", email = "trollfot@gmail.com"}]
11
+ classifiers = [
12
+ "Environment :: Web Environment", "Intended Audience :: Developers",
13
+ "License :: OSI Approved :: MIT License",
14
+ "Operating System :: OS Independent",
15
+ "Programming Language :: Python",
16
+ "Topic :: Communications :: Email"
17
+ ]
18
+ description = "Mailbox management for batched or delayed email delivery"
19
+ readme = {file = "README.rst", content-type = "text/x-rst"}
20
+ requires-python = ">=3.9"
21
+ dependencies = ["dynaconf", "minicli"]
22
+
23
+ [project.urls]
24
+ "Source Code" = "https://github.com/HorsemanWSGI/postrider"
25
+
26
+ [project.optional-dependencies]
27
+ test = ["pytest"]
28
+
29
+ [project.scripts]
30
+ postrider = "postrider:clirunner"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
postrider-0.1/setup.py ADDED
@@ -0,0 +1,3 @@
1
+ from setuptools import setup
2
+
3
+ setup()
@@ -0,0 +1,111 @@
1
+ import logging
2
+ import typing as t
3
+ from io import IOBase
4
+ from mailbox import Mailbox, Maildir
5
+ from pathlib import Path
6
+ from email.utils import make_msgid
7
+ from email.mime.text import MIMEText
8
+ from email.mime.multipart import MIMEMultipart
9
+ from email.mime.application import MIMEApplication
10
+ from minicli import cli, run
11
+ from .mailer import SMTPConfiguration, Courrier
12
+ from .queue import ProcessorThread
13
+
14
+
15
+ def create_message(
16
+ origin: str,
17
+ targets: str,
18
+ subject: str,
19
+ text: str,
20
+ html: str = None,
21
+ files: list[Path | str | IOBase] = None,
22
+ ):
23
+ msg = MIMEMultipart("alternative")
24
+ msg["From"] = origin
25
+ msg["To"] = ",".join(targets)
26
+ msg["Subject"] = subject
27
+ msg.set_charset("utf-8")
28
+
29
+ part1 = MIMEText(text, "plain")
30
+ part1.set_charset("utf-8")
31
+ msg.attach(part1)
32
+
33
+ if html is not None:
34
+ part2 = MIMEText(html, "html")
35
+ part2.set_charset("utf-8")
36
+ msg.attach(part2)
37
+
38
+ if files:
39
+ for name, f in files.items():
40
+ if isinstance(f, str):
41
+ with open(f, "rb") as fd:
42
+ part = MIMEApplication(fd.read(), Name=name)
43
+ elif isinstance(f, Path):
44
+ with f.open("rb") as fd:
45
+ part = MIMEApplication(fd.read(), Name=name)
46
+ elif isinstance(f, IOBase):
47
+ part = MIMEApplication(f.read(), Name=name)
48
+ part["Content-Disposition"] = f'attachment; filename="{name}"'
49
+ msg.attach(part)
50
+
51
+ return msg
52
+
53
+
54
+ def configure_logging(settings, debug: bool = False):
55
+ import sys
56
+ import logging
57
+
58
+ log_level = logging.DEBUG if debug else settings.logging.level
59
+ stream_handler = logging.StreamHandler(stream=sys.stdout)
60
+ stream_handler.setLevel(log_level)
61
+ logging.basicConfig(
62
+ level=log_level,
63
+ format="%(asctime)s %(levelname)s %(module)s %(message)s",
64
+ handlers=[stream_handler],
65
+ )
66
+ logger = logging.getLogger("postrider")
67
+ return logger
68
+
69
+
70
+ @cli
71
+ def sender(config: Path, forever: bool = True, debug: bool = False):
72
+ from dynaconf import Dynaconf
73
+
74
+ settings = Dynaconf(settings_files=[config])
75
+ logger = configure_logging(settings, debug=debug)
76
+ logger.info("Start Mail Sender")
77
+ mailer = Courrier(SMTPConfiguration(**settings.smtp))
78
+
79
+ workers = {}
80
+ for name, conf in settings.box.items():
81
+ path = Path(conf.path).resolve()
82
+ assert path not in workers
83
+ mailbox = Maildir(path)
84
+ interval = settings.worker[name].get("interval", 5.0)
85
+ workers[path] = ProcessorThread(mailer, mailbox, interval)
86
+
87
+ for name, worker in workers.items():
88
+ worker.start()
89
+
90
+ for name, worker in workers.items():
91
+ worker.join()
92
+
93
+
94
+ @cli
95
+ def testmail(config: Path, boxname: str):
96
+ from dynaconf import Dynaconf
97
+
98
+ settings = Dynaconf(settings_files=[config])
99
+ mailbox = Maildir(settings.box[boxname].path)
100
+ msg = create_message(
101
+ "test@test.org", ["trollfot@gmail.com"], "This is a subject", "This is my email"
102
+ )
103
+ mailbox.add(msg)
104
+
105
+
106
+ def clirunner():
107
+ run()
108
+
109
+
110
+ if __name__ == "__main__":
111
+ run()
@@ -0,0 +1,43 @@
1
+ import typing as t
2
+ import smtplib
3
+ import logging
4
+ from contextlib import contextmanager
5
+
6
+
7
+ class SMTPConfiguration(t.NamedTuple):
8
+ port: int = 25
9
+ host: str = "localhost"
10
+ user: str = None
11
+ password: str = None
12
+ debug: bool = False
13
+
14
+
15
+ class Courrier:
16
+
17
+ def __init__(self, config: SMTPConfiguration):
18
+ self.config = config
19
+
20
+ def connect(self):
21
+ server = smtplib.SMTP(self.config.host, str(self.config.port))
22
+ server.set_debuglevel(self.config.debug)
23
+ code, response = server.ehlo()
24
+ if code < 200 or code >= 300:
25
+ raise RuntimeError(
26
+ 'Error sending EHLO to the SMTP server '
27
+ f'(code={code}, response={response})'
28
+ )
29
+ # If we can encrypt this session, do it
30
+ if server.has_extn("STARTTLS"):
31
+ server.starttls()
32
+ server.ehlo() # re-identify ourselves over TLS connection
33
+ if self.config.user:
34
+ server.login(self.config.user, self.config.password)
35
+ return server
36
+
37
+ @contextmanager
38
+ def sender(self):
39
+ server = self.connect()
40
+ try:
41
+ yield server.send_message
42
+ finally:
43
+ server.close()
@@ -0,0 +1,51 @@
1
+ import time
2
+ import logging
3
+ import threading
4
+
5
+
6
+ logger = logging.getLogger(__name__)
7
+
8
+
9
+ class ProcessorThread(threading.Thread):
10
+
11
+ _running: bool = False
12
+
13
+ def __init__(self, mailer, mailbox, interval: float):
14
+ super().__init__(name=__name__)
15
+ self.mailer = mailer
16
+ self.mailbox = mailbox
17
+ self.interval = interval
18
+ self.daemon = True
19
+
20
+ def stop(self):
21
+ if self._running:
22
+ self._running = False
23
+
24
+ def salvo(self):
25
+ self.mailbox.lock()
26
+ try:
27
+ with self.mailer.sender() as sender:
28
+ for key, message in self.mailbox.iteritems():
29
+ if not self._running:
30
+ logger.warning('Processor was stopped.')
31
+ raise StopIteration('Thread has stopped running')
32
+ logger.debug(f'Sending {key}.')
33
+ sender(message)
34
+ logger.debug(f'Discarding {key}.')
35
+ self.mailbox.discard(key)
36
+ finally:
37
+ self.mailbox.close()
38
+
39
+ def run(self, forever: bool = True):
40
+ self._running = True
41
+ self.salvo()
42
+ if forever:
43
+ time.sleep(self.interval)
44
+ while self._running:
45
+ try:
46
+ self.salvo()
47
+ logger.debug(f'Sleeping for {self.interval}.')
48
+ time.sleep(self.interval)
49
+ except:
50
+ self._running = False
51
+ raise
@@ -0,0 +1,45 @@
1
+ Metadata-Version: 2.4
2
+ Name: postrider
3
+ Version: 0.1
4
+ Summary: Mailbox management for batched or delayed email delivery
5
+ Author-email: Souheil Chelfouh <trollfot@gmail.com>
6
+ Maintainer-email: Souheil Chelfouh <trollfot@gmail.com>
7
+ License: MIT
8
+ Project-URL: Source Code, https://github.com/HorsemanWSGI/postrider
9
+ Classifier: Environment :: Web Environment
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Topic :: Communications :: Email
15
+ Requires-Python: >=3.9
16
+ Description-Content-Type: text/x-rst
17
+ License-File: LICENSE
18
+ Requires-Dist: dynaconf
19
+ Requires-Dist: minicli
20
+ Provides-Extra: test
21
+ Requires-Dist: pytest; extra == "test"
22
+ Dynamic: license-file
23
+
24
+ postrider
25
+ *********
26
+
27
+ Mailbox-based e-mail delivery/queuing system.
28
+
29
+
30
+ Running fake server
31
+ -------------------
32
+
33
+ $ sudo python -m smtpd -n -c DebuggingServer localhost:25
34
+
35
+
36
+ Starting the worker
37
+ -------------------
38
+
39
+ $ postrider sender post.toml
40
+
41
+
42
+ Adding a test mail
43
+ ------------------
44
+
45
+ $ postrider testmail post.toml BOXNAME
@@ -0,0 +1,15 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.rst
4
+ pyproject.toml
5
+ setup.py
6
+ docs/CREDITS.txt
7
+ src/postrider/__init__.py
8
+ src/postrider/mailer.py
9
+ src/postrider/queue.py
10
+ src/postrider.egg-info/PKG-INFO
11
+ src/postrider.egg-info/SOURCES.txt
12
+ src/postrider.egg-info/dependency_links.txt
13
+ src/postrider.egg-info/entry_points.txt
14
+ src/postrider.egg-info/requires.txt
15
+ src/postrider.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ postrider = postrider:clirunner
@@ -0,0 +1,5 @@
1
+ dynaconf
2
+ minicli
3
+
4
+ [test]
5
+ pytest
@@ -0,0 +1 @@
1
+ postrider