123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- from PyQt4 import QtCore
- class StorageError(Exception):
- """
- Simple Storage Error Base Class
- """
- pass
- class Storage(object):
- """
- Simple Class to store Values and Throw an error if a value is retrieved that has not been stored before.
- """
- def __init__(self, default=None):
- self._storage = {}
- if default:
- self._storage = default
- def setdefault(self, key, object):
- """
- Call setdefault on the underlying dictionary and return the result
- :param key: the key to call setdefault with
- :param object: the object to call setdefault with
- :return: the return value of setdefault of the dictionary
- """
- return self._storage.setdefault(key, object)
- def __setattr__(self, key, value):
- if key == '_storage':
- super(Storage, self).__setattr__(key, value)
- else:
- self._storage[key] = value
- def __getattr__(self, item):
- if item == '_storage':
- return super(Storage, self).__getattr__(item)
- if item in self._storage:
- return self._storage[item]
- else:
- raise StorageError("'"+item+"' Not saved in Storage")
- storage = Storage() # Container Object to use storage globally
- _board_specific_storages = {}
- def get_board_specific_storage(board_id):
- """
- Get the storage instance for a specific board
- :param board_id: the board to get the storage for
- :return: the instance of the storage for the given board
- """
- if board_id in _board_specific_storages:
- return _board_specific_storages[board_id]
- else:
- _board_specific_storages[board_id] = Storage()
- return _board_specific_storages[board_id]
- class ThreadStorage(object):
- """
- Wrapper for QThreads
- """
- def __init__(self):
- self._q_thread = None
- self._threaded_object = None
- self.running = False
- def register(self, threaded_object):
- """
- Register a class object to run in a QThread
- :param threaded_object: the class object
- :return:
- """
- del self._q_thread # start over
- self._q_thread = QtCore.QThread()
- self._q_thread.started.connect(self.__start)
- self._threaded_object = threaded_object
- def is_registered(self):
- """
- Check if a class for this thread is registered.
- :return:
- """
- return not (self._threaded_object is None)
- def __start(self):
- """
- This will be registered to the QThread started signal and will call the actual start_method
- :return:
- """
- self._start_method()
- def start(self, method_to_run_in_thread):
- """
- Move the class object to a QThread, connect the QThread.started signal to the given method of the
- class object
- :param method_to_run_in_thread: the method to start when starting the QThread
- :return:
- """
- self._threaded_object.moveToThread(self._q_thread)
- if isinstance(method_to_run_in_thread, str):
- self._start_method = getattr(self._threaded_object, method_to_run_in_thread)
- # self._q_thread.started.connect(getattr(self._threaded_object, method_to_run_in_thread))
- else:
- self._start_method = method_to_run_in_thread
- # self._q_thread.started.connect(method_to_run_in_thread)
- self._q_thread.start()
- self.running = True
- def quit(self):
- """
- Quit the thread class (if it has a method to quit)
- :return:
- """
- if hasattr(self._threaded_object, 'quit'):
- self._threaded_object.quit()
- def stop(self):
- """
- Call quit and wait to the underlying QThread
- :return:
- """
- self._q_thread.quit()
- self._q_thread.wait()
- self.running = False
- def __getattr__(self, item):
- """
- Get Attributes of the registered class object
- :param item: the item to get from the class object
- :return: the attribute of the class object
- """
- if not item.startswith('obj_'):
- raise AttributeError("If this was a call to an object of the threaded object, the call has to start with"
- "obj_.")
- if self._threaded_object is None:
- raise AttributeError("This Thread object does not have a threaded object. Register one first")
- return getattr(self._threaded_object, item[4:])
- def connect(self, signal, slot):
- """
- Connect signals of the class object to the given slots
- :param signal: the signal
- :param slot: the slot
- :return:
- """
- if isinstance(signal, str):
- getattr(self._threaded_object, signal).connect(slot)
- else:
- signal.connect(slot)
- def disconnect(self, *args):
- """
- Disconnect the given signals
- :param args: the signals to disconnect
- :return:
- """
- for signal in args:
- getattr(self._threaded_object, signal).disconnect()
- def init(self, *args, **kwargs):
- """
- call the threaded objects init method (not __init__) with the passed arguments
- :return:
- """
- self._threaded_object.init(*args, **kwargs)
|