def change_class(self, obj, newclass, **kw): """ Change the class of the object. This process will actually instantiate a new object of ``newclass`` and copy the state from ``obj`` to the new object. Finally, the new object will replace ``obj`` in the database. :param obj: The object whose class to change. :param newclass: The new parent class of the object. :param kw: The keyword arguments to pass the the new object init. """ invoke_hook(obj, "before_class_changed", newclass=newclass, **kw) obj = obj._real_object() newobj = newclass(**kw) newobj.__dict__.update(obj.__dict__) if isinstance(obj, PersistentSlots): slots = set() for c in obj.__class__.__mro__: if '__slots__' in c.__dict__: slots.update(c.__slots__) for slot in slots: if slot in obj.__dict__: continue try: slot_val = getattr(obj, slot) setattr(newobj, slot, slot_val) except AttributeError: continue self.unregister_object(obj) self.register_object(newobj, force_id=obj.obj_id) invoke_hook(newobj, "after_class_changed", oldclass=obj.__class__, **kw) return newobj.ref()
def on_server_shutdown(self): """ Run just prior to server shutdown. """ for obj in self.objects.itervalues(): invoke_hook(obj, 'server_shutdown') for task in self.tasks.itervalues(): task.server_shutdown()
def on_server_startup(self): """ Run once per server start after everything is loaded and ready. """ for task in self.tasks.itervalues(): task.server_startup() for obj in self.objects.itervalues(): invoke_hook(obj, 'server_startup')
def create(cls, **kwargs): """ Create an instance of the class and register it with the database. @param cls: The class to instantiate. @param kwargs: Any initialization arguments to pass to the class. @return: A reference to the new object. """ if cls.db is None: raise Exception("Database not available to %s" % cls.__name__) obj = cls(**kwargs) cls.db.register_object(obj) invoke_hook(obj, 'after_created') return obj.ref()
def delete(self): """ Canonical method for deleting an object. """ invoke_hook(self, 'before_deleted') self.db.unregister_object(self)