2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
A typeclass is the companion of a TypedObject django model.
|
|
|
|
|
It 'decorates' the model without actually having to add new
|
|
|
|
|
fields to the model - transparently storing data onto its
|
|
|
|
|
associated model without the admin/user just having to deal
|
|
|
|
|
with a 'normal' Python class. The only restrictions is that
|
|
|
|
|
the typeclass must inherit from TypeClass and not reimplement
|
|
|
|
|
the get/setters defined below. There are also a few properties
|
|
|
|
|
that are protected, so as to not overwrite property names
|
2012-03-30 23:47:22 +02:00
|
|
|
used by the typesystem or django itself.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
|
2012-03-24 18:25:32 +01:00
|
|
|
from src.utils.logger import log_trace, log_errmsg
|
2010-08-29 18:46:58 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
2012-03-31 15:09:22 +02:00
|
|
|
__all__ = ("TypeClass",)
|
|
|
|
|
|
2012-02-05 21:04:10 +01:00
|
|
|
# these are called so many times it's worth to avoid lookup calls
|
2012-03-31 13:06:29 +02:00
|
|
|
_GA = object.__getattribute__
|
|
|
|
|
_SA = object.__setattr__
|
|
|
|
|
_DA = object.__delattr__
|
2012-02-05 21:04:10 +01:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
# To ensure the sanity of the model, there are a
|
|
|
|
|
# few property names we won't allow the admin to
|
2011-03-20 20:44:48 +00:00
|
|
|
# set on the typeclass just like that. Note that these are *not* related
|
2011-03-19 22:17:00 +00:00
|
|
|
# to *in-game* safety (if you can edit typeclasses you have
|
|
|
|
|
# full access anyway), so no protection against changing
|
|
|
|
|
# e.g. 'locks' or 'permissions' should go here.
|
2012-02-05 21:04:10 +01:00
|
|
|
PROTECTED = ('id', 'dbobj', 'db', 'ndb', 'objects', 'typeclass',
|
2012-02-14 23:40:16 +01:00
|
|
|
'attr', 'save', 'delete', 'db_model_name','attribute_class',
|
|
|
|
|
'typeclass_paths')
|
2011-03-19 22:17:00 +00:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
# If this is true, all non-protected property assignments
|
|
|
|
|
# are directly stored to a database attribute
|
|
|
|
|
|
|
|
|
|
class MetaTypeClass(type):
|
|
|
|
|
"""
|
|
|
|
|
This metaclass just makes sure the class object gets
|
|
|
|
|
printed in a nicer way (it might end up having no name at all
|
|
|
|
|
otherwise due to the magics being done with get/setattribute).
|
|
|
|
|
"""
|
2011-03-20 19:45:56 +00:00
|
|
|
def __init__(mcs, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Adds some features to typeclassed objects
|
|
|
|
|
"""
|
|
|
|
|
super(MetaTypeClass, mcs).__init__(*args, **kwargs)
|
2011-10-03 23:53:23 +02:00
|
|
|
mcs.typename = mcs.__name__
|
2011-03-20 19:45:56 +00:00
|
|
|
mcs.path = "%s.%s" % (mcs.__module__, mcs.__name__)
|
2012-03-24 23:02:45 +01:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def __str__(cls):
|
|
|
|
|
return "%s" % cls.__name__
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
class TypeClass(object):
|
|
|
|
|
"""
|
|
|
|
|
This class implements a 'typeclass' object. This is connected
|
|
|
|
|
to a database object inheriting from TypedObject.
|
|
|
|
|
the TypeClass allows for all customization.
|
|
|
|
|
Most of the time this means that the admin never has to
|
|
|
|
|
worry about database access but only deal with extending
|
2012-03-30 23:47:22 +02:00
|
|
|
TypeClasses to create diverse objects in the game.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
The ObjectType class has all functionality for wrapping a
|
2012-03-30 23:47:22 +02:00
|
|
|
database object transparently.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
It's up to its child classes to implement eventual custom hooks
|
2012-03-30 23:47:22 +02:00
|
|
|
and other functions called by the engine.
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
__metaclass__ = MetaTypeClass
|
|
|
|
|
|
|
|
|
|
def __init__(self, dbobj):
|
|
|
|
|
"""
|
|
|
|
|
Initialize the object class. There are two ways to call this class.
|
|
|
|
|
o = object_class(dbobj) : this is used to initialize dbobj with the class name
|
2012-03-30 23:47:22 +02:00
|
|
|
o = dbobj.object_class(dbobj) : this is used when dbobj.object_class is already set.
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2011-08-06 18:15:04 +00:00
|
|
|
# typecheck of dbobj - we can't allow it to be added here
|
|
|
|
|
# unless it's really a TypedObject.
|
2012-03-31 13:06:29 +02:00
|
|
|
dbobj_cls = _GA(dbobj, '__class__')
|
|
|
|
|
dbobj_mro = _GA(dbobj_cls, '__mro__')
|
2012-02-26 15:10:22 +01:00
|
|
|
if not any('src.typeclasses.models.TypedObject' in str(mro) for mro in dbobj_mro):
|
|
|
|
|
raise Exception("dbobj is not a TypedObject: %s: %s" % (dbobj_cls, dbobj_mro))
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2012-02-26 15:10:22 +01:00
|
|
|
# store the reference to the database model instance
|
2012-03-31 13:06:29 +02:00
|
|
|
_SA(self, 'dbobj', dbobj)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def __getattribute__(self, propname):
|
|
|
|
|
"""
|
|
|
|
|
Change the normal property access to
|
|
|
|
|
transparently include the properties on
|
|
|
|
|
self.dbobj. Note that dbobj properties have
|
|
|
|
|
priority, so if you define a same-named
|
2012-04-26 17:47:25 +02:00
|
|
|
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
property on the class, it will NOT be
|
2012-03-30 23:47:22 +02:00
|
|
|
accessible through getattr.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
if propname == 'dbobj':
|
2012-03-31 13:06:29 +02:00
|
|
|
return _GA(self, 'dbobj')
|
2010-08-29 18:46:58 +00:00
|
|
|
if propname.startswith('__') and propname.endswith('__'):
|
|
|
|
|
# python specials are parsed as-is (otherwise things like
|
|
|
|
|
# isinstance() fail to identify the typeclass)
|
2012-03-31 13:06:29 +02:00
|
|
|
return _GA(self, propname)
|
2012-03-30 23:47:22 +02:00
|
|
|
#print "get %s (dbobj:%s)" % (propname, type(dbobj))
|
2010-08-29 18:46:58 +00:00
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
return _GA(self, propname)
|
2010-08-29 18:46:58 +00:00
|
|
|
except AttributeError:
|
2012-03-24 18:25:32 +01:00
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
dbobj = _GA(self, 'dbobj')
|
2012-03-24 18:25:32 +01:00
|
|
|
except AttributeError:
|
|
|
|
|
log_trace("Typeclass CRITICAL ERROR! dbobj not found for Typeclass %s!" % self)
|
2012-03-30 23:47:22 +02:00
|
|
|
raise
|
2010-08-29 18:46:58 +00:00
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
return _GA(dbobj, propname)
|
2010-08-29 18:46:58 +00:00
|
|
|
except AttributeError:
|
|
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
return _GA(dbobj,"get_attribute_raise")(propname)
|
2010-08-29 18:46:58 +00:00
|
|
|
except AttributeError:
|
2012-04-26 17:47:25 +02:00
|
|
|
string = "Object: '%s' not found on %s(#%s), nor on its typeclass %s."
|
|
|
|
|
raise AttributeError(string % (propname, dbobj, dbobj.dbid, dbobj.typeclass_path))
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def __setattr__(self, propname, value):
|
|
|
|
|
"""
|
|
|
|
|
Transparently save data to the dbobj object in
|
|
|
|
|
all situations. Note that this does not
|
|
|
|
|
necessarily mean storing it to the database
|
|
|
|
|
unless data is stored into a propname
|
2012-03-30 23:47:22 +02:00
|
|
|
corresponding to a field on ObjectDB model.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
#print "set %s -> %s" % (propname, value)
|
2012-02-05 21:04:10 +01:00
|
|
|
if propname in PROTECTED:
|
2012-03-30 23:47:22 +02:00
|
|
|
string = "%s: '%s' is a protected attribute name."
|
2012-02-05 21:04:10 +01:00
|
|
|
string += " (protected: [%s])" % (", ".join(PROTECTED))
|
2012-03-24 18:25:32 +01:00
|
|
|
log_errmsg(string % (self.name, propname))
|
2012-03-30 23:47:22 +02:00
|
|
|
return
|
2012-02-05 21:04:10 +01:00
|
|
|
|
|
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
dbobj = _GA(self, 'dbobj')
|
2012-02-05 21:04:10 +01:00
|
|
|
except AttributeError:
|
2012-03-30 23:47:22 +02:00
|
|
|
dbobj = None
|
|
|
|
|
log_trace("This is probably due to an unsafe reload.")
|
|
|
|
|
|
|
|
|
|
if dbobj:
|
2010-08-29 18:46:58 +00:00
|
|
|
try:
|
2012-03-30 23:47:22 +02:00
|
|
|
# only set value on propname if propname already exists
|
2012-02-05 21:04:10 +01:00
|
|
|
# on dbobj. __getattribute__ will raise attribute error otherwise.
|
2012-03-31 13:06:29 +02:00
|
|
|
_GA(dbobj, propname)
|
|
|
|
|
_SA(dbobj, propname, value)
|
2010-08-29 18:46:58 +00:00
|
|
|
except AttributeError:
|
2012-02-05 21:04:10 +01:00
|
|
|
dbobj.set_attribute(propname, value)
|
|
|
|
|
else:
|
2012-03-31 13:06:29 +02:00
|
|
|
_SA(self, propname, value)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
"""
|
|
|
|
|
dbobj-recognized comparison
|
2012-03-30 23:47:22 +02:00
|
|
|
"""
|
2012-02-05 21:04:10 +01:00
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
return other == self or other == _GA(self, dbobj) or other == _GA(self, dbobj).user
|
2012-02-05 21:04:10 +01:00
|
|
|
except AttributeError:
|
|
|
|
|
# if self.dbobj.user fails it means the two previous comparisons failed already
|
|
|
|
|
return False
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2011-02-05 18:06:18 +00:00
|
|
|
|
|
|
|
|
def __delattr__(self, propname):
|
|
|
|
|
"""
|
|
|
|
|
Transparently deletes data from the typeclass or dbobj by first searching on the typeclass,
|
2011-10-01 15:10:21 +02:00
|
|
|
secondly on the dbobj.db.
|
2012-03-30 23:47:22 +02:00
|
|
|
Will not allow deletion of properties stored directly on dbobj.
|
2011-02-05 18:06:18 +00:00
|
|
|
"""
|
2012-02-05 21:04:10 +01:00
|
|
|
if propname in PROTECTED:
|
2012-03-30 23:47:22 +02:00
|
|
|
string = "%s: '%s' is a protected attribute name."
|
2012-02-05 21:04:10 +01:00
|
|
|
string += " (protected: [%s])" % (", ".join(PROTECTED))
|
2012-03-24 18:25:32 +01:00
|
|
|
log_errmsg(string % (self.name, propname))
|
2012-03-30 23:47:22 +02:00
|
|
|
return
|
2012-02-05 21:04:10 +01:00
|
|
|
|
|
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
_DA(self, propname)
|
2012-02-05 21:04:10 +01:00
|
|
|
except AttributeError:
|
|
|
|
|
# not on typeclass, try to delete on db/ndb
|
2011-02-05 18:06:18 +00:00
|
|
|
try:
|
2012-03-31 13:06:29 +02:00
|
|
|
dbobj = _GA(self, 'dbobj')
|
2011-02-05 18:06:18 +00:00
|
|
|
except AttributeError:
|
2012-03-30 23:47:22 +02:00
|
|
|
log_trace("This is probably due to an unsafe reload.")
|
|
|
|
|
return # ignore delete
|
2012-02-05 21:04:10 +01:00
|
|
|
try:
|
2012-03-30 23:47:22 +02:00
|
|
|
dbobj.del_attribute_raise(propname)
|
2012-02-05 21:04:10 +01:00
|
|
|
except AttributeError:
|
2012-04-26 17:47:25 +02:00
|
|
|
string = "Object: '%s' not found on %s(#%s), nor on its typeclass %s."
|
2012-02-05 21:04:10 +01:00
|
|
|
raise AttributeError(string % (propname, dbobj,
|
2012-04-26 17:47:25 +02:00
|
|
|
dbobj.dbid,
|
2012-02-05 21:04:10 +01:00
|
|
|
dbobj.typeclass_path,))
|
2011-02-05 18:06:18 +00:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def __str__(self):
|
|
|
|
|
"represent the object"
|
|
|
|
|
return self.key
|
2012-02-14 23:40:16 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
|
return u"%s" % self.key
|