import atexit as o_atexit
import subprocess as o_subprocess
import psutil as o_psutil

from . import atexit, subprocess, psutil

modules = [
    (o_atexit, atexit, {}),
    (o_subprocess, subprocess, {}),
    (o_psutil, psutil, {}),
]


def init_mappings():
    for original, spoof, mapping in modules:
        if mapping:
            continue
        mapping.update(
            {
                attr: (getattr(original, attr, None), spoofed)
                for attr, spoofed in vars(spoof).items()
                if not attr.startswith("_") and hasattr(original, attr)
            }
        )


def setup():
    for original, spoof, mappings in modules:
        for attr, (orig, spoofed) in mappings.items():
            setattr(original, attr, spoofed)


def setdown():
    for original, spoof, mappings in modules:
        for attr, (orig, spoofed) in mappings.items():
            setattr(original, attr, orig)


init_mappings()
