Ejemplo n.º 1
0
def connect(clear_on_failure=False):
    config.logger.info("controller:connect(%s)", clear_on_failure)
    if not config.connected:
        try:
            connect_semaphore.acquire(1)
            if config.app_window is not None:
                try:
                    #                    config.logger.debug("controller: keyring.get_keyring() %s",keyring.get_keyring())
                    #HACK set keyring backend explicitly
                    if platform.system().startswith("Windows"):
                        keyring.set_keyring(
                            keyring.backends.Windows.WinVaultKeyring(
                            ))  # @UndefinedVariable
                    elif platform.system() == 'Darwin':
                        keyring.set_keyring(keyring.backends.OS_X.Keyring())
                    else:  # Linux
                        keyring.set_keyring(keyring.backends.SecretService())
                except Exception as e:
                    config.logger.error("controller: keyring Exception %s", e)
                if config.app_window.plus_account is not None:  # @UndefinedVariable
                    try:
                        # try-catch as the keyring might not work
                        config.passwd = keyring.get_password(
                            config.app_name, config.app_window.plus_account
                        )  # @UndefinedVariable
                        if config.passwd is None:
                            config.logger.debug(
                                "controller: -> keyring.get_password returned None"
                            )
                        else:
                            config.logger.debug(
                                "controller: -> keyring passwd received")
                    except Exception as e:
                        config.logger.error("controller: keyring Exception %s",
                                            e)
                if config.app_window.plus_account is None or config.passwd is None:  # @UndefinedVariable
                    # ask user for credentials
                    import plus.login
                    login, passwd, remember = plus.login.plus_login(
                        config.app_window, config.app_window.plus_email,
                        config.app_window.plus_remember_credentials
                    )  # @UndefinedVariable
                    config.app_window.plus_remember_credentials = remember
                    # store credentials
                    config.app_window.plus_account = login
                    if remember:
                        config.app_window.plus_email = login
                    else:
                        config.app_window.plus_email = None
                    # store the passwd in the keychain
                    if login is not None and passwd is not None and remember:
                        try:
                            # try-catch as the keyring might not work
                            keyring.set_password(config.app_name, login,
                                                 passwd)
                            config.logger.debug(
                                "controller: -> keyring set (%s)", passwd)
                        except Exception as e:
                            config.logger.error(
                                "controller: keyring Exception %s", e)
                    config.passwd = passwd  # remember password in memory for this session
            if config.app_window.plus_account is None:  # @UndefinedVariable
                config.app_window.sendmessage(
                    QApplication.translate("Plus", "Login aborted",
                                           None))  # @UndefinedVariable
            else:
                success = connection.authentify()
                if success:
                    config.connected = success
                    config.app_window.sendmessage(
                        config.app_window.plus_account + " " +
                        QApplication.translate("Plus", "authentified",
                                               None))  # @UndefinedVariable
                    config.app_window.sendmessage(
                        QApplication.translate("Plus",
                                               "Connected to artisan.plus",
                                               None))  # @UndefinedVariable
                    try:
                        queue.start()  # start the outbox queue
                    except:
                        pass
                else:
                    if clear_on_failure:
                        connection.clearCredentials()
                        config.app_window.sendmessage(
                            QApplication.translate("Plus",
                                                   "artisan.plus turned off",
                                                   None))  # @UndefinedVariable
                    else:
                        message = QApplication.translate(
                            "Plus", "Authentification failed", None)
                        if not config.app_window.plus_account is None:  # @UndefinedVariable
                            message = config.app_window.plus_account + " " + message  # @UndefinedVariable
                        config.app_window.sendmessage(
                            message)  # @UndefinedVariable
        except Exception as e:
            config.logger.error("controller: connect Exception %s", e)
            if clear_on_failure:
                connection.clearCredentials()
                config.app_window.sendmessage(
                    QApplication.translate("Plus", "artisan.plus turned off",
                                           None))  # @UndefinedVariable
            else:
                config.app_window.sendmessage(
                    QApplication.translate("Plus",
                                           "Couldn't connect to artisan.plus",
                                           None))  # @UndefinedVariable
                config.connected = False
        finally:
            if connect_semaphore.available() < 1:
                connect_semaphore.release(1)
        config.app_window.updatePlusStatus()  # @UndefinedVariable
        stock.update()
