<filename>youtube_dl_gui/__init__.py
# -*- coding: utf-8 -*-

"""Youtubedlg __init__ file.

Responsible on how the package looks from the outside.

Example:
    In order to load the GUI from a python script.

        import youtube_dl_gui

        youtube_dl_gui.main()

"""


import sys
import os

if __package__ is None and not hasattr(sys, 'frozen'):
    # direct call of __main__.py
    root_path = os.path.realpath(os.path.abspath(__file__))
    sys.path.insert(0, os.path.dirname(os.path.dirname(root_path)))

import youtube_dl_gui
import gettext

try:
    import wx
except ImportError as error:
    print(error)
    sys.exit(1)

from .formats import *
from .version import __version__
from .info import (
    __author__,
    __appname__,
    __contact__,
    __license__,
    __projecturl__,
    __licensefull__,
    __description__,
    __descriptionfull__,
)

from .logmanager import LogManager
from .optionsmanager import OptionsManager
from .utils import (
    get_config_path,
    get_locale_file,
    os_path_exists,
    YOUTUBEDL_BIN
)

__packagename__ = "youtube_dl_gui"

# Set config path and create options and log managers
config_path = get_config_path()

opt_manager = OptionsManager(config_path)
log_manager = None

if opt_manager.options['enable_log']:
    log_manager = LogManager(config_path, opt_manager.options['log_time'])

# Set gettext before MainFrame import
# because the GUI strings are class level attributes
locale_dir = get_locale_file()
lang = gettext.gettext

try:
    lang = gettext.translation(__packagename__,
                               locale_dir,
                               [opt_manager.options['locale_name']])
except IOError:
    opt_manager.options['locale_name'] = 'en_US'
    lang = gettext.translation(__packagename__,
                               locale_dir,
                               [opt_manager.options['locale_name']])


# Redefine _ to gettext in builtins
_ = lang.gettext
OUTPUT_FORMATS, DEFAULT_FORMATS, AUDIO_FORMATS, VIDEO_FORMATS, FORMATS = reload_strings(_)


from .mainframe import MainFrame


def main():
    """The real main. Creates and calls the main app windows. """
    youtubedl_path = os.path.join(opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN)

    app = wx.App()
    frame = MainFrame(opt_manager, log_manager)
    frame.Show()

    if opt_manager.options["disable_update"] and not os_path_exists(youtubedl_path):
        wx.MessageBox(_("Failed to locate youtube-dl and updates are disabled"), _("Error"), wx.OK | wx.ICON_ERROR)
        frame.close()

    app.MainLoop()
