Skip to content

Commit

Permalink
Cleaning up some code and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
abkfenris committed Oct 1, 2017
1 parent 7454bfb commit 28f8da7
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ deploy:
NlRpcnc3RlAxbXdxNjl0T2tnY0VybFlCRytKcFdtUUNaWFpManpWRWhLZ25jaHhmSjFUM3V2RUtH
VUVKZUtZYUZtSUFCYlFwenp4UWZDNkRtbzRiM3BFNzNhSUhoVDNsOFRvZHBreHYrNWRXNDh5OVk9
true:
python: 3.5
python: 3.6
repo: abkfenris/email_to
tags: true
install: pip install -U tox-travis
Expand Down
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ Simplyify sending HTML emails
* Free software: MIT license
* Documentation: https://email-to.readthedocs.io.

Judgement rendered by:

.. image:: https://api.codacy.com/project/badge/Grade/7dddc6b7000349958d485080f3dda7c1
:target: https://www.codacy.com/app/abk/email_to?utm_source=github.com&utm_medium=referral&utm_content=abkfenris/email_to&utm_campaign=Badge_Grade
:alt: Codacy

.. image:: https://landscape.io/github/abkfenris/email_to/master/landscape.svg?style=flat
:target: https://landscape.io/github/abkfenris/email_to/master
:alt: Code Health

.. image:: https://codeclimate.com/github/abkfenris/email_to/badges/gpa.svg
:target: https://codeclimate.com/github/abkfenris/email_to
:alt: Code Climate

.. image:: https://scrutinizer-ci.com/g/abkfenris/email_to/badges/quality-score.png?b=master
:target: https://scrutinizer-ci.com/g/abkfenris/email_to/
:alt: scrutinizer

Features
--------
Expand Down
5 changes: 4 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.viewcode',
'sphinxcontrib.napoleon' #http://sphinxcontrib-napoleon.readthedocs.org/en/latest/
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down
1 change: 1 addition & 0 deletions email_to/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

"""Top-level package for Email To."""
from __future__ import absolute_import

from email_to.email_to import EmailServer, Message

Expand Down
71 changes: 51 additions & 20 deletions email_to/email_to.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

"""Main module."""

from __future__ import absolute_import
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
Expand All @@ -18,18 +18,15 @@ class Message(object):
an email, you can build a Message by adding lines of Markdown formatted
strings.
args:
body (str, bytes, iterable of strings): The body of the email.
It does not need to be complete, as you can add to it later.
style (str): CSS formatting to be applied to HTML formatted verion
of email.
server (EmailServer): An EmailServer instance allowing a message
to be sent directly from it's `send` method.
"""
def __init__(self, body=None, style=None, server=None):
"""
Args:
body (str, bytes, iterable of strings): The body of the email.
It does not need to be complete, as you can add to it later.
style (str): CSS formatting to be applied to HTML formatted verion
of email.
server (EmailServer): An EmailServer instance allowing a message
to be sent directly from it's `send` method.
"""
if body is None:
self.body = []
else:
Expand Down Expand Up @@ -57,7 +54,7 @@ def __str__(self):

def __repr__(self):
return '<Message: {0} and {1} more lines>'.format(
self.body[0], (len(self.body) - 1))
self.body[0], (len(self.body) - 1))

def mime(self):
""" Returns a MIMEMultipart message """
Expand All @@ -68,23 +65,33 @@ def mime(self):

return msg

def send(self, to, subject):
def send(self, send_to, subject):
""" Sends the formatted message to given recripient
args:
to (:obj:str, iterable of :obj:`str`): Email addresses to send to
send_to (:obj:`str`, iterable of :obj:`str`): Email addresses to
send to
subject (str): Subject line of email to send
"""
self.server.send_message(self, to, subject)
self.server.send_message(self, send_to, subject)


class EmailServer(object):
""" Connection to a specific email server
args:
url (str): URL for the SMTP server
port (int): SMTP port
email_address (str): Email address to log into server and send from
password (str): Password for email address on SMTP server
"""
def __init__(self, url, port, email, password):
self.url = url
self.port = port
self.email_address = email
self.password = password
self.server = None

def _login(self):
self.server = smtplib.SMTP(self.url, self.port)
Expand All @@ -94,22 +101,46 @@ def _login(self):
def _logout(self):
self.server.quit()

def quick_email(self, to, subject, body, style=None):
def quick_email(self, send_to, subject, body, style=None):
""" Compose and send an email in a single call
args:
send_to (str):
subject (str):
body (:obj:`str`, iterable of :obj:`str`): Markdown formatted
string or iterable of strings composing the message body
style (str): CSS formatting for the message
"""
message = Message(body, style=style)

self.send_message(message, to, subject)
self.send_message(message, send_to, subject)

def send_message(self, message, send_to, subject):
""" Send a precomposed Message
def send_message(self, message, to, subject):
args:
message (Message): Completed message to send
send_to (str): Email address to send the message to
subject (str): Subject of the email
"""
message = message.mime()

message['From'] = self.email_address
message['To'] = to
message['To'] = send_to

message['Subject'] = subject

self._login()
self.server.sendmail(self.email_address, to, message.as_string())
self.server.sendmail(self.email_address, send_to, message.as_string())
self._logout()

def message(self, body=None, style=None):
""" Returns a Message object
args:
body (str, bytes, iterable of strings): The body of the email.
It does not need to be complete, as you can add to it later.
style (str): CSS formatting to be applied to HTML formatted verion
of email.
"""
return Message(body=body, style=style, server=self)
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ flake8==2.6.0
tox==2.3.1
coverage==4.1
Sphinx==1.4.8
sphinxcontrib-napoleon
cryptography==1.7
PyYAML==3.11
pytest==2.9.2
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
requirements = [
'markdown',
'premailer'
# TODO: put package requirements here
# put package requirements here
]

setup_requirements = [
'pytest-runner',
# TODO(abkfenris): put setup requirements (distutils extensions, etc.) here
# put setup requirements (distutils extensions, etc.) here
]

test_requirements = [
'pytest',
# TODO: put package test requirements here
# put package test requirements here
]

setup(
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[tox]
envlist = py26, py27, py33, py34, py35, flake8
envlist = py26, py27, py33, py34, py35, py36, flake8

[travis]
python =
3.6: py36
3.5: py35
3.4: py34
3.3: py33
Expand Down
127 changes: 0 additions & 127 deletions travis_pypi_setup.py

This file was deleted.

0 comments on commit 28f8da7

Please sign in to comment.