Skip to content

Commit

Permalink
Blacken python source files
Browse files Browse the repository at this point in the history
Also ignore W503 from flake8.  It no longer agrees with PEP8.  See
these two links for details:
psf/black#21
python/peps@c59c437
  • Loading branch information
jsf9k committed Apr 15, 2019
1 parent fffd948 commit d5ed350
Show file tree
Hide file tree
Showing 17 changed files with 937 additions and 609 deletions.
7 changes: 6 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ select = C,E,F,W,B,B950
# Ignore flake8's default warning about maximum line length, which has
# a hard stop at the configured value. Instead we use
# flake8-bugbear's B950, which allows up to 10% overage.
ignore = E501
#
# Also ignore flake8's warning about line breaks before binary
# operators. It no longer agrees with PEP8. See, for example, here:
# https://github.com/ambv/black/issues/21. Guido agrees here:
# https://github.com/python/peps/commit/c59c4376ad233a62ca4b3a6060c81368bd21e85b.
ignore = E501,W503
42 changes: 30 additions & 12 deletions cyhy/mailer/CybexMessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class CybexMessage(ReportMessage):
"""

DefaultTo = ['ncats@hq.dhs.gov']
DefaultTo = ["ncats@hq.dhs.gov"]

Subject = 'Cyber Exposure Scorecard - {{report_date}} Results'
Subject = "Cyber Exposure Scorecard - {{report_date}} Results"

TextBody = '''Greetings,
TextBody = """Greetings,
The Cyber Exposure scorecard from {{report_date}} is attached for your review.
Expand All @@ -48,9 +48,9 @@ class CybexMessage(ReportMessage):
ncats@hq.dhs.gov
WARNING: This document is FOR OFFICIAL USE ONLY (FOUO). It contains information that may be exempt from public release under the Freedom of Information Act (5 U.S.G. 552). It is to be controlled, stored, handled, transmitted, distributed, and disposed of in accordance with DHS policy relating to FOUO information and is not to be released to the public or other personnel who do not have a valid 'need-to-know' without prior approval of an authorized DHS official.
'''
"""

HtmlBody = '''<html>
HtmlBody = """<html>
<head></head>
<body>
<p>Greetings,</p>
Expand All @@ -71,9 +71,20 @@ class CybexMessage(ReportMessage):
<p>WARNING: This document is FOR OFFICIAL USE ONLY (FOUO). It contains information that may be exempt from public release under the Freedom of Information Act (5 U.S.G. 552). It is to be controlled, stored, handled, transmitted, distributed, and disposed of in accordance with DHS policy relating to FOUO information and is not to be released to the public or other personnel who do not have a valid 'need-to-know' without prior approval of an authorized DHS official.</p>
</body>
</html>
'''

def __init__(self, pdf_filename, critical_open_csv_filename, critical_closed_csv_filename, high_open_csv_filename, high_closed_csv_filename, report_date, to_addrs=DefaultTo, from_addr=Message.DefaultFrom, cc_addrs=None):
"""

def __init__(
self,
pdf_filename,
critical_open_csv_filename,
critical_closed_csv_filename,
high_open_csv_filename,
high_closed_csv_filename,
report_date,
to_addrs=DefaultTo,
from_addr=Message.DefaultFrom,
cc_addrs=None,
):
"""Construct an instance.
Parameters
Expand Down Expand Up @@ -114,16 +125,23 @@ def __init__(self, pdf_filename, critical_open_csv_filename, critical_closed_csv
address to which this message should be sent.
"""
# This is the data mustache will use to render the templates
mustache_data = {
'report_date': report_date
}
mustache_data = {"report_date": report_date}

# Render the templates
subject = pystache.render(CybexMessage.Subject, mustache_data)
text_body = pystache.render(CybexMessage.TextBody, mustache_data)
html_body = pystache.render(CybexMessage.HtmlBody, mustache_data)

ReportMessage.__init__(self, to_addrs, subject, text_body, html_body, pdf_filename, from_addr, cc_addrs)
ReportMessage.__init__(
self,
to_addrs,
subject,
text_body,
html_body,
pdf_filename,
from_addr,
cc_addrs,
)

