Added weak reverse references from all handlers.

This commit is contained in:
Griatch 2014-05-10 17:12:49 +02:00
parent 4e3789cede
commit 0030530021
6 changed files with 20 additions and 22 deletions

View file

@ -179,7 +179,7 @@ class CmdSetHandler(object):
self.permanent_paths = [""] self.permanent_paths = [""]
if init_true: if init_true:
self.update(init_mode=True) #is then called from the object __init__ self.update(init_mode=True) #is then called from the object __init__.
def __str__(self): def __str__(self):
"Display current commands" "Display current commands"

View file

@ -7,7 +7,7 @@ All commands in Evennia inherit from the 'Command' class in this module.
import re import re
from src.locks.lockhandler import LockHandler from src.locks.lockhandler import LockHandler
from src.utils.utils import is_iter, fill from src.utils.utils import is_iter, fill, LazyLoadHandler
def _init_command(mcs, **kwargs): def _init_command(mcs, **kwargs):
@ -155,7 +155,7 @@ class Command(object):
overloading evential same-named class properties.""" overloading evential same-named class properties."""
if kwargs: if kwargs:
_init_command(self, **kwargs) _init_command(self, **kwargs)
self.lockhandler = LockHandler(self) self.lockhandler = LazyLoadHandler(self, "lockhandler", LockHandler)
def __str__(self): def __str__(self):
"Print the command" "Print the command"

View file

@ -298,7 +298,7 @@ class TempMsg(object):
self.header = header self.header = header
self.message = message self.message = message
self.lock_storage = lockstring self.lock_storage = lockstring
self.locks = LockHandler(self) self.locks = LazyLoadHandler(self, "locks", LockHandler)
self.hide_from = hide_from and make_iter(hide_from) or [] self.hide_from = hide_from and make_iter(hide_from) or []
self.date_sent = datetime.now() self.date_sent = datetime.now()
@ -360,9 +360,9 @@ class ChannelDB(TypedObject):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
TypedObject.__init__(self, *args, **kwargs) TypedObject.__init__(self, *args, **kwargs)
_SA(self, "tags", TagHandler(self)) _SA(self, "tags", LazyLoadHandler(self, "tags", TagHandler))
_SA(self, "attributes", AttributeHandler(self)) _SA(self, "attributes", LazyLoadHandler(self, "attributes", AttributeHandler))
_SA(self, "aliases", AliasHandler(self)) _SA(self, "aliases", LazyLoadHandler(self, "aliases", AliasHandler))
class Meta: class Meta:
"Define Django meta options" "Define Django meta options"

View file

@ -13,7 +13,7 @@ from django.conf import settings
#from src.scripts.models import ScriptDB #from src.scripts.models import ScriptDB
from src.comms.models import ChannelDB from src.comms.models import ChannelDB
from src.utils import logger, utils from src.utils import logger, utils
from src.utils.utils import make_iter, to_unicode from src.utils.utils import make_iter, to_unicode, LazyLoadHandler
from src.commands import cmdhandler, cmdsethandler from src.commands import cmdhandler, cmdsethandler
from src.server.session import Session from src.server.session import Session
@ -49,7 +49,7 @@ class ServerSession(Session):
self.puppet = None self.puppet = None
self.player = None self.player = None
self.cmdset_storage_string = "" self.cmdset_storage_string = ""
self.cmdset = cmdsethandler.CmdSetHandler(self) self.cmdset = LazyLoadHandler(self, "cmdset", cmdsethandler.CmdSetHandler, True)
def __cmdset_storage_get(self): def __cmdset_storage_get(self):
return [path.strip() for path in self.cmdset_storage_string.split(',')] return [path.strip() for path in self.cmdset_storage_string.split(',')]
@ -103,8 +103,7 @@ class ServerSession(Session):
self.player.save() self.player.save()
# add the session-level cmdset # add the session-level cmdset
self.cmdset = cmdsethandler.CmdSetHandler(self) self.cmdset = LazyLoadHandler(self, "cmdset", cmdsethandler.CmdSetHandler, True)
self.cmdset.update(init_mode=True)
def at_disconnect(self): def at_disconnect(self):
""" """

View file

