Exemple #1
0
 def __init__(self, *args, **kwargs):
     QThread.__init__(self, *args, **kwargs)
     self.app = AppClass.instance()
     self.status = STATUS_NONE
     self.last_sync = datetime.now()
     self.update_count = 0
     self.timer = QTimer()
     self.timer.timeout.connect(self.sync)
     self.update_timer()
     self.wait_condition = QWaitCondition()
     self.mutex = QMutex()
Exemple #2
0
class SyncThread(QThread):
    """Sync notes with evernote thread"""
    force_sync_signal = Signal()
    sync_state_changed = Signal(int)

    def __init__(self, app, *args, **kwargs):
        QThread.__init__(self, *args, **kwargs)
        self.app = app
        self.status = STATUS_NONE
        self.last_sync = datetime.now()
        self.timer = QTimer()
        self.timer.timeout.connect(self.sync)
        self.update_timer()
        self.wait_condition = QWaitCondition()
        self.mutex = QMutex()

    def update_timer(self):
        self.timer.stop()
        delay = int(self.app.settings.value('sync_delay') or 0) or DEFAULT_SYNC_DELAY
        if delay != SYNC_MANUAL:
            self.timer.start(delay)

    def run(self):
        self.session = get_db_session()
        self.sq = self.session.query
        self.auth_token = get_auth_token()
        self.note_store = get_note_store(self.auth_token)
        self.perform()
        while True:
            self.mutex.lock()
            self.wait_condition.wait(self.mutex)
            self.perform()
            self.mutex.unlock()

    def force_sync(self):
        self.timer.stop()
        self.sync()
        self.update_timer()

    @Slot()
    def sync(self):
        self.wait_condition.wakeAll()

    def perform(self):
        """Perform all sync"""
        self.status = STATUS_SYNC
        self.last_sync = datetime.now()
        self.sync_state_changed.emit(SYNC_STATE_START)
        try:
            self.local_changes()
            self.remote_changes()
        except Exception, e:  # maybe log this
            self.session.rollback()
        finally:
Exemple #3
0
 def __init__(self, controller, model):
     """
     This is the constructor of the class
     :param file: The json file where the packets are defined
     :param model: The model of the application
     """
     QThread.__init__(self)
     self.model = model
     self.controller = controller
     self.add_table_signal = AddTableSignal()
     self.add_table_signal.signal.connect(self.model.add)
     self.filter_table_signal = AddTableSignal()
     self.filter_table_signal.signal.connect(
         self.controller.open_filter_callback)
     self.json_file = None
     self.timer = QTimer(self)
     self.timer.timeout.connect(self.update_packets)
     self.json_file_loaded = QWaitCondition()
     self.mutex = QMutex()
Exemple #4
0
 def __init__(self, app, *args, **kwargs):
     QThread.__init__(self, *args, **kwargs)
     self.app = app
     self.status = STATUS_NONE
     self.last_sync = datetime.now()
     self.timer = QTimer()
     self.timer.timeout.connect(self.sync)
     self.update_timer()
     self.wait_condition = QWaitCondition()
     self.mutex = QMutex()
Exemple #5
0
class SyncThread(QThread):
    """Sync notes with evernote thread"""
    force_sync_signal = Signal()
    sync_state_changed = Signal(int)
    data_changed = Signal()

    def __init__(self, app, *args, **kwargs):
        QThread.__init__(self, *args, **kwargs)
        self.app = app
        self.status = STATUS_NONE
        self.last_sync = datetime.now()
        self.update_count = 0
        self.timer = QTimer()
        self.timer.timeout.connect(self.sync)
        self.update_timer()
        self.wait_condition = QWaitCondition()
        self.mutex = QMutex()

    def update_timer(self):
        self.timer.stop()
        delay = int(self.app.settings.value('sync_delay') or 0) or DEFAULT_SYNC_DELAY
        if delay != SYNC_MANUAL:
            self.timer.start(delay)

    def run(self):
        self.init_db()
        self.init_network()
        while True:
            self.mutex.lock()
            self.wait_condition.wait(self.mutex)
            self.perform()
            self.mutex.unlock()
            time.sleep(1)  # prevent cpu eating

    def init_db(self):
        self.session = get_db_session()
        self.sq = self.session.query

    def init_network(self):
        while True:
            try:
                self.auth_token = get_auth_token()
                self.note_store = get_note_store(self.auth_token)
                break
            except socket.error:
                time.sleep(30)

    def force_sync(self):
        self.timer.stop()
        self.sync()
        self.update_timer()

    @Slot()
    def sync(self):
        self.wait_condition.wakeAll()

    def perform(self):
        """Perform all sync"""
        self.status = STATUS_SYNC
        self.last_sync = datetime.now()
        self.sync_state_changed.emit(SYNC_STATE_START)
        if self._need_to_update():
            self.need_to_update = True
            self.all_notes = list(self._iter_all_notes())
        try:
            if self.need_to_update:
                self.remote_changes()
            self.local_changes()
        except Exception, e:  # maybe log this
            self.session.rollback()
            self.init_db()
            self.app.log(e)
        finally:
