import hashlib

from Crypto.PublicKey import RSA
from concurrent.futures import ThreadPoolExecutor, Future
from functools import partial


def create_xml_node(name, attrs={}, text=""):
    """create a xml node with optional attributes

    > create_xml_node("test", {"foo": "bar"}, "some text")
    <test foo="bar">some text</test>

    Args:
        name:    name of the node
        attrs:   additional attributes for the node
        text:    text between opening/closing tag

    Returns:
        A xml node as string
    """
    attrs_xml = " ".join(map(lambda v: "{}=\"{}\"".format(*v), attrs.items()))
    return "<{} {}>{}</{}>".format(name, attrs_xml, text, name)


def safe_get(name, structure):
    if name not in structure:
        return None
    return structure[name]


def md5digest(input):
    return hashlib.md5(input).hexdigest()


def generate_rsa_key_pair():
    rsa = RSA.generate(4096)
    pub = rsa.publickey()
    return (rsa.exportKey("PEM"), pub.exportKey("OpenSSH"))


def flatten(to_flatten):
    x = []
    if isinstance(to_flatten, list):
        for i in to_flatten:
            x = x + flatten(i)
        return x
    return [to_flatten]


def in_parallel(worker_count, objects, executor):
    try:
        pool = ThreadPoolExecutor(worker_count)
        futures = map(partial(pool.submit, executor), objects)

        # handle possible errors
        errors = filter(None, map(Future.exception, futures))

        if errors:
            for err in errors:
                raise err
        return map(Future.result, futures)
    finally:
        pool.shutdown(wait=False)


def indented(lx, n):
    if not isinstance(lx, list):
        return (" " * n) + lx
    # FIXME: Make generator after debugging
    x = []
    for l in lx:
        x.append( (" " * n) + l)
    return x

@@ -828,16 +828,32 @@
 d5(input
+.encode(%22utf-8%22)
 ).hexdig
@@ -1307,16 +1307,21 @@
 tures = 
+list(
 map(part
@@ -1356,16 +1356,17 @@
 objects)
+)
 %0A%0A      
@@ -1409,16 +1409,21 @@
 rrors = 
+list(
 filter(N
@@ -1458,16 +1458,17 @@
 utures))
+)
 %0A%0A      

move to python3