import inspect
from enum import Enum

import toml

from raiden.utils.serialization import serialize_bytes

HEADER_PREFIX = ' ' * 2
CONFIG_PREFIX = HEADER_PREFIX * 2


def _clean_non_serializables(data):
    copy = {}
    for key, value in data.items():
        if callable(value):
            continue

        if hasattr(value, 'to_dict'):
            value = value.to_dict()

        if isinstance(value, dict):
            value = _clean_non_serializables(value)

        if isinstance(value, bytes):
            value = serialize_bytes(value)

        if isinstance(value, tuple):
            value = list(value)

        if isinstance(key, Enum):
            key = key.name

        if isinstance(value, Enum):
            value = value.value

        copy[key] = value
    return copy


def dump_config(config):
    print(toml.dumps({'configs': config}))
    print()


def dump_cmd_options(options):
    print(toml.dumps({
        'options': _clean_non_serializables(options),
    }))
    print()


def dump_module(header, module):
    attribs = dict()
    for name, value in inspect.getmembers(module):
        if name.isupper():
            attribs[name] = value

    attribs = _clean_non_serializables(attribs)

    print(toml.dumps({header: attribs}))
    print()

@@ -42,13 +42,15 @@
 ort 
+py
 toml%0A
-
 %0Afro
@@ -106,65 +106,47 @@
 es%0A%0A
-HEADER_PREFIX = ' ' * 2%0ACONFIG_PREFIX = HEADER_PREFIX * 2
+builtin_types = (int, str, bool, tuple)
 %0A%0A%0Ad
@@ -722,24 +722,202 @@
 alue.value%0A%0A
+        if value and not isinstance(value, builtin_types):%0A            try:%0A                pytoml.dumps(%7Bkey: value%7D)%0A            except RuntimeError:%0A                continue%0A%0A
         copy
@@ -975,32 +975,34 @@
 fig):%0A    print(
+py
 toml.dumps(%7B'con
@@ -1065,32 +1065,34 @@
 ons):%0A    print(
+py
 toml.dumps(%7B%0A   
@@ -1380,16 +1380,16 @@
 tribs)%0A%0A
-
     prin
@@ -1390,16 +1390,18 @@
   print(
+py
 toml.dum

Fix toml import