import sys
from os.path import abspath

sys.path.append(abspath(f'{__file__}/../'))


from build import onbuild


def launch(func, *args, **kwargs):
    """
    Usage:
        # my_main.py
        from lk_utils.easy_launcher import launch

        def main(a, b):
            print(a + b)

        launch(main, a=1, b=2)
    """

    # noinspection PyUnusedLocal
    def show_err_on_console(err):
        print('Runtime Error:', f'\n\t{err}')

    def show_err_on_msgbox(err):
        # https://stackoverflow.com/questions/17280637/tkinter-messagebox
        # -without-window
        from tkinter import Tk, messagebox
        root = Tk()
        root.withdraw()
        messagebox.showerror(title='Runtime Error', message=err)

    try:
        func(*args, **kwargs)

    except:
        # To obtain more message about this error.
        #   https://stackoverflow.com/questions/1278705/when-i-catch-an
        #   -exception-how-do-i-get-the-type-file-and-line-number
        import traceback
        msg = traceback.format_exc()

        show_err_on_msgbox(msg)
        #   show_err_on_console(msg)

        input('Prgress terminated, press ENTER to close the window...')


launch(onbuild.main)
