<commit_before>from PyQt4.QtCore import QObject, pyqtSignal, QCoreApplication
from nxdrive.utils import encrypt
from nxdrive.utils import decrypt
from nxdrive.logging_config import get_logger, configure
from nxdrive.client.base_automation_client import get_proxies_for_handler
from nxdrive.utils import normalized_path
from nxdrive import __version__
import subprocess
from nxdrive.utils import ENCODING
import os
import uuid
import sys
log = get_logger(__name__)


try:
    # Set the cffi tmp path to lib for xattr
    if sys.platform != "win32":
        from cffi.verifier import set_tmpdir
        import nxdrive
        nxdrive_path = os.path.dirname(nxdrive.__file__)
        nxdrive_path = os.path.dirname(nxdrive_path)
        nxdrive_path = os.path.dirname(nxdrive_path)
        lib_path = os.path.join(nxdrive_path, 'lib-dynload')
        set_tmpdir(lib_path)
except Exception as e:
    pass

class EngineTypeMissing(Exception):
    pass

class MissingToken(Exception):
    pass


class GeneralSettings(object):
    """Summarize general settings"""

    def __init__(self, auto_update=False):
        self.auto_update = auto_update

    def __repr__(self):
        return "GeneralSettings<auto_update=%r>" % self.auto_update


class ServerBindingSettings(object):
    """Summarize server binding settings"""

    def __init__(self, server_url=None, server_version=None,
                 username=None, password=<PASSWORD>,
                 local_folder=None, initialized=False,
                 pwd_update_required=False):
        self.server_url = server_url
        self.server_version = server_version
        self.username = username
        self.password = password
        self.local_folder = local_folder
        self.initialized = initialized
        self.pwd_update_required = pwd_update_required

    def __repr__(self):
        return ("ServerBindingSettings<server_url=%s, server_version=%s, "
                "username=%s, local_folder=%s, initialized=%r, "
                "pwd_update_required=%r>") % (
                    self.server_url, self.server_version, self.username,
                    self.local_folder, self.initialized,
                    self.pwd_update_required)


class ProxySettings(object):
    """Summarize HTTP proxy settings"""

    def __init__(self, dao=None, config='System', proxy_type=None,
                 server=None, port=None,
                 authenticated=False, username=None, password=<PASSWORD>,
                 exceptions=None):
        self.config = config
        self.proxy_type = proxy_type
        self.server = server
        self.port = port
        self.authenticated = authenticated
        self.username = username
        self.password = password
        self.exceptions = exceptions
        if dao is not None:
            self.load(dao)

    def save(self, dao, token = None):
        # Encrypt password with token as the secret
        if token is None:
            token = dao.get_config("device_id", None)
        if token is None:
            raise MissingToken("Your token has been revoked,"
                        " please update your password to acquire a new one.")
        token = token + "_proxy"
        password = encrypt(self.password, token)
        dao.update_config("proxy_password", password)
        dao.update_config("proxy_username", self.username)
        dao.update_config("proxy_exceptions", self.exceptions)
        dao.update_config("proxy_port", self.port)
        dao.update_config("proxy_type", self.proxy_type)
        dao.update_config("proxy_config", self.config)
        dao.update_config("proxy_server", self.server)
        dao.update_config("proxy_authenticated", self.authenticated)

    def load(self, dao, token=None):
        self.config = dao.get_config("proxy_config", "System")
        self.proxy_type = dao.get_config("proxy_type", None)
        self.port = dao.get_config("proxy_port", None)
        self.server = dao.get_config("proxy_server", None)
        self.username = dao.get_config("proxy_username", None)
        password = dao.get_config("proxy_password", None)
        self.exceptions = dao.get_config("proxy_exceptions", None)
        self.authenticated = (dao.get_config("proxy_authenticated", "0") == "1")
        if token is None:
            token = dao.get_config("device_id", None)
        if password is not None and token is not None:
            token = token + "_proxy"
            self.password = decrypt(password, token)
        else:
            # If no server binding or no token available
            # (possibly after token revocation) reset password
            self.password = ''

    def __repr__(self):
        return ("ProxySettings<config=%s, proxy_type=%s, server=%s, port=%s, "
                "authenticated=%r, username=%s, exceptions=%s>") % (
                    self.config, self.proxy_type, self.server, self.port,
                    self.authenticated, self.username, self.exceptions)


