Skip to content

Commit c6a1f46

Browse files
committed
Initial checkin for final project
1 parent a0bdd2f commit c6a1f46

File tree

8 files changed

+247
-0
lines changed

8 files changed

+247
-0
lines changed

Students/CarolynEvans/final_project/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import yaml
2+
3+
class config():
4+
"""
5+
This class parses config.yaml into a dictionary and provides callability.
6+
"""
7+
_VALUES = dict()
8+
9+
def __init__(self):
10+
with open('config.yaml', 'r') as f:
11+
config = yaml.load(f)
12+
13+
self.flatten(config)
14+
15+
16+
def flatten(self, config):
17+
"""
18+
This method loads a dictionary using the data that was loaded from config.yaml in __init__.
19+
The yaml dictionary 'values' may potentially be another dictionary.
20+
This method is used recursively to explode all levels of the yaml into a flat dictionary.
21+
:param config: A dict containing configuration values.
22+
:return: There is no return value. This method is called during __init__.
23+
"""
24+
# TODO: Rewrite for loop as comprehension
25+
26+
for key, val in config.items():
27+
if type(val) is dict:
28+
self.flatten(val)
29+
else:
30+
self._VALUES[key]=val
31+
32+
33+
34+
def get(self, key):
35+
"""
36+
This method is provides a configuration value for the given 'key'.
37+
:param key: A string containing the 'key' of the config item in the config dict.
38+
:return: A string containing the value of the config item in the config dict.
39+
"""
40+
val = self._VALUES[key]
41+
return val
42+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
data_warehouse_config:
2+
dw_dbname: "rightsidedw"
3+
dw_user: "rouser"
4+
dw_password: "RedshiftRO1"
5+
dw_host: "rightside-dw.cwisduywvaeb.us-east-1.redshift.amazonaws.com"
6+
dw_port: "5439"
7+
#
8+
#data_warehouse_config:
9+
# dw_dbname: rightsidedw
10+
# dw_user: carolyn.evans
11+
# dw_password: PassW0rd
12+
# dw_host: localhost
13+
# dw_port: 5439
14+
15+
hummingbird_config:
16+
hummingbird_url: http://hummingbird.dizzyninja.co/
17+
18+
email_config:
19+
email_server: mail.notify-customer.com
20+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import psycopg2
2+
from psycopg2._psycopg import ProgrammingError
3+
from config import config
4+
5+
class data_warehouse(object):
6+
'''
7+
This class is used to get data from the Rightside data warehouse.
8+
The complete data warehouse documentation can be found at
9+
https://wiki.rightside.net/display/tnt/BI
10+
'''
11+
12+
def __init__(self):
13+
"""
14+
This method sets the data warehouse connection string from the config.
15+
"""
16+
cnf = config()
17+
dbname = cnf.get('dw_dbname')
18+
user = cnf.get('dw_user')
19+
password = cnf.get('dw_password')
20+
host = cnf.get('dw_host')
21+
port = cnf.get('dw_port')
22+
23+
self.dw_connection_string = \
24+
"dbname='{}' user='{}' password='{}' host='{}' port='{}'".format(dbname,user,password,host,port)
25+
26+
27+
def __call__(self, query):
28+
"""
29+
This method calls the data warehouse API.
30+
The complete data warehouse documentation can be found at
31+
https://wiki.rightside.net/display/tnt/BI
32+
:param query: A string containing a valid redshift data warehouse query.
33+
:return: This depends on the query.
34+
If the query returns records, as in a 'select' query, the records are returned.
35+
If the query does not return records, as in an 'update' query, a unicode string is returned.
36+
"""
37+
with psycopg2.connect(self.dw_connection_string) as conn:
38+
with conn.cursor() as curs:
39+
curs.execute(query)
40+
41+
try:
42+
records = curs.fetchall()
43+
return records
44+
except ProgrammingError as e:
45+
# This error occurs when there are no records to return,
46+
# for example, queries such as inserts, updates, and unloads.
47+
message = unicode(e)
48+
if message == u'no results to fetch':
49+
return message
50+
else:
51+
raise message
52+
53+
54+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import urllib2
2+
import json
3+
4+
from config import config
5+
6+
class hummingbird(object):
7+
"""
8+
This class is used to call the DizzyNinja Hummingbird Domain Recommendation API.
9+
The complete hummingbird API documentation can be found at
10+
https://wiki.rightside.net/display/tnt/API+Documentation
11+
"""
12+
13+
def __init__(self):
14+
"""
15+
This method sets the hummingbird url from the config.
16+
"""
17+
cnf = config()
18+
self.url = cnf.get('hummingbird_url')
19+
20+
21+
def __call__(self, command, **kwargs):
22+
"""
23+
This method calls the hummingbird API.
24+
The complete hummingbird API documentation can be found at
25+
https://wiki.rightside.net/display/tnt/API+Documentation
26+
:param command: A string containing a valid hummingbird command.
27+
:param kwargs: Depends on 'command'. See hummingbird documentation for complete list.
28+
:return: Returns JSON with results for the 'command'.
29+
"""
30+
31+
# build the query string parameter for the hummingbird API http request.
32+
query_string = "".join(['{}={},'.format(key,val) for key, val in kwargs.items()])
33+
34+
#add the 'command' and the query string to the url.
35+
url = '{}{}?{}'.format(self.url, command, query_string)
36+
37+
response = urllib2.urlopen(url).read()
38+
return json.loads(response)
39+
40+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# TODO: Get list of registered domain names from data warehouse
3+
# TODO: Get list of premium domains from data warehouse
4+
# TODO: Call word splitter on both lists and save each in a dict with
5+
# original domain name as key and split words as values.
6+
# TODO: Sort split words for each domain name.
7+
# TODO: Compare split words for registered domain to split words for premium domains.
8+
# TODO: Get registered owners contact info from data warehouse.
9+
# TODO: Write file that can be opened in excel.
10+
11+
12+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import smtplib
2+
from config import config
3+
4+
5+
def send_mail(email_address, subject, body=''):
6+
"""
7+
This function sends email notifications to the current user.
8+
9+
:param email_address: This is the email address of the current user that was obtained from the ldap call.
10+
:param subject: This parameter will be used as the subject line of the email.
11+
:param body: This parameter will be used as the body of the email.
12+
"""
13+
14+
# If email is None, the task is being run from the command line and no email is sent.
15+
if email_address is None:
16+
return
17+
18+
cnf = config()
19+
SERVER = cnf.get('email_server')
20+
21+
TO = [email_address]
22+
TEXT = 'Subject: %s\n\n%s' % (subject, body)
23+
24+
# Send the mail
25+
server = smtplib.SMTP(SERVER)
26+
server.sendmail(FROM, TO, TEXT)
27+
28+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
def test_config():
2+
# Test config.py
3+
from config import config
4+
5+
cnf = config()
6+
assert cnf.get('hummingbird_url') == 'http://hummingbird.dizzyninja.co/'
7+
assert cnf.get('dw_dbname') == 'rightsidedw'
8+
assert cnf.get('email_server') == 'mail.notify-customer.com'
9+
10+
11+
def test_hummingbird():
12+
# Test hummingbird.py
13+
# TODO: Add more hummingbird commands to the test.
14+
from hummingbird import hummingbird
15+
16+
h = hummingbird()
17+
result = h('word-split', input='toiletseat')
18+
assert result['output']['split'] == 'toilet seat'
19+
20+
21+
def test_data_warehouse():
22+
# Test data_warehouse.py
23+
# This test dumps a database table to a file.
24+
# TODO: Add a select query and an update query to the test.
25+
from data_warehouse import data_warehouse
26+
27+
dw = data_warehouse()
28+
# TODO: Make the following line readable.
29+
query = "unload ('SELECT * FROM utld.tld') to 's3://rightside-data/whois/output/ActiveDomainStatus_20141210_ce.txt' with CREDENTIALS 'aws_access_key_id=AKIAI5ZAKRM5QRQ3LJXA;aws_secret_access_key=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' DELIMITER '\t' PARALLEL OFF ALLOWOVERWRITE GZIP;"
30+
assert dw(query) == u'no results to fetch'
31+
#result = dw(query)
32+
#print result
33+
x='x'
34+
35+
36+
37+
def test_send_mail():
38+
# Test send_mail.py
39+
# The send_mail function only works inside the firewall.
40+
from send_mail import send_mail
41+
email_address = '[email protected]'
42+
subject = 'test.py'
43+
assert send_mail(email_address=email_address, subject=subject, body="") == None
44+
45+
46+
if __name__ == '__main__':
47+
test_config()
48+
test_data_warehouse()
49+
test_hummingbird()
50+
#test_send_mail()
51+

0 commit comments

Comments
 (0)