def _store_key(self, obj, interval, idstring=""): """ Tries to create a store_key for the object. Returns a tuple (isdb, store_key) where isdb is a boolean True if obj was a database object, False otherwise. Args: obj (Object): Subscribing object. interval (int): Ticker interval idstring (str, optional): Additional separator between different subscription types. """ if hasattr(obj, "db_key"): # create a store_key using the database representation objkey = pack_dbobj(obj) isdb = True else: # non-db object, look for a property "key" on it, otherwise # use its memory location. try: objkey = _GA(obj, "key") except AttributeError: objkey = id(obj) isdb = False # return sidb and store_key return isdb, (objkey, interval, idstring)
def _store_key(self, obj, path, interval, callfunc, idstring="", persistent=True): """ Tries to create a store_key for the object. Args: obj (Object, tuple or None): Subscribing object if any. If a tuple, this is a packed_obj tuple from dbserialize. path (str or None): Python-path to callable, if any. interval (int): Ticker interval. callfunc (callable or str): This is either the callable function or the name of the method to call. Note that the callable is never stored in the key; that is uniquely identified with the python-path. idstring (str, optional): Additional separator between different subscription types. persistent (bool, optional): If this ticker should survive a system shutdown or not. Returns: store_key (tuple): A tuple `(packed_obj, methodname, outpath, interval, idstring, persistent)` that uniquely identifies the ticker. Here, `packed_obj` is the unique string representation of the object or `None`. The `methodname` is the string name of the method on `packed_obj` to call, or `None` if `packed_obj` is unset. `path` is the Python-path to a non-method callable, or `None`. Finally, `interval` `idstring` and `persistent` are integers, strings and bools respectively. """ interval = int(interval) persistent = bool(persistent) packed_obj = pack_dbobj(obj) methodname = callfunc if callfunc and isinstance(callfunc, basestring) else None outpath = path if path and isinstance(path, basestring) else None return (packed_obj, methodname, outpath, interval, idstring, persistent)
def _add_monitor(self, obj, sessid, fieldname, oobfuncname, *args, **kwargs): """ Helper method. Creates a fieldmonitor and store it on the object. This tracker will be updated whenever the given field changes. Args: obj (Object): The object on which to store the monitor. sessid (int): The Session id associated with the monitor. fieldname (str): The field to monitor oobfuncname (str): The OOB callback function to trigger when field `fieldname` changes. args, kwargs (any): Arguments to pass on to the callback. """ fieldmonitorname = self._get_fieldmonitor_name(fieldname) if not hasattr(obj, fieldmonitorname): # assign a new fieldmonitor to the object _SA(obj, fieldmonitorname, OOBFieldMonitor(obj)) # register the session with the monitor _GA(obj, fieldmonitorname).add(sessid, oobfuncname, *args, **kwargs) # store calling arguments as a pickle for retrieval at reload storekey = (pack_dbobj(obj), sessid, fieldname, oobfuncname) stored = (args, kwargs) self.oob_monitor_storage[storekey] = stored
def _remove_monitor(self, obj, sessid, fieldname, oobfuncname=None): """ Remove the OOB from obj. If oob implements an at_delete hook, this will be called with args, kwargs """ fieldmonitorname = self._get_fieldmonitor_name(fieldname) try: _GA(obj, fieldmonitorname).remove(sessid, oobfuncname=oobfuncname) if not _GA(obj, fieldmonitorname).subscribers: _DA(obj, fieldmonitorname) except AttributeError: pass # remove the pickle from storage store_key = (pack_dbobj(obj), sessid, fieldname, oobfuncname) self.oob_monitor_storage.pop(store_key, None)
def _add_monitor(self, obj, sessid, fieldname, oobfuncname, *args, **kwargs): """ Create a fieldmonitor and store it on the object. This tracker will be updated whenever the given field changes. """ fieldmonitorname = self._get_fieldmonitor_name(fieldname) if not hasattr(obj, fieldmonitorname): # assign a new fieldmonitor to the object _SA(obj, fieldmonitorname, OOBFieldMonitor(obj)) # register the session with the monitor _GA(obj, fieldmonitorname).add(sessid, oobfuncname, *args, **kwargs) # store calling arguments as a pickle for retrieval at reload storekey = (pack_dbobj(obj), sessid, fieldname, oobfuncname) stored = (args, kwargs) self.oob_monitor_storage[storekey] = stored
def _remove_monitor(self, obj, sessid, fieldname, oobfuncname=None): """ Helper method. Removes the OOB from obj. Args: obj (Object): The object from which to remove the monitor. sessid (int): The Session id associated with the monitor. fieldname (str): The monitored field from which to remove the monitor. oobfuncname (str): The oob callback function. """ fieldmonitorname = self._get_fieldmonitor_name(fieldname) try: _GA(obj, fieldmonitorname).remove(sessid, oobfuncname=oobfuncname) if not _GA(obj, fieldmonitorname).subscribers: _DA(obj, fieldmonitorname) except AttributeError: pass # remove the pickle from storage store_key = (pack_dbobj(obj), sessid, fieldname, oobfuncname) self.oob_monitor_storage.pop(store_key, None)
def _store_key(self, obj, interval, idstring=""): """ Tries to create a store_key for the object. Returns a tuple (isdb, store_key) where isdb is a boolean True if obj was a database object, False otherwise. """ if hasattr(obj, "db_key"): # create a store_key using the database representation objkey = pack_dbobj(obj) isdb = True else: # non-db object, look for a property "key" on it, otherwise # use its memory location. try: objkey = _GA(obj, "key") except AttributeError: objkey = id(obj) isdb = False # return sidb and store_key return isdb, (objkey, interval, idstring)
def dbsafe_encode(value, compress_object=False, pickle_protocol=DEFAULT_PROTOCOL): # We use deepcopy() here to avoid a problem with cPickle, where dumps # can generate different character streams for same lookup value if # they are referenced differently. # The reason this is important is because we do all of our lookups as # simple string matches, thus the character streams must be the same # for the lookups to work properly. See tests.py for more information. try: value = deepcopy(value) except CopyError: # this can happen on a manager query where the search query string is a # database model. value = pack_dbobj(value) value = dumps(value, protocol=pickle_protocol) if compress_object: value = compress(value) value = b64encode(value).decode() # decode bytes to str return PickledObject(value)