proxmaster/utils.py
2016-04-01 02:37:21 +03:00

109 lines
2.7 KiB
Python

#. -*- coding: utf-8
#
# helper functions
import functools
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)
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
def find_rec(search_dict, field):
"""
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):
results = find_rec(value, field)
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
def get_path(search_dict, key):
""" takes a nested dict and returns the path for the searched value """
for k,v in search_dict.items():
if isinstance(v,dict):
p = get_path(v,key)
if p:
return [k] + p
elif v == key:
return [k]
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)
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()