@ -29,7 +29,6 @@ these to create custom managers.
import sys import sys
import re import re
import traceback import traceback
import weakref
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
@ -216,7 +215,7 @@ class AttributeHandler(object):
def _recache(self): def _recache(self):
self._cache = dict(("%s-%s" % (to_str(attr.db_key).lower(), self._cache = dict(("%s-%s" % (to_str(attr.db_key).lower(),
attr.db_category.lower() if attr.db_category else None), attr) attr.db_category.lower() if attr.db_category else None), attr)
for attr in _GA(self.obj, self._m2m_fieldname).filter( for attr in getattr(self.obj, self._m2m_fieldname).filter(
db_model=self._model, db_attrtype=self._attrtype)) db_model=self._model, db_attrtype=self._attrtype))
#set_attr_cache(self.obj, self._cache) # currently only for testing #set_attr_cache(self.obj, self._cache) # currently only for testing
@ -319,7 +318,7 @@ class AttributeHandler(object):
attr_obj = Attribute(db_key=key, db_category=category, attr_obj = Attribute(db_key=key, db_category=category,
db_model=self._model, db_attrtype=self._attrtype) db_model=self._model, db_attrtype=self._attrtype)
attr_obj.save() # important attr_obj.save() # important
_GA(self.obj, self._m2m_fieldname).add(attr_obj) getattr(self.obj, self._m2m_fieldname).add(attr_obj)
self._cache[cachekey] = attr_obj self._cache[cachekey] = attr_obj
if lockstring: if lockstring:
attr_obj.locks.add(lockstring) attr_obj.locks.add(lockstring)
@ -540,7 +539,7 @@ class TagHandler(object):
def _recache(self): def _recache(self):
"Update cache from database field" "Update cache from database field"
self._cache = dict(("%s-%s" % (tag.db_key, tag.db_category), tag) self._cache = dict(("%s-%s" % (tag.db_key, tag.db_category), tag)
for tag in _GA(self.obj, self._m2m_fieldname).filter( for tag in getattr(self.obj, self._m2m_fieldname).filter(
db_model=self._model, db_tagtype=self._tagtype)) db_model=self._model, db_tagtype=self._tagtype))
def add(self, tag, category=None, data=None): def add(self, tag, category=None, data=None):
@ -556,7 +555,7 @@ class TagHandler(object):
# considered part of making the tag unique) # considered part of making the tag unique)
tagobj = Tag.objects.create_tag(key=tagstr, category=category, data=data, tagobj = Tag.objects.create_tag(key=tagstr, category=category, data=data,
model=self._model, tagtype=self._tagtype) model=self._model, tagtype=self._tagtype)
_GA(self.obj, self._m2m_fieldname).add(tagobj) getattr(self.obj, self._m2m_fieldname).add(tagobj)
if self._cache is None: if self._cache is None:
self._recache() self._recache()
cachestring = "%s-%s" % (tagstr, category) cachestring = "%s-%s" % (tagstr, category)
@ -589,13 +588,13 @@ class TagHandler(object):
# that when no objects reference the tag anymore (how to check)? # that when no objects reference the tag anymore (how to check)?
tagobj = self.obj.db_tags.filter(db_key=tagstr, db_category=category) tagobj = self.obj.db_tags.filter(db_key=tagstr, db_category=category)
if tagobj: if tagobj:
_GA(self.obj, self._m2m_fieldname).remove(tagobj[0]) getattr(self.obj, self._m2m_fieldname).remove(tagobj[0])
self._recache() self._recache()
def clear(self): def clear(self):
"Remove all tags from the handler" "Remove all tags from the handler"
for tag in _GA(self.obj, self._m2m_fieldname).filter(db_model=self._model, db_tagtype=self._tagtype): for tag in getattr(self.obj, self._m2m_fieldname).filter(db_model=self._model, db_tagtype=self._tagtype):
_GA(self.obj, self._m2m_fieldname).remove(tag) getattr(self.obj, self._m2m_fieldname).remove(tag)
self._recache() self._recache()
def all(self, category=None, return_key_and_category=False): def all(self, category=None, return_key_and_category=False):
@ -608,7 +607,7 @@ class TagHandler(object):
self._recache() self._recache()
if category: if category:
category = category.strip().lower() if category is not None else None category = category.strip().lower() if category is not None else None
matches = _GA(self.obj, self._m2m_fieldname).filter(db_category=category, matches = getattr(self.obj, self._m2m_fieldname).filter(db_category=category,
db_tagtype=self._tagtype, db_tagtype=self._tagtype,
db_model=self._model).values_list("db_key") db_model=self._model).values_list("db_key")
else: else:
@ -621,7 +620,7 @@ class TagHandler(object):
return [to_str(p.db_key) for p in matches] return [to_str(p.db_key) for p in matches]
return [] return []
#return [to_str(p[0]) for p in _GA(self.obj, self._m2m_fieldname).filter(db_category__startswith=self.prefix).values_list("db_key") if p[0]] #return [to_str(p[0]) for p in getattr(self.obj, self._m2m_fieldname).filter(db_category__startswith=self.prefix).values_list("db_key") if p[0]]
def __str__(self): def __str__(self):
return ",".join(self.all()) return ",".join(self.all())

View file

@ -1052,7 +1052,7 @@ class LazyLoadHandler(object):
Initialize handler as cls(obj, *args) Initialize handler as cls(obj, *args)
""" """
obj = _GA(self, "obj")() obj = _GA(self, "obj")()
instance = _GA(self, "cls")(obj, *_GA(self, "args")) instance = _GA(self, "cls")(weakref.proxy(obj), *_GA(self, "args"))
_SA(obj, _GA(self, "name"), instance) _SA(obj, _GA(self, "name"), instance)
return instance return instance