Skip to content

Commit f441483

Browse files
authored
Merge pull request #319 from itkovian/feat-vscmail-config
Add mail config file argument for VscMail
2 parents 9d06192 + a4b0156 commit f441483

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

lib/vsc/utils/mail.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import re
3636
import smtplib
3737
import ssl
38+
from configparser import ConfigParser
3839
from email.mime.multipart import MIMEMultipart
3940
from email.mime.text import MIMEText
4041
from email.mime.image import MIMEImage
@@ -71,16 +72,35 @@ class VscMail(object):
7172
def __init__(
7273
self,
7374
mail_host='',
74-
mail_port=0,
75+
mail_port=587,
7576
smtp_auth_user=None,
7677
smtp_auth_password=None,
77-
smtp_use_starttls=False):
78+
smtp_use_starttls=False,
79+
mail_config=None):
80+
"""
81+
- If there is a config file provided, its values take precedence over the arguments passed to __init__
82+
- If the mail_host is of the format host:port, that port takes precedence over mail_port
83+
"""
84+
85+
mail_options = ConfigParser()
86+
if mail_config:
87+
logging.info("Reading config file: %s", mail_config)
88+
with open(mail_config, "r") as mc:
89+
mail_options.read_file(mc)
90+
91+
# we can have cases where the host part is actually host:port
92+
_mail_host = mail_options.get("main", "mail_host", fallback=mail_host)
93+
try:
94+
self.mail_host, _mail_port = _mail_host.split(":")
95+
except ValueError:
96+
self.mail_host = _mail_host
97+
_mail_port = mail_options.get("main", "mail_port", fallback=mail_port)
98+
99+
self.mail_port = int(_mail_port)
100+
self.smtp_auth_user = mail_options.get("main", "smtp_auth_user", fallback=smtp_auth_user)
101+
self.smtp_auth_password = mail_options.get("main", "smtp_auth_password", fallback=smtp_auth_password)
102+
self.smtp_use_starttls = mail_options.get("main", "smtp_use_starttls", fallback=smtp_use_starttls)
78103

79-
self.mail_host = mail_host
80-
self.mail_port = mail_port
81-
self.smtp_auth_user = smtp_auth_user
82-
self.smtp_auth_password = smtp_auth_password
83-
self.smtp_use_starttls = smtp_use_starttls
84104

85105
def _connect(self):
86106
"""

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
import vsc.install.shared_setup as shared_setup
3737
from vsc.install.shared_setup import ag, kh, jt, sdw
3838

39-
VSC_INSTALL_REQ_VERSION = '0.15.2'
39+
VSC_INSTALL_REQ_VERSION = '0.17.19'
4040

4141
_coloredlogs_pkgs = [
4242
'coloredlogs < 6.0', # automatic log colorizer
4343
'humanfriendly', # detect if terminal has colors
4444
]
4545

4646
PACKAGE = {
47-
'version': '3.3.2',
47+
'version': '3.4.0',
4848
'author': [sdw, jt, ag, kh],
4949
'maintainer': [sdw, jt, ag, kh],
5050
# as long as 1.0.0 is not out, vsc-base should still provide vsc.fancylogger

test/data/mailconfig.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[main]
2+
mail_host = config_host
3+
mail_port = 789
4+
smtp_auth_user = config_user
5+
smtp_auth_password = config_passwd
6+
smtp_use_starttls = 1

test/mail.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
@author: Andy Georges (Ghent University)
3030
"""
3131
import mock
32+
import logging
3233
import os
3334

3435
from vsc.install.testing import TestCase
@@ -38,6 +39,35 @@
3839

3940
class TestVscMail(TestCase):
4041

42+
43+
def test_config_file(self):
44+
45+
mail_host = "mailhost.domain"
46+
mail_port = 123
47+
mail_host_port = "mailhost.domain:567"
48+
smtp_auth_user = "user"
49+
smtp_auth_password = "passwd"
50+
smtp_use_starttls = True
51+
52+
mail = VscMail(mail_host=mail_host)
53+
54+
self.assertEqual(mail.mail_host, mail_host)
55+
self.assertEqual(mail.mail_port, 587)
56+
57+
mail = VscMail(mail_host=mail_host, mail_port=mail_port)
58+
self.assertEqual(mail.mail_host, mail_host)
59+
self.assertEqual(mail.mail_port, mail_port)
60+
61+
mail = VscMail(mail_config=os.path.dirname(__file__) + '/data/' + 'mailconfig.ini')
62+
63+
logging.warning("mail.mail_host: %s", mail.mail_host)
64+
65+
self.assertEqual(mail.mail_host, "config_host")
66+
self.assertEqual(mail.mail_port, 789)
67+
self.assertEqual(mail.smtp_auth_user, "config_user")
68+
self.assertEqual(mail.smtp_auth_password, "config_passwd")
69+
self.assertEqual(mail.smtp_use_starttls, '1')
70+
4171
@mock.patch('vsc.utils.mail.smtplib')
4272
@mock.patch('vsc.utils.mail.ssl')
4373
def test_send(self, mock_ssl, mock_smtplib):
@@ -51,7 +81,7 @@ def test_send(self, mock_ssl, mock_smtplib):
5181
vm = VscMail()
5282

5383
self.assertEqual(vm.mail_host, '')
54-
self.assertEqual(vm.mail_port, 0)
84+
self.assertEqual(vm.mail_port, 587)
5585
self.assertEqual(vm.smtp_auth_user, None)
5686
self.assertEqual(vm.smtp_auth_password, None)
5787
self.assertEqual(vm.smtp_use_starttls, False)

0 commit comments

Comments
 (0)