Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP8 Fixes and String Formatting Enhancement #697

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/helpers/stats/stats_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def build_subuser_stats():
# subuser_stats.add_subuser(Subuser("bar"))
return subuser_stats.get()


def build_subuser_stats_sums():
subuser_stats = SubuserStats()
subuser_stats.start_date = '2017-10-15'
Expand Down Expand Up @@ -92,6 +93,7 @@ def get_subuser_stats_sums():
print(response.headers)
pprint_json(response.body)


get_global_stats()
get_category_stats()
get_category_stats_sums()
Expand Down
2 changes: 1 addition & 1 deletion register.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
'''
final_text = readme_rst.replace(replace, replacement)
with open('./README.txt', 'w', encoding='utf-8') as f:
f.write(final_text)
f.write(final_text)
7 changes: 4 additions & 3 deletions sendgrid/helpers/inbound/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, **opts):
self.path = opts.get(
'path', os.path.abspath(os.path.dirname(__file__))
)
with open(self.path + '/config.yml') as stream:
with open('{0}/config.yml'.format(self.path)) as stream:
config = yaml.load(stream)
self._debug_mode = config['debug_mode']
self._endpoint = config['endpoint']
Expand All @@ -28,8 +28,9 @@ def init_environment():
"""Allow variables assigned in .env available using
os.environ.get('VAR_NAME')"""
base_path = os.path.abspath(os.path.dirname(__file__))
if os.path.exists(base_path + '/.env'):
with open(base_path + '/.env') as f:
env_path = '{0}/.env'.format(base_path)
if os.path.exists(env_path):
with open(env_path) as f:
lines = f.readlines()
for line in lines:
var = line.strip().split('=')
Expand Down
4 changes: 3 additions & 1 deletion sendgrid/helpers/inbound/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def url(self):
"""URL to send to."""
return self._url


def main():
config = Config()
parser = argparse.ArgumentParser(description='Test data and optional host.')
Expand All @@ -54,5 +55,6 @@ def main():
print(response.headers)
print(response.body)


if __name__ == '__main__':
main()
main()
1 change: 1 addition & 0 deletions sendgrid/helpers/mail/content.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .validators import ValidateAPIKey


class Content(object):
"""Content to be included in your email.

Expand Down
2 changes: 1 addition & 1 deletion sendgrid/helpers/mail/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Various types of extensible SendGrid related exceptions
################################################################


class SendGridException(Exception):
"""Wrapper/default SendGrid-related exception"""
pass
Expand All @@ -19,4 +20,3 @@ def __init__(self,
message="SendGrid API Key detected"):
self.expression = expression
self.message = message

4 changes: 1 addition & 3 deletions sendgrid/helpers/mail/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Various types of Validators
################################################################


class ValidateAPIKey(object):
"""Validates content to ensure SendGrid API key is not present"""

Expand All @@ -27,7 +28,6 @@ def __init__(self, regex_strings=None, use_default=True):
default_regex_string = 'SG\.[0-9a-zA-Z]+\.[0-9a-zA-Z]+'
self.regexes.add(re.compile(default_regex_string))


def validate_message_dict(self, request_body):
"""With the JSON dict that will be sent to SendGrid's API,
check the content for SendGrid API keys - throw exception if found
Expand All @@ -54,7 +54,6 @@ def validate_message_dict(self, request_body):
message_text = content.get("value", "")
self.validate_message_text(message_text)


def validate_message_text(self, message_string):
"""With a message string, check to see if it contains a SendGrid API Key
If a key is found, throw an exception
Expand All @@ -68,4 +67,3 @@ def validate_message_text(self, message_string):
for regex in self.regexes:
if regex.match(message_string) is not None:
raise APIKeyIncludedException()

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def getRequires():
deps.append('unittest2py3k')
return deps


setup(
name='sendgrid',
version=str(__version__),
Expand Down
2 changes: 1 addition & 1 deletion test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def test_up_and_running(self):
def test_used_port_true(self):
if self.config.debug_mode:
port = int(os.environ.get("PORT", self.config.port))
self.assertEqual(port, self.config.port)
self.assertEqual(port, self.config.port)
2 changes: 1 addition & 1 deletion test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_initialization(self):

def test_init_environment(self):
config_file = sendgrid.helpers.inbound.config.__file__
env_file_path = os.path.abspath(os.path.dirname(config_file)) + '/.env'
env_file_path = '{0}/.env'.format(os.path.abspath(os.path.dirname(config_file)))
with open(env_file_path, 'w') as f:
f.write('RANDOM_VARIABLE=RANDOM_VALUE')
Config()
Expand Down
6 changes: 3 additions & 3 deletions test/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_sendgridAPIKey(self):
personalization.add_to(Email("test@example.com"))
mail.add_personalization(personalization)

#Try to include SendGrid API key
# Try to include SendGrid API key
try:
mail.add_content(Content("text/plain", "some SG.2123b1B.1212lBaC here"))
mail.add_content(
Expand All @@ -72,11 +72,11 @@ def test_sendgridAPIKey(self):
'"subject": "Hello World from the SendGrid Python Library"}'
)

#Exception should be thrown
# Exception should be thrown
except Exception as e:
pass

#Exception not thrown
# Exception not thrown
else:
self.fail("Should have failed as SendGrid API key included")

Expand Down
2 changes: 2 additions & 0 deletions test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
except ImportError:
import unittest


class ProjectTests(unittest.TestCase):

# ./docker
Expand Down Expand Up @@ -71,5 +72,6 @@ def test_usage(self):
def test_use_cases(self):
self.assertTrue(os.path.isfile('./use_cases/README.md'))


if __name__ == '__main__':
unittest.main()
15 changes: 10 additions & 5 deletions test/test_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ def test_send(self):
x = send.Send(fake_url)
x.test_payload(fake_url)

send.Client.assert_called_once_with(host=fake_url, request_headers={'User-Agent': 'SendGrid-Test',
'Content-Type': 'multipart/form-data; boundary=xYzZY'})
send.Client.assert_called_once_with(host=fake_url, request_headers={
'User-Agent': 'SendGrid-Test',
'Content-Type': 'multipart/form-data; boundary=xYzZY'
})

def test_main_call(self):
fake_url = 'https://fake_url'

with mock.patch('argparse.ArgumentParser.parse_args', return_value=argparse.Namespace(host=fake_url, data='test_file.txt')):
with mock.patch('argparse.ArgumentParser.parse_args', return_value=argparse.Namespace(
host=fake_url, data='test_file.txt')):
send.main()
send.Client.assert_called_once_with(host=fake_url, request_headers={'User-Agent': 'SendGrid-Test',
'Content-Type': 'multipart/form-data; boundary=xYzZY'})
send.Client.assert_called_once_with(host=fake_url, request_headers={
'User-Agent': 'SendGrid-Test',
'Content-Type': 'multipart/form-data; boundary=xYzZY'
})
7 changes: 5 additions & 2 deletions test/test_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ def test_hello_world(self):
content = Content(
"text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
self.assertTrue(mail.get() == {'content': [{'type': 'text/plain', 'value': 'and easy to do anywhere, even with Python'}], 'personalizations': [
{'to': [{'email': 'test@example.com'}]}], 'from': {'email': 'test@example.com'}, 'subject': 'Sending with SendGrid is Fun'})
self.assertTrue(mail.get() == {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.assertEqual(mail.get, {...})  # ?

Copy link
Contributor Author

@vkmrishad vkmrishad Oct 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@42B
I have updated file with master branch. This file was changed when merged with others PR.
May be this PR was at the time of github outage period.

'content': [{'type': 'text/plain', 'value': 'and easy to do anywhere, even with Python'}],
'personalizations': [{'to': [{'email': 'test@example.com'}]}], 'from': {'email': 'test@example.com'},
'subject': 'Sending with SendGrid is Fun'
})

def test_access_settings_activity_get(self):
params = {'limit': 1}
Expand Down
4 changes: 3 additions & 1 deletion test/test_unassigned.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
} ]
'''


def get_all_ip():
ret_val = json.loads(ret_json)
return ret_val
Expand All @@ -67,7 +68,6 @@ def make_data():
return data



def test_unassigned_ip_json():

data = make_data()
Expand All @@ -79,6 +79,7 @@ def test_unassigned_ip_json():
for item in calculated:
assert item["ip"] in data


def test_unassigned_ip_obj():

data = make_data()
Expand All @@ -89,6 +90,7 @@ def test_unassigned_ip_obj():
for item in calculated:
assert item["ip"] in data


def test_unassigned_baddata():
as_json = False
calculated = unassigned(dict(), as_json=as_json)
Expand Down