Exemple #6
0
class SyncThread(QThread, SyncAgent):
    """Sync notes with evernote thread"""
    force_sync_signal = Signal()
    sync_state_changed = Signal(int)
    data_changed = Signal()

    def __init__(self, *args, **kwargs):
        QThread.__init__(self, *args, **kwargs)
        self.app = AppClass.instance()
        self.status = STATUS_NONE
        self.last_sync = datetime.now()
        self.update_count = 0
        self.timer = QTimer()
        self.timer.timeout.connect(self.sync)
        self.update_timer()
        self.wait_condition = QWaitCondition()
        self.mutex = QMutex()

    def update_timer(self):
        self.timer.stop()
        delay = int(self.app.settings.value('sync_delay')
                    or 0) or DEFAULT_SYNC_DELAY
        if delay != SYNC_MANUAL:
            self.timer.start(delay)

    def run(self):
        self.init_db()
        self.init_network()
        while True:
            self.mutex.lock()
            self.wait_condition.wait(self.mutex)
            self.perform()
            self.mutex.unlock()
            time.sleep(1)  # prevent cpu eating

    def init_db(self):
        self.session = get_db_session()
        self.sq = self.session.query

    def init_network(self):
        while True:
            try:
                self.auth_token = get_auth_token()
                self.note_store = get_note_store(self.auth_token)
                break
            except socket.error:
                time.sleep(30)

    def force_sync(self):
        self.timer.stop()
        self.sync()
        self.update_timer()

    @Slot()
    def sync(self):
        self.wait_condition.wakeAll()

    def perform(self):
        """Perform all sync"""
        self.status = STATUS_SYNC
        self.last_sync = datetime.now()
        self.sync_state_changed.emit(SYNC_STATE_START)
        if self._need_to_update():
            self.need_to_update = True
            self.all_notes = list(self._iter_all_notes())
        try:
            if self.need_to_update:
                self.remote_changes()
            self.local_changes()
            self.sharing_changes()
        except Exception, e:  # maybe log this
            self.session.rollback()
            self.init_db()
            self.app.log(e)
        finally:
Exemple #7
0
class PusThread(QThread):
    """
    This class overrides a QThread object to be able to
    define custom safe threads with custom signals. This
    class is used to be able to make a simulation of a connection
    with the robot where the packets arrives with an interval
    between them without blocking the rest of the functionality
    of the application. The simulation is done by reading the
    packets from a json file.

    """
    def __init__(self, controller, model):
        """
        This is the constructor of the class
        :param file: The json file where the packets are defined
        :param model: The model of the application
        """
        QThread.__init__(self)
        self.model = model
        self.controller = controller
        self.add_table_signal = AddTableSignal()
        self.add_table_signal.signal.connect(self.model.add)
        self.filter_table_signal = AddTableSignal()
        self.filter_table_signal.signal.connect(
            self.controller.open_filter_callback)
        self.json_file = None
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_packets)
        self.json_file_loaded = QWaitCondition()
        self.mutex = QMutex()

    def run(self):
        """
        This method runs the thread reading from
        the json defined and making an sleep of T
        seconds between packet and packet according
        to the interval defined in the json file.
        """

        self.timer.start(1000)

        while True:
            self.mutex.lock()
            self.json_file_loaded.wait(self.mutex)
            file = self.json_file
            self.mutex.unlock()

            pt = PacketTranslator()
            mt = MakoTranslate()
            if file is not None:
                with open(file) as jfile:
                    jsondata = mt.replace(jfile.read())
                    activities = json.loads(jsondata)[TestTags.ACTIVITIES_TAG]
                    for activity in activities:
                        interval = activity[TestTags.INTERVAL_TAG]
                        self.sleep(interval)
                        if TestTags.PACKET_TAG in activity:
                            packet = pt.json2packet(
                                activity[TestTags.PACKET_TAG])
                            pb.pus_notify_sendPacket(packet)
                            self.add_table_signal.throw(packet)
                        elif TestTags.ACTIONS_TAG in activity:
                            if activity[TestTags.
                                        ACTIONS_TAG] == TestTags.SAVEDB_TAG:
                                file = activity[TestTags.PARAMS_TAG]

                                d = Database(file)
                                d.create_dump_table()
                                packages = [
                                    tuple(e[1:-1]) for e in self.model.table
                                ]

                                d.insert_db(
                                    "INSERT INTO packages VALUES(?,?,?,?,?,?,?,?,?,?)",
                                    packages)

                            elif activity[
                                    TestTags.
                                    ACTIONS_TAG] == TestTags.SETFILTER_TAG:
                                filter_ = FilterModel()
                                type_ = activity[TestTags.PARAMS_TAG]["type"]
                                svc = activity[TestTags.PARAMS_TAG]["svc"]
                                msg = activity[TestTags.PARAMS_TAG]["msg"]
                                filter_.set_filter_options(type_, svc, msg)
                                filter_index = self.model.set_filter(
                                    filter_.get_filter_options())
                                self.filter_table_signal.throw(filter_index)
            self.mutex.lock()
            self.json_file = None
            self.mutex.unlock()

    def update_packets(self):
        for _ in range(10):
            packet = pb.pusPacket_t()
            if pb.pusError_t.PUS_NO_ERROR == pb.pus_notify_readTm(
                    packet):  # Comprobar si null
                self.add_table_signal.throw(packet)

    def load_test(self, json_file):
        self.mutex.lock()
        self.json_file = json_file
        if json_file is not None:
            self.json_file_loaded.wakeAll()
        self.mutex.unlock()