# Path: stagehand/main.py
# def main():
#     p = FullHelpParser(prog='stagehand')
#     p.add_argument('-q', '--quiet', dest='quiet', action='store_true',
#                    help='disable all logging')
#     p.add_argument('-v', '--verbose', dest='verbose', action='append_const', default=[], const=1,
#                    help='log more detail (twice or thrice logs even more)')
#     p.add_argument('-b', '--bg', dest='background', action='store_true',
#                    help='run stagehand in the background (daemonize)')
#     p.add_argument('-p', '--port', dest='port', action='store', type=int, default=0,
#                    help='port the embedded webserver listens on (default is %d)' % config.web.port)
#     p.add_argument('-d', '--data', dest='data', action='store', metavar='PATH',
#                    help="path to Stagehand's static data directory")
#     p.add_argument('-s', '--stop', dest='stop', action='store_true',
#                    help='stop a currently running instance of Stagehand')
#     p.add_argument('--version', action='version', version='%(prog)s ' + __version__)
#     args = p.parse_args()
# 
#     paths = init_default_paths(args.data)
# 
#     if os.path.exists(paths.config):
#         config.load(paths.config)
#     if config.misc.logdir != config.misc.logdir.default:
#         # If logdir configurable is non-default, then apply it.
#         paths.logs = build_path(config.misc.logdir)
# 
#     create_paths(paths)
# 
#     handler = logging.FileHandler(os.path.join(paths.logs, 'stagehand.log'))
#     handler.setFormatter(logging.getLogger().handlers[0].formatter)
#     logging.getLogger().addHandler(handler)
# 
#     handler = logging.FileHandler(os.path.join(paths.logs, 'http.log'))
#     handler.setFormatter(logging.getLogger().handlers[0].formatter)
#     logging.getLogger('stagehand.http').addHandler(handler)
# 
#     if args.stop:
#         if call_rest_api('/api/shutdown') is None:
#             log.info('Stagehand is not running.')
#         return
# 
# 
#     # Make sure Stagehand isn't already running.
#     if call_rest_api('/api/pid') is not None:
#         return log.error('Stagehand is already running')
# 
# 
#     # Default log levels.
# 
#     # XXX: these should be INFO, WARNING, INFO but increase levels temporarily
#     # for development.
#     log.setLevel(logging.DEBUG)
#     logging.getLogger('stagehand.http').setLevel(logging.INFO)
#     logging.getLogger('http').setLevel(logging.DEBUG)
# 
#     if args.quiet:
#         log.setLevel(logging.CRITICAL)
#     elif len(args.verbose) == 1:
#         log.setLevel(logging.DEBUG)
#         logging.getLogger('stagehand.web').setLevel(logging.INFO)
#     elif len(args.verbose) >= 2:
#         log.setLevel(logging.DEBUG2)
#         if len(args.verbose) == 3:
#             logging.getLogger('stagehand.web').setLevel(logging.DEBUG2)
#             logging.getLogger('stagehand.http').setLevel(logging.DEBUG)
#         else:
#             logging.getLogger('stagehand.web').setLevel(logging.DEBUG)
#             logging.getLogger('stagehand.http').setLevel(logging.INFO)
# 
#     httplog = logging.getLogger('stagehand.http')
#     handler = logging.StreamHandler()
#     handler.setFormatter(logging.Formatter('%(asctime)s [HTTP] %(message)s'))
#     httplog.addHandler(handler)
#     httplog.propagate = False
# 
#     if args.background:
#         daemonize(chdir=None)
# 
#     loop = asyncio.get_event_loop()
#     mgr = Manager(paths, loop=loop)
#     # Take care to start platform plugins before any logging is done.  At least
#     # for win32, for reasons yet unclear, it seems that performing any output
#     # before the window is created can cause it to fail.
#     loop.run_until_complete(platform.start(mgr))
# 
#     log.info('starting Stagehand %s', __version__)
# 
#     config.web.add_monitor(functools.partial(web_config_changed, mgr, args))
#     asyncio.async(web_start_server(mgr, args))
#     asyncio.async(mgr.start())
# 
#     if sys.platform != 'win32':
#         loop.add_signal_handler(signal.SIGHUP, reload, mgr)
#         loop.add_signal_handler(signal.SIGUSR1, resync, mgr)
# 
#     try:
#         loop.run_forever()
#     finally:
#         mgr.commit()
#         platform.stop()
#         # Return manager for interactive interpreter
#         return mgr


import sys
import os
import stagehand
    from ctypes import windll
from stagehand.main import main

def win32_version_error():
    """
    Pops up a message box on Windows indicating the Python version is
    incompatible.
    """
    if sys.hexversion >= 0x03000000:
        # Python 3 strings are unicode, so use the Unicode variant.
        MessageBox = windll.user32.MessageBoxW
    else:
        # Python 2 strings are non-unicode, so use the ANSI variant.
        MessageBox = windll.user32.MessageBoxA
    ver = sys.version.split()[0]
    MessageBox(None,
    	      'Stagehand requires Python 3.3 or later, but you have %s.\n\n'
              'You can download Python at www.python.org.' % ver,
              'Incompatible Python Version',
              0x00000010)

# Are we running a new enough Python?
if sys.hexversion < 0x03030000:
    if sys.platform == 'win32':
        win32_version_error()
    else:
        print('fatal: Python 3.3 or later is required')
        print('\nNote you can always run stagehand directly through an interpreter:')
        print('$ python3.4 {}'.format(sys.argv[0]))
    sys.exit(1)


# If we get this far, preflight checks pass. Let's start.

