2016-02-15 05:30:43 -05:00
|
|
|
#. -*- coding: utf-8
|
|
|
|
#
|
|
|
|
# helper functions
|
|
|
|
|
|
|
|
import functools
|
2016-03-31 10:40:40 -04:00
|
|
|
from copy import deepcopy
|
|
|
|
from random import SystemRandom
|
|
|
|
|
|
|
|
|
|
|
|
def genpassword(length=20):
|
|
|
|
""" generates pseudo-random password """
|
|
|
|
choice = SystemRandom().choice
|
|
|
|
charsets = [
|
|
|
|
'abcdefghijklmnopqrstuvwxyz',
|
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
|
|
'0123456789',
|
|
|
|
'%&=?+~#-_.',
|
|
|
|
]
|
|
|
|
pwd = []
|
|
|
|
charset = choice(charsets)
|
|
|
|
while len(pwd) < length:
|
|
|
|
pwd.append(choice(charset))
|
|
|
|
charset = choice(list(set(charsets) - set([charset])))
|
|
|
|
return "".join(pwd)
|
|
|
|
|
2016-02-15 05:30:43 -05:00
|
|
|
|
|
|
|
def dict_merge(target, *args):
|
|
|
|
""" Recursively merges mutiple dicts """
|
|
|
|
# Merge multiple dicts
|
|
|
|
if len(args) > 1:
|
|
|
|
for obj in args:
|
|
|
|
dict_merge(target, obj)
|
|
|
|
return target
|
|
|
|
|
|
|
|
# Recursively merge dicts and set non-dict values
|
|
|
|
obj = args[0]
|
|
|
|
if not isinstance(obj, dict):
|
|
|
|
return obj
|
|
|
|
for k, v in obj.items():
|
|
|
|
if k in target and isinstance(target[k], dict):
|
|
|
|
dict_merge(target[k], v)
|
|
|
|
else:
|
|
|
|
target[k] = deepcopy(v)
|
|
|
|
return target
|
|
|
|
|
2016-03-31 10:40:40 -04:00
|
|
|
|
2016-03-29 22:50:03 -04:00
|
|
|
def find_rec(search_dict, field):
|
2016-02-15 05:30:43 -05:00
|
|
|
"""
|
|
|
|
Takes a dict with nested lists and dicts,
|
|
|
|
and searches all dicts for a key of the field
|
|
|
|
provided.
|
|
|
|
"""
|
|
|
|
fields_found = []
|
|
|
|
|
|
|
|
for key, value in search_dict.items():
|
|
|
|
if key == field:
|
|
|
|
fields_found.append(value)
|
|
|
|
|
|
|
|
elif isinstance(value, dict):
|
2016-03-29 22:50:03 -04:00
|
|
|
results = find_rec(value, field)
|
2016-02-15 05:30:43 -05:00
|
|
|
for result in results:
|
|
|
|
fields_found.append(result)
|
|
|
|
|
|
|
|
elif isinstance(value, list):
|
|
|
|
for item in value:
|
|
|
|
if isinstance(item, dict):
|
|
|
|
more_results = get_recursively(item, field)
|
|
|
|
for another_result in more_results:
|
|
|
|
fields_found.append(another_result)
|
|
|
|
|
|
|
|
return fields_found
|
|
|
|
|
|
|
|
|
2016-03-29 22:50:03 -04:00
|
|
|
def get_path(search_dict, key):
|
2016-03-03 20:51:54 -05:00
|
|
|
""" takes a nested dict and returns the path for the searched value """
|
|
|
|
for k,v in search_dict.items():
|
|
|
|
if isinstance(v,dict):
|
2016-03-29 22:50:03 -04:00
|
|
|
p = get_path(v,key)
|
2016-03-03 20:51:54 -05:00
|
|
|
if p:
|
|
|
|
return [k] + p
|
|
|
|
elif v == key:
|
|
|
|
return [k]
|
2016-02-15 05:30:43 -05:00
|
|
|
|
|
|
|
|
|
|
|
def chained_get(dct, *keys):
|
|
|
|
SENTRY = object()
|
|
|
|
def getter(level, key):
|
|
|
|
return 'NA' if level is SENTRY else level.get(key, SENTRY)
|
|
|
|
return functools.reduce(getter, keys, dct)
|
|
|
|
|
2016-03-31 10:40:40 -04:00
|
|
|
|
2016-03-31 19:37:21 -04:00
|
|
|
def sendmail(FROM,TO,SUBJECT,TEXT,SERVER):
|
|
|
|
import smtplib
|
|
|
|
"""this is some test documentation in the function"""
|
|
|
|
message = """\
|
|
|
|
From: %s
|
|
|
|
To: %s
|
|
|
|
Subject: %s
|
|
|
|
%s
|
|
|
|
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
|
|
|
|
# Send the mail
|
|
|
|
server = smtplib.SMTP(SERVER)
|
|
|
|
"New part"
|
|
|
|
server.starttls()
|
|
|
|
#server.login('username', 'password')
|
|
|
|
server.sendmail(FROM, TO, message)
|
|
|
|
server.quit()
|
|
|
|
|