Fixed cache and metaclass irregularities as well as the location and contents caches.

This commit is contained in:
Griatch 2013-09-22 21:48:08 +02:00
parent 0a394929b7
commit 642ae2faf9
4 changed files with 38 additions and 45 deletions

View file

@ -214,7 +214,6 @@ def format_script_list(scripts):
table.align = 'r'
for script in scripts:
nextrep = script.time_until_next_repeat()
print "@script:", script.key, type(script.key)
table.add_row([script.id,
script.obj.key if (hasattr(script, 'obj') and script.obj) else "<Global>",
script.key,

View file

@ -146,7 +146,7 @@ class ObjectDB(TypedObject):
_SA(self, "aliases", AliasHandler(self))
_SA(self, "nicks", NickHandler(self))
# make sure to sync the contents cache when initializing
self.contents_update()
_GA(self, "contents_update")()
# Wrapper properties to easily set database fields. These are
# @property decorators that allows to access these fields using
@ -214,17 +214,9 @@ class ObjectDB(TypedObject):
def _at_db_location_save(self, new_value, old_value=None):
"This is called automatically just before a new location is saved."
#print "db_location_handler:", loc, old_value
loc = new_value
try:
old_loc = old_value
# new_value can be dbref, typeclass or dbmodel
if ObjectDB.objects.dbref(loc, reqhash=False):
loc = ObjectDB.objects.dbref_search(loc)
if loc and type(loc) != ObjectDB:
# this should not fail if new_value is valid.
loc = _GA(loc, "dbobj")
# recursive location check
def is_loc_loop(loc, depth=0):
"Recursively traverse the target location to make sure we are not in it."
@ -239,9 +231,9 @@ class ObjectDB(TypedObject):
#print "db_location_handler2:", _GA(loc, "db_key") if loc else loc, type(loc)
# update the contents of each location
if old_loc:
_GA(_GA(old_loc, "dbobj"), "contents_update")(self, remove=True)
_GA(_GA(old_loc, "dbobj"), "contents_remove")(self)
if loc:
_GA(loc, "contents_update")(self)
_GA(loc, "contents_add")(self)
return loc
except RuntimeError:
string = "Cannot set location, "
@ -463,32 +455,35 @@ class ObjectDB(TypedObject):
exclude is one or more objects to not return
"""
cont = get_prop_cache(self, "_contents")
contents = get_prop_cache(self, "_contents")
exclude = make_iter(exclude)
if cont == None:
cont = _GA(self, "contents_update")()
return [obj for obj in cont.values() if obj not in exclude]
if contents == None:
contents = _GA(self, "contents_update")()
return [obj.typeclass for obj in contents.values() if obj not in exclude]
contents = property(contents_get)
def contents_update(self, obj=None, remove=False):
"""
Updates the contents property of the object
add - object to add to content list
remove object to remove from content list
"""
cont = get_prop_cache(self, "_contents")
if not cont:
cont = {}
if obj:
if remove:
cont.pop(self.dbid, None)
else:
cont[self.dbid] = obj
# manage the content cache
def contents_add(self, obj):
"Add a new object to the internal content cache"
contents = get_prop_cache(self, "_contents")
if contents == None:
contents={obj.dbid:obj}
else:
cont = dict((o.dbid, o) for o in ObjectDB.objects.get_contents(self))
set_prop_cache(self, "_contents", cont)
return cont
contents[obj.dbid] = obj
set_prop_cache(self, "_contents", contents)
def contents_remove(self, obj):
"Remove object from internal content cache"
contents = get_prop_cache(self, "_contents")
if contents == None:
contents = {}
else:
contents.pop(obj.dbid, None)
set_prop_cache(self, "_contents", contents)
def contents_update(self):
"Re-sync the contents cache"
contents = dict((o.dbid, o) for o in ObjectDB.objects.get_contents(self))
set_prop_cache(self, "_contents", contents)
return contents
#@property
def __exits_get(self):

View file

@ -95,7 +95,6 @@ def field_pre_save(sender, instance=None, update_fields=None, raw=False, **kwarg
"""
if raw:
return
#print "field_pre_save:", instance, update_fields# if hasattr(instance, "db_key") else instance, update_fields
if update_fields:
# this is a list of strings at this point. We want field objects
update_fields = (_GA(_GA(instance, "_meta"), "get_field_by_name")(field)[0] for field in update_fields)
@ -104,12 +103,10 @@ def field_pre_save(sender, instance=None, update_fields=None, raw=False, **kwarg
update_fields = _GA(_GA(instance, "_meta"), "fields")
for field in update_fields:
fieldname = field.name
new_value = field.value_from_object(instance)
new_value = _GA(instance, fieldname)#field.value_from_object(instance)
# try to see if there is a handler on object that should be triggered when saving.
handlername = "_at_%s_save" % fieldname
handler = _GA(instance, handlername) if handlername in _GA(instance, '__dict__') else None
#if handlername == "_at_db_location_save":
# print "handler:", handlername, handler, _GA(sender, '__dict__').keys()
handler = _GA(instance, handlername) if handlername in _GA(sender, '__dict__') else None
if callable(handler):
#hid = hashid(instance, "-%s" % fieldname)
try:

View file

@ -85,19 +85,20 @@ class SharedMemoryModelBase(ModelBase):
"Helper method to create property wrappers with unique names (must be in separate call)"
def _get(cls, fname):
"Wrapper for getting database field"
return _GA(cls, fieldname)
def _get_foreign(cls, fname):
"Wrapper for returing foreignkey fields"
value = _GA(cls, fieldname)
if isinstance(value, (basestring, int, float, bool)):
return value
elif hasattr(value, "typeclass"):
if fieldname == "db_key": print "idmapper _get typeclass:, ", cls.__class__.__name__, fieldname, _GA(value, "typeclass")
#print "_get_foreign:value:", value
try:
return _GA(value, "typeclass")
return value
except:
return value
def _set_nonedit(cls, fname, value):
"Wrapper for blocking editing of field"
raise FieldError("Field %s cannot be edited." % fname)
def _set(cls, fname, value):
"Wrapper for setting database field"
if fname=="db_key": print "db_key _set:", value, type(value)
_SA(cls, fname, value)
# only use explicit update_fields in save if we actually have a
# primary key assigned already (won't be set when first creating object)
@ -142,6 +143,7 @@ class SharedMemoryModelBase(ModelBase):
if not editable:
fset = lambda cls, val: _set_nonedit(cls, fieldname, val)
elif foreignkey:
fget = lambda cls: _get_foreign(cls, fieldname)
fset = lambda cls, val: _set_foreign(cls, fieldname, val)
else:
fset = lambda cls, val: _set(cls, fieldname, val)