# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

import importlib
import os
import sys


def _import_package_bootstrap():
    pkg_dir = os.path.dirname(__file__)
    pkg_name = os.path.basename(pkg_dir)
    parent_dir = os.path.dirname(pkg_dir)

    if parent_dir and parent_dir not in sys.path:
        sys.path.insert(0, parent_dir)

    qualified_name = "{0}.bootstrap".format(pkg_name)
    return importlib.import_module(qualified_name)


def _report_entry_failure(entry_name, exc):
    try:
        import nuke
        nuke.tprint(
            "[NukeChineseAnnotation] {0} failed and was isolated: {1}: {2}".format(
                entry_name,
                getattr(getattr(exc, "__class__", None), "__name__", "Exception"),
                str(exc),
            )
        )
    except Exception:
        pass


try:
    run_init_entry = getattr(_import_package_bootstrap(), "run_init_entry")
    run_init_entry()
except Exception as exc:
    # A damaged or incompatible plug-in must not abort Nuke initialization.
    _report_entry_failure("init.py", exc)