Ejemplo n.º 2
0
def connect(clear_on_failure=False, interactive=True):
    if not is_connected():
        config.logger.info("controller:connect(%s,%s)", clear_on_failure,
                           interactive)
        try:
            connect_semaphore.acquire(1)
            if config.app_window is not None:
                connection.setKeyring()
                account = config.app_window.plus_account
                if account is None:
                    account = config.app_window.plus_email
                    if isinstance(threading.current_thread(),
                                  threading._MainThread):
                        # this is dangerous and should only be done while running in the main GUI thread as a consequence are GUI actions which might crash in other threads
                        interactive = True
                if account is not None:  # @UndefinedVariable
                    try:
                        # try-catch as the keyring might not work
                        config.passwd = keyring.get_password(
                            config.app_name, account)  # @UndefinedVariable
                        if config.passwd is None:
                            config.logger.debug(
                                "controller: -> keyring.get_password returned None"
                            )
                        else:
                            config.logger.debug(
                                "controller: -> keyring passwd received")
                    except Exception as e:
                        config.logger.error("controller: keyring Exception %s",
                                            e)
                if interactive and (
                        config.app_window.plus_account is None
                        or config.passwd is None):  # @UndefinedVariable
                    # ask user for credentials
                    import plus.login
                    login, passwd, remember, res = plus.login.plus_login(
                        config.app_window, config.app_window.plus_email,
                        config.passwd, config.app_window.
                        plus_remember_credentials)  # @UndefinedVariable
                    if res:  # Login dialog not Canceled
                        config.app_window.plus_remember_credentials = remember
                        # store credentials
                        config.app_window.plus_account = login
                        if remember:
                            config.app_window.plus_email = login
                        else:
                            config.app_window.plus_email = None
                        # store the passwd in the keychain
                        if login is not None and passwd is not None and remember:
                            try:
                                # try-catch as the keyring might not work
                                keyring.set_password(config.app_name, login,
                                                     passwd)
                                config.logger.debug(
                                    "controller: -> keyring set password (%s)",
                                    login)
                            except Exception as e:
                                config.logger.error(
                                    "controller: keyring Exception %s", e)
                                if not platform.system().startswith(
                                        "Windows"
                                ) and platform.system() != 'Darwin':
                                    # on Linux remind to install the gnome-keyring
                                    config.app_window.sendmessageSignal.emit(
                                        QApplication.translate(
                                            "Plus",
                                            "Keyring error: Ensure that gnome-keyring is installed.",
                                            None), True,
                                        None)  # @UndefinedVariable
                        config.passwd = passwd  # remember password in memory for this session
            if config.app_window.plus_account is None:  # @UndefinedVariable
                if interactive:
                    config.app_window.sendmessageSignal.emit(
                        QApplication.translate("Plus", "Login aborted", None),
                        True, None)  # @UndefinedVariable
            else:
                success = connection.authentify()
                if success:
                    config.connected = success
                    config.app_window.sendmessageSignal.emit(
                        config.app_window.plus_account + " " +
                        QApplication.translate("Plus", "authentified", None),
                        True, None)  # @UndefinedVariable
                    config.app_window.sendmessageSignal.emit(
                        QApplication.translate("Plus",
                                               "Connected to artisan.plus",
                                               None), True,
                        None)  # @UndefinedVariable
                    config.logger.info("Artisan v%s (%s, %s) connected",
                                       str(__version__), str(__revision__),
                                       str(__build__))
                    try:
                        queue.start()  # start the outbox queue
                    except Exception:
                        pass
                    try:
                        config.app_window.resetDonateCounter()
                    except Exception:
                        pass
                else:
                    if clear_on_failure:
                        connection.clearCredentials()
                        config.app_window.sendmessageSignal.emit(
                            QApplication.translate("Plus",
                                                   "artisan.plus turned off",
                                                   None), True,
                            None)  # @UndefinedVariable
                    elif interactive:
                        message = QApplication.translate(
                            "Plus", "Authentication failed", None)
                        if not config.app_window.plus_account is None:  # @UndefinedVariable
                            message = config.app_window.plus_account + " " + message  # @UndefinedVariable
                        config.app_window.sendmessageSignal.emit(
                            message, True, None)  # @UndefinedVariable
        except Exception as e:
            if interactive:
                config.logger.error("controller: connect Exception %s", e)
            if clear_on_failure:
                connection.clearCredentials()
                if interactive:
                    config.app_window.sendmessageSignal.emit(
                        QApplication.translate("Plus",
                                               "artisan.plus turned off",
                                               None), True, None)
            else:
                if interactive:
                    config.app_window.sendmessageSignal.emit(
                        QApplication.translate(
                            "Plus", "Couldn't connect to artisan.plus", None),
                        True, None)
                config.connected = False
        finally:
            if connect_semaphore.available() < 1:
                connect_semaphore.release(1)
        config.app_window.updatePlusStatusSignal.emit()  # @UndefinedVariable
        if interactive and is_connected():
            stock.update()