class Manager(QObject):
    '''
    classdocs
    '''
    proxyUpdated = pyqtSignal(object)
    clientUpdated = pyqtSignal(object, object)
    engineNotFound = pyqtSignal(object)
    newEngine = pyqtSignal(object)
    dropEngine = pyqtSignal(object)
    started = pyqtSignal()
    _singleton = None

    @staticmethod
    def get():
        return Manager._singleton

    def __init__(self, options):
        '''
        Constructor
        '''
        if Manager._singleton is not None:
            raise Exception("Only one instance of Manager can be create")
        Manager._singleton = self
        super(Manager, self).__init__()
        self.nxdrive_home = os.path.expanduser(options.nxdrive_home)
        self._debug = options.debug
        self._dao = self._create_dao()
        self.proxies = None
        self.proxy_exceptions = None
        self._engines = None
        self._engine_definitions = None
        self._engine_types = dict()
        self.device_id = self._dao.get_config("device_id")
        self.updated = False#self.update_version()
        if self.device_id is None:
            self.generate_device_id()
        self.client_version = __version__
        from nxdrive.engine.engine import Engine
        self._engine_types["NXDRIVE"] = Engine
        self.load()
        # Setup analytics tracker
        if self.get_tracking():
            self._create_tracker()

    def is_debug(self):
        return self._debug

    def get_device_id(self):
        return self.device_id

    def _create_tracker(self):
        from nxdrive.engine.tracker import Tracker
        self._tracker = Tracker(self)
        # Start the tracker when we launch
        self.started.connect(self._tracker._thread.start)
        return self._tracker

    def get_tracker(self):
        return self._tracker

    def _get_db(self):
        return os.path.join(self.nxdrive_home, "manager.db")

    def _create_dao(self):
        from nxdrive.engine.dao.sqlite import ManagerDAO
        return ManagerDAO(self._get_db())

    def resume(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Resume engine %s", uid)
            engine.resume()

    def suspend(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Suspend engine %s", uid)
            engine.suspend()

    def stop(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Stop engine %s", uid)
            engine.stop()

    def start(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Launch engine %s", uid)
            if not self.is_debug():
                engine.start()
        log.debug("Emitting started")
        self.started.emit()

    def load(self):
        if self._engine_definitions is None:
            self._engine_definitions = self._dao.get_engines()
        in_error = dict()
        self._engines = dict()
        for engine in self._engine_definitions:
            if not engine.engine in self._engine_types:
                log.warn("Can't find engine %s anymore", engine.engine)
                if not engine.engine in in_error:
                    in_error[engine.engine] = True
                    self.engineNotFound.emit(engine)
            self._engines[engine.uid] = self._engine_types[engine.engine](self, engine)

    def _get_default_nuxeo_drive_name(self):
        return 'Nuxeo Drive'

    def _get_default_nuxeo_drive_folder(self):
        """Find a reasonable location for the root Nuxeo Drive folder

        This folder is user specific, typically under the home folder.

        Under Windows, try to locate My Documents as a home folder, using the
        win32com shell API if allowed, else falling back on a manual detection.

        Note that we need to decode the path returned by os.path.expanduser with
        the local encoding because the value of the HOME environment variable is
        read as a byte string. Using os.path.expanduser(u'~') fails if the home
        path contains non ASCII characters since Unicode coercion attempts to
        decode the byte string as an ASCII string.
        """
        if sys.platform == "win32":<commit_msg>NXDRIVE-45: Add an initEngine signal to simplify engine connection<commit_after>from PyQt4.QtCore import QObject, pyqtSignal, QCoreApplication
from nxdrive.utils import encrypt
from nxdrive.utils import decrypt
from nxdrive.logging_config import get_logger, configure
from nxdrive.client.base_automation_client import get_proxies_for_handler
from nxdrive.utils import normalized_path
from nxdrive import __version__
import subprocess
from nxdrive.utils import ENCODING
import os
import uuid
import sys
log = get_logger(__name__)


try:
    # Set the cffi tmp path to lib for xattr
    if sys.platform != "win32":
        from cffi.verifier import set_tmpdir
        import nxdrive
        nxdrive_path = os.path.dirname(nxdrive.__file__)
        nxdrive_path = os.path.dirname(nxdrive_path)
        nxdrive_path = os.path.dirname(nxdrive_path)
        lib_path = os.path.join(nxdrive_path, 'lib-dynload')
        set_tmpdir(lib_path)
except Exception as e:
    pass

class EngineTypeMissing(Exception):
    pass

class MissingToken(Exception):
    pass


class GeneralSettings(object):
    """Summarize general settings"""

    def __init__(self, auto_update=False):
        self.auto_update = auto_update

    def __repr__(self):
        return "GeneralSettings<auto_update=%r>" % self.auto_update


class ServerBindingSettings(object):
    """Summarize server binding settings"""

    def __init__(self, server_url=None, server_version=None,
                 username=None, password=<PASSWORD>,
                 local_folder=None, initialized=False,
                 pwd_update_required=False):
        self.server_url = server_url
        self.server_version = server_version
        self.username = username
        self.password = password
        self.local_folder = local_folder
        self.initialized = initialized
        self.pwd_update_required = pwd_update_required

    def __repr__(self):
        return ("ServerBindingSettings<server_url=%s, server_version=%s, "
                "username=%s, local_folder=%s, initialized=%r, "
                "pwd_update_required=%r>") % (
                    self.server_url, self.server_version, self.username,
                    self.local_folder, self.initialized,
                    self.pwd_update_required)


class ProxySettings(object):
    """Summarize HTTP proxy settings"""

    def __init__(self, dao=None, config='System', proxy_type=None,
                 server=None, port=None,
                 authenticated=False, username=None, password=<PASSWORD>,
                 exceptions=None):
        self.config = config
        self.proxy_type = proxy_type
        self.server = server
        self.port = port
        self.authenticated = authenticated
        self.username = username
        self.password = password
        self.exceptions = exceptions
        if dao is not None:
            self.load(dao)

    def save(self, dao, token = None):
        # Encrypt password with token as the secret
        if token is None:
            token = dao.get_config("device_id", None)
        if token is None:
            raise MissingToken("Your token has been revoked,"
                        " please update your password to acquire a new one.")
        token = token + "_proxy"
        password = encrypt(self.password, token)
        dao.update_config("proxy_password", password)
        dao.update_config("proxy_username", self.username)
        dao.update_config("proxy_exceptions", self.exceptions)
        dao.update_config("proxy_port", self.port)
        dao.update_config("proxy_type", self.proxy_type)
        dao.update_config("proxy_config", self.config)
        dao.update_config("proxy_server", self.server)
        dao.update_config("proxy_authenticated", self.authenticated)

    def load(self, dao, token=None):
        self.config = dao.get_config("proxy_config", "System")
        self.proxy_type = dao.get_config("proxy_type", None)
        self.port = dao.get_config("proxy_port", None)
        self.server = dao.get_config("proxy_server", None)
        self.username = dao.get_config("proxy_username", None)
        password = dao.get_config("proxy_password", None)
        self.exceptions = dao.get_config("proxy_exceptions", None)
        self.authenticated = (dao.get_config("proxy_authenticated", "0") == "1")
        if token is None:
            token = dao.get_config("device_id", None)
        if password is not None and token is not None:
            token = token + "_proxy"
            self.password = decrypt(password, token)
        else:
            # If no server binding or no token available
            # (possibly after token revocation) reset password
            self.password = ''

    def __repr__(self):
        return ("ProxySettings<config=%s, proxy_type=%s, server=%s, port=%s, "
                "authenticated=%r, username=%s, exceptions=%s>") % (
                    self.config, self.proxy_type, self.server, self.port,
                    self.authenticated, self.username, self.exceptions)


class Manager(QObject):
    '''
    classdocs
    '''
    proxyUpdated = pyqtSignal(object)
    clientUpdated = pyqtSignal(object, object)
    engineNotFound = pyqtSignal(object)
    newEngine = pyqtSignal(object)
    dropEngine = pyqtSignal(object)
    initEngine = pyqtSignal(object)
    started = pyqtSignal()
    _singleton = None

    @staticmethod
    def get():
        return Manager._singleton

    def __init__(self, options):
        '''
        Constructor
        '''
        if Manager._singleton is not None:
            raise Exception("Only one instance of Manager can be create")
        Manager._singleton = self
        super(Manager, self).__init__()
        self.nxdrive_home = os.path.expanduser(options.nxdrive_home)
        self._debug = options.debug
        self._dao = self._create_dao()
        self.proxies = None
        self.proxy_exceptions = None
        self._engines = None
        self._engine_definitions = None
        self._engine_types = dict()
        self.device_id = self._dao.get_config("device_id")
        self.updated = False#self.update_version()
        if self.device_id is None:
            self.generate_device_id()
        self.client_version = __version__
        from nxdrive.engine.engine import Engine
        self._engine_types["NXDRIVE"] = Engine
        self.load()
        # Setup analytics tracker
        if self.get_tracking():
            self._create_tracker()

    def is_debug(self):
        return self._debug

    def get_device_id(self):
        return self.device_id

    def _create_tracker(self):
        from nxdrive.engine.tracker import Tracker
        self._tracker = Tracker(self)
        # Start the tracker when we launch
        self.started.connect(self._tracker._thread.start)
        return self._tracker

    def get_tracker(self):
        return self._tracker

    def _get_db(self):
        return os.path.join(self.nxdrive_home, "manager.db")

    def _create_dao(self):
        from nxdrive.engine.dao.sqlite import ManagerDAO
        return ManagerDAO(self._get_db())

    def resume(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Resume engine %s", uid)
            engine.resume()

    def suspend(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Suspend engine %s", uid)
            engine.suspend()

    def stop(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Stop engine %s", uid)
            engine.stop()

    def start(self, euid=None):
        for uid, engine in self._engines.items():
            if euid is not None and euid != uid:
                continue
            log.debug("Launch engine %s", uid)
            if not self.is_debug():
                engine.start()
        log.debug("Emitting started")
        self.started.emit()

    def load(self):
        if self._engine_definitions is None:
            self._engine_definitions = self._dao.get_engines()
        in_error = dict()
        self._engines = dict()
        for engine in self._engine_definitions:
            if not engine.engine in self._engine_types:
                log.warn("Can't find engine %s anymore", engine.engine)
                if not engine.engine in in_error:
                    in_error[engine.engine] = True
                    self.engineNotFound.emit(engine)
            self._engines[engine.uid] = self._engine_types[engine.engine](self, engine)
            self.initEngine.emit(self._engines[engine.uid])

    def _get_default_nuxeo_drive_name(self):
        return 'Nuxeo Drive'

    def _get_default_nuxeo_drive_folder(self):
        """Find a reasonable location for the root Nuxeo Drive folder

        This folder is user specific, typically under the home folder.

        Under Windows, try to locate My Documents as a home folder, using the
        win32com shell API if allowed, else falling back on a manual detection.

        Note that we need to decode the path returned by os.path.expanduser with
        the local encoding because the value of the HOME environment variable is
        read as a byte string. Using os.path.expanduser(u'~') fails if the home
        path contains non ASCII characters since Unicode coercion attempts to
        decode the byte string as an ASCII string.
        """
        if sys.platform == "win32":
{'fraction_non_alphanumeric': 0.0893371757925072, 'fraction_numerical': 0.001047943411055803, 'mean_word_length': 2.3972944108223566, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1066104', 'n_tokens_mistral': 5437, 'n_tokens_neox': 5290, 'n_words': 1400}