self.attach_csv(critical_open_csv_filename)
self.attach_csv(critical_closed_csv_filename)
Expand Down
38 changes: 26 additions & 12 deletions cyhy/mailer/CyhyMessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class CyhyMessage(ReportMessage):
message body.
"""

Subject = '{{acronym}} - Cyber Hygiene Report - {{report_date}} Results'
Subject = "{{acronym}} - Cyber Hygiene Report - {{report_date}} Results"

TextBody = '''Greetings {{acronym}},
TextBody = """Greetings {{acronym}},
The Cyber Hygiene scan results are attached for your review. Same password as before. (If this is your first report and you have yet to receive a password, please let us know!)
Expand All @@ -40,9 +40,9 @@ class CyhyMessage(ReportMessage):
ncats@hq.dhs.gov
WARNING: This document is FOR OFFICIAL USE ONLY (FOUO). It contains information that may be exempt from public release under the Freedom of Information Act (5 U.S.G. 552). It is to be controlled, stored, handled, transmitted, distributed, and disposed of in accordance with DHS policy relating to FOUO information and is not to be released to the public or other personnel who do not have a valid 'need-to-know' without prior approval of an authorized DHS official.
'''
"""

HtmlBody = '''<html>
HtmlBody = """<html>
<head></head>
<body>
<p>Greetings {{acronym}},</p>
Expand All @@ -62,9 +62,17 @@ class CyhyMessage(ReportMessage):
<p>WARNING: This document is FOR OFFICIAL USE ONLY (FOUO). It contains information that may be exempt from public release under the Freedom of Information Act (5 U.S.G. 552). It is to be controlled, stored, handled, transmitted, distributed, and disposed of in accordance with DHS policy relating to FOUO information and is not to be released to the public or other personnel who do not have a valid 'need-to-know' without prior approval of an authorized DHS official.</p>
</body>
</html>
'''

def __init__(self, to_addrs, pdf_filename, agency_acronym, report_date, from_addr=Message.DefaultFrom, cc_addrs=Message.DefaultCc):
"""

def __init__(
self,
to_addrs,
pdf_filename,
agency_acronym,
report_date,
from_addr=Message.DefaultFrom,
cc_addrs=Message.DefaultCc,
):
"""Construct an instance.
Parameters
Expand Down Expand Up @@ -93,14 +101,20 @@ def __init__(self, to_addrs, pdf_filename, agency_acronym, report_date, from_add
address to which this message should be sent.
"""
# This is the data mustache will use to render the templates
mustache_data = {
'acronym': agency_acronym,
'report_date': report_date
}
mustache_data = {"acronym": agency_acronym, "report_date": report_date}

# Render the templates
subject = pystache.render(CyhyMessage.Subject, mustache_data)
text_body = pystache.render(CyhyMessage.TextBody, mustache_data)
html_body = pystache.render(CyhyMessage.HtmlBody, mustache_data)

ReportMessage.__init__(self, to_addrs, subject, text_body, html_body, pdf_filename, from_addr, cc_addrs)
ReportMessage.__init__(
self,
to_addrs,
subject,
text_body,
html_body,
pdf_filename,
from_addr,
cc_addrs,
)
38 changes: 26 additions & 12 deletions cyhy/mailer/HttpsMessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class HttpsMessage(ReportMessage):
message body.
"""

Subject = '{{acronym}} - HTTPS Report - {{report_date}} Results'
Subject = "{{acronym}} - HTTPS Report - {{report_date}} Results"

TextBody = '''Greetings {{acronym}},
TextBody = """Greetings {{acronym}},
Attached is your latest HTTPS Report.
Expand Down Expand Up @@ -58,9 +58,9 @@ class HttpsMessage(ReportMessage):
* Fixed: A flaw in the report logic would sometimes cause the "Results" section of the PDF to inaccurately represent raw pshtt scores. This error would also represent domains that were "HSTS Preload Ready" or "HSTS Preload Pending" as preloaded (a checkmark).
* Added: The report will now represent domains with "bad chain" errors (but not hostname or expired certificate errors) that otherwise satisfy M-15-13 as compliant. This is in line with M-15-13 not requiring the use of a particular certificate authority
--------------------
'''
"""

HtmlBody = '''<html>
HtmlBody = """<html>
<head></head>
<body>
<div style=""font-size:14.5"">
Expand Down Expand Up @@ -92,9 +92,17 @@ class HttpsMessage(ReportMessage):
<p>--------------------</p>
</body>
</html>
'''

def __init__(self, to_addrs, pdf_filename, agency_acronym, report_date, from_addr=Message.DefaultFrom, cc_addrs=Message.DefaultCc):
"""

