"""
This module contains the hooks load and our builtin hooks.

"""
import re
import pkg_resources
import traceback


#: Name of the entrypoint to use in setup.py
ENTRYPOINT = 'pyqt_distutils_hooks'


def load_hooks():
    """
    Load the exposed hooks.

    Returns a dict of hooks where the keys are the name of the hook and the
    values are the actual hook functions.
    """
    hooks = {}
    for entrypoint in pkg_resources.iter_entry_points(ENTRYPOINT):
        name = str(entrypoint).split('=')[0].strip()
        try:
            hook = entrypoint.load()
        except Exception:
            traceback.print_exc()
        else:
            hooks[name] = hook
    return hooks


def hook(ui_file_path):
    """
    This is the prototype of a hook function.
    """
    pass


GETTEXT_REPLACEMENT = '''        import gettext
        _ = gettext.gettext
'''


def gettext(ui_file_path):
    """
    Let you use gettext instead of the Qt tools for l18n
    """
    with open(ui_file_path, 'r') as fin:
        content = fin.read()

    content = re.sub(r'_translate\(".*",\s', '_(', content)

    with open(ui_file_path, 'w') as fout:
        fout.write(content.replace(
            '        _translate = QtCore.QCoreApplication.translate',
            GETTEXT_REPLACEMENT))