Ejemplo n.º 3
0
def connect(clear_on_failure=False,interactive=True):
    if not is_connected():
        config.logger.info("controller:connect(%s,%s)",clear_on_failure,interactive)
        try:
            connect_semaphore.acquire(1)
            if config.app_window is not None:
                connection.setKeyring()
                if config.app_window.plus_account is not None: # @UndefinedVariable
                    try:
                        # try-catch as the keyring might not work
                        config.passwd = keyring.get_password(config.app_name, config.app_window.plus_account) # @UndefinedVariable
                        if config.passwd is None:
                            config.logger.debug("controller: -> keyring.get_password returned None")
                        else:
                            config.logger.debug("controller: -> keyring passwd received")
                    except Exception as e:
                        config.logger.error("controller: keyring Exception %s",e)
                if interactive and (config.app_window.plus_account is None or config.passwd is None): # @UndefinedVariable
                    # ask user for credentials
                    import plus.login
                    login,passwd,remember = plus.login.plus_login(config.app_window,config.app_window.plus_email,config.app_window.plus_remember_credentials) # @UndefinedVariable
                    config.app_window.plus_remember_credentials = remember
                    # store credentials
                    config.app_window.plus_account = login
                    if remember:
                        config.app_window.plus_email = login
                    else:
                        config.app_window.plus_email = None                    
                    # store the passwd in the keychain
                    if login is not None and passwd is not None and remember:
                        try:
                            # try-catch as the keyring might not work
                            keyring.set_password(config.app_name, login, passwd)
                            config.logger.debug("controller: -> keyring set password (%s)",login)
                        except Exception as e:
                            config.logger.error("controller: keyring Exception %s",e)
                            if not platform.system().startswith("Windows") and platform.system() != 'Darwin':
                                # on Linux remind to install the gnome-keyring
                                config.app_window.sendmessage(QApplication.translate("Plus","Keyring error: Ensure that gnome-keyring is installed.",None)) # @UndefinedVariable 
                    config.passwd = passwd # remember password in memory for this session
            if config.app_window.plus_account is None: # @UndefinedVariable
                if interactive:
                    config.app_window.sendmessage(QApplication.translate("Plus","Login aborted",None)) # @UndefinedVariable
            else:
                success = connection.authentify()
                if success:
                    config.connected = success
                    config.app_window.sendmessage(config.app_window.plus_account + " " + QApplication.translate("Plus","authentified",None)) # @UndefinedVariable
                    config.app_window.sendmessage(QApplication.translate("Plus","Connected to artisan.plus",None))  # @UndefinedVariable
                    try:
                        queue.start() # start the outbox queue
                    except:
                        pass
                else:
                    if clear_on_failure:
                        connection.clearCredentials()
                        config.app_window.sendmessage(QApplication.translate("Plus","artisan.plus turned off",None))  # @UndefinedVariable
                    elif interactive:
                        message = QApplication.translate("Plus","Authentication failed",None)
                        if not config.app_window.plus_account is None: # @UndefinedVariable
                            message = config.app_window.plus_account + " " + message # @UndefinedVariable
                        config.app_window.sendmessage(message) # @UndefinedVariable
        except Exception as e:
            if interactive:
                config.logger.error("controller: connect Exception %s",e)
            if clear_on_failure:
                connection.clearCredentials()
                if interactive:
                    config.app_window.sendmessage(QApplication.translate("Plus","artisan.plus turned off",None)) # @UndefinedVariable
            else:
                if interactive:
                    config.app_window.sendmessage(QApplication.translate("Plus","Couldn't connect to artisan.plus",None)) # @UndefinedVariable
                config.connected = False
        finally:
            if connect_semaphore.available() < 1:
                connect_semaphore.release(1)
        config.app_window.updatePlusStatus() # @UndefinedVariable
        if interactive and is_connected():
            stock.update()