def __init__(
self,
to_addrs,
pdf_filename,
agency_acronym,
report_date,
from_addr=Message.DefaultFrom,
cc_addrs=Message.DefaultCc,
):
"""Construct an instance.
Parameters
Expand Down Expand Up @@ -124,14 +132,20 @@ def __init__(self, to_addrs, pdf_filename, agency_acronym, report_date, from_add
address to which this message should be sent.
"""
# This is the data mustache will use to render the templates
mustache_data = {
'acronym': agency_acronym,
'report_date': report_date
}
mustache_data = {"acronym": agency_acronym, "report_date": report_date}

# Render the templates
subject = pystache.render(HttpsMessage.Subject, mustache_data)
text_body = pystache.render(HttpsMessage.TextBody, mustache_data)
html_body = pystache.render(HttpsMessage.HtmlBody, mustache_data)

ReportMessage.__init__(self, to_addrs, subject, text_body, html_body, pdf_filename, from_addr, cc_addrs)
ReportMessage.__init__(
self,
to_addrs,
subject,
text_body,
html_body,
pdf_filename,
from_addr,
cc_addrs,
)
73 changes: 41 additions & 32 deletions cyhy/mailer/Message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ class Message(MIMEMultipart):
directed.
"""

DefaultFrom = 'reports@cyber.dhs.gov'

DefaultCc = ['ncats@hq.dhs.gov']

DefaultReplyTo = 'ncats@hq.dhs.gov'

def __init__(self, to_addrs, subject=None, text_body=None, html_body=None, from_addr=DefaultFrom, cc_addrs=DefaultCc, reply_to_addr=DefaultReplyTo):
DefaultFrom = "reports@cyber.dhs.gov"

DefaultCc = ["ncats@hq.dhs.gov"]

DefaultReplyTo = "ncats@hq.dhs.gov"

def __init__(
self,
to_addrs,
subject=None,
text_body=None,
html_body=None,
from_addr=DefaultFrom,
cc_addrs=DefaultCc,
reply_to_addr=DefaultReplyTo,
):
"""Construct an instance.
Parameters
Expand Down Expand Up @@ -58,25 +67,25 @@ def __init__(self, to_addrs, subject=None, text_body=None, html_body=None, from_
reply_to_addr : str
The email address to which replies should be sent.
"""
MIMEMultipart.__init__(self, 'mixed')
MIMEMultipart.__init__(self, "mixed")

self['From'] = from_addr
logging.debug('Message to be sent from: %s', self['From'])
self["From"] = from_addr
logging.debug("Message to be sent from: %s", self["From"])

self['To'] = ','.join(to_addrs)
logging.debug('Message to be sent to: %s', self['To'])
self["To"] = ",".join(to_addrs)
logging.debug("Message to be sent to: %s", self["To"])

if cc_addrs:
self['CC'] = ','.join(cc_addrs)
logging.debug('Message to be sent as CC to: %s', self['CC'])
self["CC"] = ",".join(cc_addrs)
logging.debug("Message to be sent as CC to: %s", self["CC"])

if reply_to_addr:
self['Reply-To'] = reply_to_addr
logging.debug('Replies to be sent to: %s', self['Reply-To'])
self["Reply-To"] = reply_to_addr
logging.debug("Replies to be sent to: %s", self["Reply-To"])

if subject:
self['Subject'] = subject
logging.debug('Message subject: %s', subject)
self["Subject"] = subject
logging.debug("Message subject: %s", subject)

if html_body or text_body:
self.attach_text_and_html_bodies(html_body, text_body)
Expand All @@ -95,20 +104,20 @@ def attach_text_and_html_bodies(self, html, text):
text : str
The plain text to attach.
"""
textBody = MIMEMultipart('alternative')
textBody = MIMEMultipart("alternative")

# The order is important here. This order makes the HTML version the
# default version that is displayed, as long as the client supports it.
if text:
textBody.attach(MIMEText(text, 'plain'))
logging.debug('Message plain-text body: %s', text)
textBody.attach(MIMEText(text, "plain"))
logging.debug("Message plain-text body: %s", text)

if html:
htmlPart = MIMEText(html, 'html')
htmlPart = MIMEText(html, "html")
# See https://en.wikipedia.org/wiki/MIME#Content-Disposition
htmlPart.add_header('Content-Disposition', 'inline')
htmlPart.add_header("Content-Disposition", "inline")
textBody.attach(htmlPart)
logging.debug('Message HTML body: %s', html)
logging.debug("Message HTML body: %s", html)

self.attach(textBody)

Expand All @@ -120,15 +129,15 @@ def attach_pdf(self, pdf_filename):
pdf_filename : str
The filename of the PDF file to attach.
"""
with open(pdf_filename, 'rb') as attachment:
part = MIMEApplication(attachment.read(), 'pdf')
with open(pdf_filename, "rb") as attachment:
part = MIMEApplication(attachment.read(), "pdf")

encoders.encode_base64(part)
# See https://en.wikipedia.org/wiki/MIME#Content-Disposition
_, filename = os.path.split(pdf_filename)
part.add_header('Content-Disposition', 'attachment', filename=filename)
part.add_header("Content-Disposition", "attachment", filename=filename)
self.attach(part)
logging.debug('Message PDF attachment: %s', pdf_filename)
logging.debug("Message PDF attachment: %s", pdf_filename)

def attach_csv(self, csv_filename):
"""Attach a CSV file to this message.
Expand All @@ -138,11 +147,11 @@ def attach_csv(self, csv_filename):
csv_filename : str
The filename of the CSV file to attach.
"""
with open(csv_filename, 'r') as attachment:
part = MIMEText(attachment.read(), 'csv')
with open(csv_filename, "r") as attachment:
part = MIMEText(attachment.read(), "csv")

# See https://en.wikipedia.org/wiki/MIME#Content-Disposition
_, filename = os.path.split(csv_filename)
part.add_header('Content-Disposition', 'attachment', filename=filename)
part.add_header("Content-Disposition", "attachment", filename=filename)
self.attach(part)
logging.debug('Message CSV attachment: %s', csv_filename)
logging.debug("Message CSV attachment: %s", csv_filename)
15 changes: 13 additions & 2 deletions cyhy/mailer/ReportMessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ class ReportMessage(Message):
"""A class representing an email message with a report PDF attachment.
"""

def __init__(self, to_addrs, subject, text_body, html_body, pdf_filename, from_addr=Message.DefaultFrom, cc_addrs=Message.DefaultCc):
def __init__(
self,
to_addrs,
subject,
text_body,
html_body,
pdf_filename,
from_addr=Message.DefaultFrom,
cc_addrs=Message.DefaultCc,
):
"""Construct an instance.
Parameters
Expand Down Expand Up @@ -34,6 +43,8 @@ def __init__(self, to_addrs, subject, text_body, html_body, pdf_filename, from_a
An array of string objects, each of which is a CC email
address to which this message should be sent.
"""
Message.__init__(self, to_addrs, subject, text_body, html_body, from_addr, cc_addrs)
Message.__init__(
self, to_addrs, subject, text_body, html_body, from_addr, cc_addrs
)

self.attach_pdf(pdf_filename)
Loading

0 comments on commit d5ed350

Please sign in to comment.