<commit_before>'''
Created on 28 janv. 2015

@author: <NAME>

TODO: Find a better way for the try/catch on slot
'''
from PyQt4 import QtGui, QtCore, QtWebKit
from nxdrive.logging_config import get_logger
from nxdrive.engine.activity import FileAction
from nxdrive.client.base_automation_client import Unauthorized
from nxdrive.wui.translator import Translator
from nxdrive.manager import FolderAlreadyUsed
import urllib2
import json
import time
import datetime
log = get_logger(__name__)


class WebDriveApi(QtCore.QObject):

    def __init__(self, application, dlg=None):
        super(WebDriveApi, self).__init__()
        self._manager = application.manager
        self._application = application
        self._dialog = dlg

    def _json(self, obj):
        return json.dumps(obj)

    def set_dialog(self, dlg):
        self._dialog = dlg

    def _export_engine(self, engine):
        result = dict()
        result["uid"] = engine._uid
        result["type"] = engine._type
        result["name"] = engine._name
        result["metrics"] = engine.get_metrics()
        result["started"] = engine.is_started()
        result["syncing"] = engine.is_syncing()
        result["paused"] = engine.is_paused()
        result["local_folder"] = engine._local_folder
        result["queue"] = engine.get_queue_manager().get_metrics()
        # TODO Make it more generic
        bind = engine.get_binder()
        result["server_url"] = bind.server_url
        result["username"] = bind.username
        result["need_password_update"] = bind.pwd_update_required
        result["initialized"] = bind.initialized
        result["server_version"] = bind.server_version
        result["threads"] = self._get_threads(engine)
        return result

    def get_date_from_sqlite(self, d):
        if d is None:
            return 0
        format_date = "%Y-%m-%d %H:%M:%S"
        return datetime.datetime.strptime(str(d.split(".")[0]), format_date)

    def get_timestamp_from_date(self, d):
        return int(time.mktime(d.timetuple()))

    def get_timestamp_from_sqlite(self, d):
        return int(time.mktime(self.get_date_from_sqlite(d).timetuple()))

    def _export_state(self, state):
        if state is None:
            return None
        result = dict()
        # Direction
        result["state"] = state.pair_state
        # Last sync in sec
        try:
            current_time = int(time.time())
            date_time = self.get_date_from_sqlite(state.last_sync_date)
            sync_time = self.get_timestamp_from_date(date_time)
            if state.last_local_updated > state.last_remote_updated:
                result["last_sync_direction"] = "download"
            else:
                result["last_sync_direction"] = "upload"
            result["last_sync"] = current_time - sync_time
            result["last_sync_date"] = Translator.format_datetime(date_time)
        except Exception as e:
            log.exception(e)
        result["name"] = state.local_name
        if state.local_name is None:
            result["name"] = state.remote_name
        result["last_error"] = state.last_error
        result["local_path"] = state.local_path
        result["remote_ref"] = state.remote_ref
        result["id"] = state.id
        return result
<commit_msg>NXDRIVE-205: Fix None ValueErrors in debug Window<commit_after>'''
Created on 28 janv. 2015

@author: <NAME>

TODO: Find a better way for the try/catch on slot
'''
from PyQt4 import QtGui, QtCore, QtWebKit
from nxdrive.logging_config import get_logger
from nxdrive.engine.activity import FileAction
from nxdrive.client.base_automation_client import Unauthorized
from nxdrive.wui.translator import Translator
from nxdrive.manager import FolderAlreadyUsed
import urllib2
import json
import time
import datetime
log = get_logger(__name__)


class WebDriveApi(QtCore.QObject):

    def __init__(self, application, dlg=None):
        super(WebDriveApi, self).__init__()
        self._manager = application.manager
        self._application = application
        self._dialog = dlg

    def _json(self, obj):
        return json.dumps(obj)

    def set_dialog(self, dlg):
        self._dialog = dlg

    def _export_engine(self, engine):
        result = dict()
        result["uid"] = engine._uid
        result["type"] = engine._type
        result["name"] = engine._name
        result["metrics"] = engine.get_metrics()
        result["started"] = engine.is_started()
        result["syncing"] = engine.is_syncing()
        result["paused"] = engine.is_paused()
        result["local_folder"] = engine._local_folder
        result["queue"] = engine.get_queue_manager().get_metrics()
        # TODO Make it more generic
        bind = engine.get_binder()
        result["server_url"] = bind.server_url
        result["username"] = bind.username
        result["need_password_update"] = bind.pwd_update_required
        result["initialized"] = bind.initialized
        result["server_version"] = bind.server_version
        result["threads"] = self._get_threads(engine)
        return result

    def get_date_from_sqlite(self, d):
        if d is None:
            return 0
        format_date = "%Y-%m-%d %H:%M:%S"
        return datetime.datetime.strptime(str(d.split(".")[0]), format_date)

    def get_timestamp_from_date(self, d):
        if d == 0:
            return 0
        return int(time.mktime(d.timetuple()))

    def get_timestamp_from_sqlite(self, d):
        return int(time.mktime(self.get_date_from_sqlite(d).timetuple()))

    def _export_state(self, state):
        if state is None:
            return None
        result = dict()
        # Direction
        result["state"] = state.pair_state
        # Last sync in sec
        try:
            current_time = int(time.time())
            date_time = self.get_date_from_sqlite(state.last_sync_date)
            sync_time = self.get_timestamp_from_date(date_time)
            if state.last_local_updated > state.last_remote_updated:
                result["last_sync_direction"] = "download"
            else:
                result["last_sync_direction"] = "upload"
            result["last_sync"] = current_time - sync_time
            if date_time == 0:
                result["last_sync_date"] = ""
            else:
                result["last_sync_date"] = Translator.format_datetime(date_time)
        except Exception as e:
            log.exception(e)
        result["name"] = state.local_name
        if state.local_name is None:
            result["name"] = state.remote_name
        result["last_error"] = state.last_error
        result["local_path"] = state.local_path
        result["remote_ref"] = state.remote_ref
        result["id"] = state.id
        return result

{'fraction_non_alphanumeric': 0.106044853705629, 'fraction_numerical': 0.0038615773058072182, 'mean_word_length': 2.667755991285403, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2221043', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1959, 'n_words': 432}