# Path: stagehand/main.py
# def main():
#     p = FullHelpParser(prog='stagehand')
#     p.add_argument('-q', '--quiet', dest='quiet', action='store_true',
#                    help='disable all logging')
#     p.add_argument('-v', '--verbose', dest='verbose', action='append_const', default=[], const=1,
#                    help='log more detail (twice or thrice logs even more)')
#     p.add_argument('-b', '--bg', dest='background', action='store_true',
#                    help='run stagehand in the background (daemonize)')
#     p.add_argument('-p', '--port', dest='port', action='store', type=int, default=0,
#                    help='port the embedded webserver listens on (default is %d)' % config.web.port)
#     p.add_argument('-d', '--data', dest='data', action='store', metavar='PATH',
#                    help="path to Stagehand's static data directory")
#     p.add_argument('-s', '--stop', dest='stop', action='store_true',
#                    help='stop a currently running instance of Stagehand')
#     p.add_argument('--version', action='version', version='%(prog)s ' + __version__)
#     args = p.parse_args()
# 
#     paths = init_default_paths(args.data)
# 
#     if os.path.exists(paths.config):
#         config.load(paths.config)
#     if config.misc.logdir != config.misc.logdir.default:
#         # If logdir configurable is non-default, then apply it.
#         paths.logs = build_path(config.misc.logdir)
# 
#     create_paths(paths)
# 
#     handler = logging.FileHandler(os.path.join(paths.logs, 'stagehand.log'))
#     handler.setFormatter(logging.getLogger().handlers[0].formatter)
#     logging.getLogger().addHandler(handler)
# 
#     handler = logging.FileHandler(os.path.join(paths.logs, 'http.log'))
#     handler.setFormatter(logging.getLogger().handlers[0].formatter)
#     logging.getLogger('stagehand.http').addHandler(handler)
# 
#     if args.stop:
#         if call_rest_api('/api/shutdown') is None:
#             log.info('Stagehand is not running.')
#         return
# 
# 
#     # Make sure Stagehand isn't already running.
#     if call_rest_api('/api/pid') is not None:
#         return log.error('Stagehand is already running')
# 
# 
#     # Default log levels.
# 
#     # XXX: these should be INFO, WARNING, INFO but increase levels temporarily
#     # for development.
#     log.setLevel(logging.DEBUG)
#     logging.getLogger('stagehand.http').setLevel(logging.INFO)
#     logging.getLogger('http').setLevel(logging.DEBUG)
# 
#     if args.quiet:
#         log.setLevel(logging.CRITICAL)
#     elif len(args.verbose) == 1:
#         log.setLevel(logging.DEBUG)
#         logging.getLogger('stagehand.web').setLevel(logging.INFO)
#     elif len(args.verbose) >= 2:
#         log.setLevel(logging.DEBUG2)
#         if len(args.verbose) == 3:
#             logging.getLogger('stagehand.web').setLevel(logging.DEBUG2)
#             logging.getLogger('stagehand.http').setLevel(logging.DEBUG)
#         else:
#             logging.getLogger('stagehand.web').setLevel(logging.DEBUG)
#             logging.getLogger('stagehand.http').setLevel(logging.INFO)
# 
#     httplog = logging.getLogger('stagehand.http')
#     handler = logging.StreamHandler()
#     handler.setFormatter(logging.Formatter('%(asctime)s [HTTP] %(message)s'))
#     httplog.addHandler(handler)
#     httplog.propagate = False
# 
#     if args.background:
#         daemonize(chdir=None)
# 
#     loop = asyncio.get_event_loop()
#     mgr = Manager(paths, loop=loop)
#     # Take care to start platform plugins before any logging is done.  At least
#     # for win32, for reasons yet unclear, it seems that performing any output
#     # before the window is created can cause it to fail.
#     loop.run_until_complete(platform.start(mgr))
# 
#     log.info('starting Stagehand %s', __version__)
# 
#     config.web.add_monitor(functools.partial(web_config_changed, mgr, args))
#     asyncio.async(web_start_server(mgr, args))
#     asyncio.async(mgr.start())
# 
#     if sys.platform != 'win32':
#         loop.add_signal_handler(signal.SIGHUP, reload, mgr)
#         loop.add_signal_handler(signal.SIGUSR1, resync, mgr)
# 
#     try:
#         loop.run_forever()
#     finally:
#         mgr.commit()
#         platform.stop()
#         # Return manager for interactive interpreter
#         return mgr


# Path: stagehand/bootstrap.py
import sys
import os
import stagehand
    from ctypes import windll
from stagehand.main import main

def win32_version_error():
    """
    Pops up a message box on Windows indicating the Python version is
    incompatible.
    """
    if sys.hexversion >= 0x03000000:
        # Python 3 strings are unicode, so use the Unicode variant.
        MessageBox = windll.user32.MessageBoxW
    else:
        # Python 2 strings are non-unicode, so use the ANSI variant.
        MessageBox = windll.user32.MessageBoxA
    ver = sys.version.split()[0]
    MessageBox(None,
    	      'Stagehand requires Python 3.3 or later, but you have %s.\n\n'
              'You can download Python at www.python.org.' % ver,
              'Incompatible Python Version',
              0x00000010)

# Are we running a new enough Python?
if sys.hexversion < 0x03030000:
    if sys.platform == 'win32':
        win32_version_error()
    else:
        print('fatal: Python 3.3 or later is required')
        print('\nNote you can always run stagehand directly through an interpreter:')
        print('$ python3.4 {}'.format(sys.argv[0]))
    sys.exit(1)


# If we get this far, preflight checks pass. Let's start.

main()
