Example #1
0
 def _getMD(self):
     if self._md is None:
         try:
             from markdown import Markdown
             if getPlatform()==PLATFORM_WINDOWS and isPyinstallerBuild():
                 self._md = Markdown()
             else:
                 self._md = Markdown(extensions=['extra'])
         except ImportError:
             self.logger.error("Cannot enable Markdown (%s)", formatException())
             raise
     return self._md
def startLunchinator():
    (options, _args) = parse_args()
    # @todo: using the variable "usePlugins" this way is confusing - refactor later
    usePlugins = options.noPlugins
    if options.output:
        options.cli = False
        options.noGui = True
        usePlugins = True  # <- means do NOT use plugins

    defaultLogPath = os.path.join(MAIN_CONFIG_DIR, "lunchinator.log")
    if options.exitWithStopCode:
        sys.exit(EXIT_CODE_STOP)
    elif options.version:
        initLogger(options)
        print "Lunchinator", get_settings().get_version()
        sys.exit(0)
    elif options.lunchCall or options.message != None:
        initLogger(options)
        get_settings().set_plugins_enabled(False)
        sendMessage(options.message, options.client)
    elif options.input:
        initLogger(options)
        get_settings().set_plugins_enabled(False)
        msg = sys.stdin.read()
        # @todo options.client
        if msg:
            sendMessage("HELO_LOCAL_PIPE " + msg, "127.0.0.1")
    elif options.stop:
        initLogger(options)
        get_settings().set_plugins_enabled(False)
        get_server().stop_server(stop_any=True)
        print "Sent stop command to local lunchinator"
    elif options.installDep:
        initLogger(options)
        req = getCoreDependencies()
        installDependencies(req)

    # lunchinator starts in listening mode:

    elif options.cli:
        initLogger(options, defaultLogPath)
        usePlugins = checkDependencies(usePlugins)

        retCode = 1
        try:
            from lunchinator import lunch_cli

            get_settings().set_plugins_enabled(usePlugins)
            get_server().set_disable_broadcast(options.noBroadcast)
            cli = lunch_cli.LunchCommandLineInterface()
            sys.retCode = cli.start()
        except:
            getCoreLogger().exception("cli version cannot be started")
        finally:
            sys.exit(retCode)
    elif options.noGui:
        initLogger(options, defaultLogPath)
        usePlugins = checkDependencies(usePlugins)

        #    sys.settrace(trace)
        get_settings().set_plugins_enabled(usePlugins)
        get_server().set_disable_broadcast(options.noBroadcast)
        get_server().initialize()
        get_server().start_server()
        sys.exit(get_server().exitCode)
    else:
        signal.signal(signal.SIGINT, signal.SIG_IGN)

        if getPlatform() != PLATFORM_MAC:
            import socket

            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                s.bind(("", 50000))
                s.close()
            except:
                # something is listening, hopefully a lunchinator
                print "The lunchinator port is already in use, trying to open window"
                initLogger(options)
                sendMessage("HELO_OPEN_WINDOW please", "127.0.0.1")
                sys.exit(0)

        initLogger(options, defaultLogPath)

        getCoreLogger().info("We are on %s, %s, version %s", platform.system(), platform.release(), platform.version())
        try:
            from PyQt4.QtCore import QThread

            set_has_gui(True)
        except:
            getCoreLogger().error("pyQT4 not found - start lunchinator with --no-gui")
            sys.exit(EXIT_CODE_NO_QT)

        from lunchinator.gui_controller import LunchinatorGuiController
        from PyQt4.QtGui import QApplication

        class LunchApplication(QApplication):
            def notify(self, obj, event):
                try:
                    return QApplication.notify(self, obj, event)
                except:
                    getCoreLogger().exception("C++ Error")
                    return False

        app = LunchApplication(sys.argv)
        usePlugins = checkDependencies(usePlugins)

        get_settings().set_plugins_enabled(usePlugins)
        get_server().set_disable_broadcast(options.noBroadcast)
        app.setQuitOnLastWindowClosed(False)
        lanschi = LunchinatorGuiController()
        if lanschi.isShuttingDown():
            # seems lanschi would prefer to not start up
            sys.exit(0)
        if options.showWindow:
            lanschi.openWindowClicked()

        if getPlatform() == PLATFORM_MAC and isPyinstallerBuild():
            import AppKit

            class MyDelegate(AppKit.AppKit.NSObject):
                def applicationShouldHandleReopen_hasVisibleWindows_(self, _app, hasOpenWindow):
                    if not hasOpenWindow:
                        lanschi.openWindowClicked()

            delegate = MyDelegate.alloc().init()
            AppKit.AppKit.NSApplication.sharedApplication().setDelegate_(delegate)

        # enable CRTL-C
        signal.signal(signal.SIGINT, partial(handleInterrupt, lanschi))

        try:
            app.exec_()
        finally:
            retValue = lanschi.quit()
            sys.exit(retValue)