mirror of
https://github.com/evennia/evennia.git
synced 2026-03-25 09:16:32 +01:00
Cleaning some unnecessary whitespace, overall cleanup of various source codes.
This commit is contained in:
parent
d4c97d7df8
commit
c0322c9eae
27 changed files with 1342 additions and 1318 deletions
|
|
@ -1,7 +1,7 @@
|
|||
"""
|
||||
This implements the common managers that are used by the
|
||||
abstract models in dbobjects.py (and which are thus shared by
|
||||
all Attributes and TypedObjects).
|
||||
all Attributes and TypedObjects).
|
||||
"""
|
||||
from functools import update_wrapper
|
||||
from django.db import models
|
||||
|
|
@ -9,15 +9,15 @@ from src.utils import idmapper
|
|||
from src.utils.utils import make_iter
|
||||
#from src.typeclasses import idmap
|
||||
|
||||
# Managers
|
||||
# Managers
|
||||
|
||||
class AttributeManager(models.Manager):
|
||||
"Manager for handling Attributes."
|
||||
|
||||
def attr_namesearch(self, searchstr, obj, exact_match=True):
|
||||
"""
|
||||
Searches the object's attributes for name matches.
|
||||
|
||||
Searches the object's attributes for name matches.
|
||||
|
||||
searchstr: (str) A string to search for.
|
||||
"""
|
||||
# Retrieve the list of attributes for this object.
|
||||
|
|
@ -29,21 +29,21 @@ class AttributeManager(models.Manager):
|
|||
db_key__icontains=searchstr)
|
||||
|
||||
#
|
||||
# helper functions for the TypedObjectManager.
|
||||
#
|
||||
# helper functions for the TypedObjectManager.
|
||||
#
|
||||
|
||||
def returns_typeclass_list(method):
|
||||
"""
|
||||
Decorator: Chantes return of the decorated method (which are
|
||||
TypeClassed objects) into object_classes(s) instead. Will always
|
||||
return a list (may be empty).
|
||||
"""
|
||||
"""
|
||||
def func(self, *args, **kwargs):
|
||||
"decorator. Returns a list."
|
||||
"decorator. Returns a list."
|
||||
self.__doc__ = method.__doc__
|
||||
matches = method(self, *args, **kwargs)
|
||||
matches = method(self, *args, **kwargs)
|
||||
return [(hasattr(dbobj, "typeclass") and dbobj.typeclass) or dbobj for dbobj in make_iter(matches)]
|
||||
return update_wrapper(func, method)
|
||||
return update_wrapper(func, method)
|
||||
|
||||
def returns_typeclass(method):
|
||||
"""
|
||||
|
|
@ -55,8 +55,8 @@ def returns_typeclass(method):
|
|||
rfunc = returns_typeclass_list(method)
|
||||
try:
|
||||
return rfunc(self, *args, **kwargs)[0]
|
||||
except IndexError:
|
||||
return None
|
||||
except IndexError:
|
||||
return None
|
||||
return update_wrapper(func, method)
|
||||
|
||||
|
||||
|
|
@ -64,20 +64,20 @@ def returns_typeclass(method):
|
|||
#class TypedObjectManager(models.Manager):
|
||||
class TypedObjectManager(idmapper.manager.SharedMemoryManager):
|
||||
"""
|
||||
Common ObjectManager for all dbobjects.
|
||||
Common ObjectManager for all dbobjects.
|
||||
"""
|
||||
|
||||
def dbref(self, dbref):
|
||||
"""
|
||||
Valid forms of dbref (database reference number)
|
||||
are either a string '#N' or an integer N.
|
||||
Output is the integer part.
|
||||
Output is the integer part.
|
||||
"""
|
||||
if isinstance(dbref, basestring):
|
||||
dbref = dbref.lstrip('#')
|
||||
try:
|
||||
if int(dbref) < 1:
|
||||
return None
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
return dbref
|
||||
|
|
@ -92,7 +92,7 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
|
|||
"""
|
||||
self.filter()
|
||||
|
||||
|
||||
|
||||
@returns_typeclass
|
||||
def dbref_search(self, dbref):
|
||||
"""
|
||||
|
|
@ -131,25 +131,24 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
|
|||
"""
|
||||
Returns a dictionary with all the typeclasses active in-game
|
||||
as well as the number of such objects defined (i.e. the number
|
||||
of database object having that typeclass set on themselves).
|
||||
of database object having that typeclass set on themselves).
|
||||
"""
|
||||
dbtotals = {}
|
||||
typeclass_paths = set(self.values_list('db_typeclass_path', flat=True))
|
||||
for typeclass_path in typeclass_paths:
|
||||
for typeclass_path in typeclass_paths:
|
||||
dbtotals[typeclass_path] = \
|
||||
self.filter(db_typeclass_path=typeclass_path).count()
|
||||
return dbtotals
|
||||
return dbtotals
|
||||
|
||||
@returns_typeclass_list
|
||||
def typeclass_search(self, typeclass):
|
||||
"""
|
||||
Searches through all objects returning those which has a certain
|
||||
typeclass. If location is set, limit search to objects in
|
||||
that location.
|
||||
that location.
|
||||
"""
|
||||
if callable(typeclass):
|
||||
cls = typeclass.__class__
|
||||
typeclass = "%s.%s" % (cls.__module__, cls.__name__)
|
||||
o_query = self.filter(db_typeclass_path__exact=typeclass)
|
||||
o_query = self.filter(db_typeclass_path__exact=typeclass)
|
||||
return o_query
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -7,7 +7,7 @@ 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
|
||||
used by the typesystem or django itself.
|
||||
used by the typesystem or django itself.
|
||||
"""
|
||||
|
||||
from src.utils.logger import log_trace, log_errmsg
|
||||
|
|
@ -45,12 +45,9 @@ class MetaTypeClass(type):
|
|||
mcs.typename = mcs.__name__
|
||||
mcs.path = "%s.%s" % (mcs.__module__, mcs.__name__)
|
||||
|
||||
|
||||
|
||||
|
||||
def __str__(cls):
|
||||
return "%s" % cls.__name__
|
||||
|
||||
|
||||
class TypeClass(object):
|
||||
"""
|
||||
This class implements a 'typeclass' object. This is connected
|
||||
|
|
@ -58,14 +55,14 @@ class TypeClass(object):
|
|||
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
|
||||
TypeClasses to create diverse objects in the game.
|
||||
TypeClasses to create diverse objects in the game.
|
||||
|
||||
The ObjectType class has all functionality for wrapping a
|
||||
database object transparently.
|
||||
database object transparently.
|
||||
|
||||
It's up to its child classes to implement eventual custom hooks
|
||||
and other functions called by the engine.
|
||||
|
||||
and other functions called by the engine.
|
||||
|
||||
"""
|
||||
__metaclass__ = MetaTypeClass
|
||||
|
||||
|
|
@ -73,8 +70,8 @@ class TypeClass(object):
|
|||
"""
|
||||
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
|
||||
o = dbobj.object_class(dbobj) : this is used when dbobj.object_class is already set.
|
||||
|
||||
o = dbobj.object_class(dbobj) : this is used when dbobj.object_class is already set.
|
||||
|
||||
"""
|
||||
# typecheck of dbobj - we can't allow it to be added here
|
||||
# unless it's really a TypedObject.
|
||||
|
|
@ -84,7 +81,7 @@ class TypeClass(object):
|
|||
raise Exception("dbobj is not a TypedObject: %s: %s" % (dbobj_cls, dbobj_mro))
|
||||
|
||||
# store the reference to the database model instance
|
||||
SA(self, 'dbobj', dbobj)
|
||||
SA(self, 'dbobj', dbobj)
|
||||
|
||||
def __getattribute__(self, propname):
|
||||
"""
|
||||
|
|
@ -93,7 +90,7 @@ class TypeClass(object):
|
|||
self.dbobj. Note that dbobj properties have
|
||||
priority, so if you define a same-named
|
||||
property on the class, it will NOT be
|
||||
accessible through getattr.
|
||||
accessible through getattr.
|
||||
"""
|
||||
if propname == 'dbobj':
|
||||
return GA(self, 'dbobj')
|
||||
|
|
@ -101,7 +98,7 @@ class TypeClass(object):
|
|||
# python specials are parsed as-is (otherwise things like
|
||||
# isinstance() fail to identify the typeclass)
|
||||
return GA(self, propname)
|
||||
#print "get %s (dbobj:%s)" % (propname, type(dbobj))
|
||||
#print "get %s (dbobj:%s)" % (propname, type(dbobj))
|
||||
try:
|
||||
return GA(self, propname)
|
||||
except AttributeError:
|
||||
|
|
@ -109,7 +106,7 @@ class TypeClass(object):
|
|||
dbobj = GA(self, 'dbobj')
|
||||
except AttributeError:
|
||||
log_trace("Typeclass CRITICAL ERROR! dbobj not found for Typeclass %s!" % self)
|
||||
raise
|
||||
raise
|
||||
try:
|
||||
return GA(dbobj, propname)
|
||||
except AttributeError:
|
||||
|
|
@ -118,31 +115,31 @@ class TypeClass(object):
|
|||
except AttributeError:
|
||||
string = "Object: '%s' not found on %s(%s), nor on its typeclass %s."
|
||||
raise AttributeError(string % (propname, dbobj, dbobj.dbref, dbobj.typeclass_path))
|
||||
|
||||
|
||||
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
|
||||
corresponding to a field on ObjectDB model.
|
||||
corresponding to a field on ObjectDB model.
|
||||
"""
|
||||
#print "set %s -> %s" % (propname, value)
|
||||
if propname in PROTECTED:
|
||||
string = "%s: '%s' is a protected attribute name."
|
||||
string = "%s: '%s' is a protected attribute name."
|
||||
string += " (protected: [%s])" % (", ".join(PROTECTED))
|
||||
log_errmsg(string % (self.name, propname))
|
||||
return
|
||||
return
|
||||
|
||||
try:
|
||||
dbobj = GA(self, 'dbobj')
|
||||
except AttributeError:
|
||||
dbobj = None
|
||||
log_trace("This is probably due to an unsafe reload.")
|
||||
|
||||
if dbobj:
|
||||
dbobj = None
|
||||
log_trace("This is probably due to an unsafe reload.")
|
||||
|
||||
if dbobj:
|
||||
try:
|
||||
# only set value on propname if propname already exists
|
||||
# only set value on propname if propname already exists
|
||||
# on dbobj. __getattribute__ will raise attribute error otherwise.
|
||||
GA(dbobj, propname)
|
||||
SA(dbobj, propname, value)
|
||||
|
|
@ -154,25 +151,25 @@ class TypeClass(object):
|
|||
def __eq__(self, other):
|
||||
"""
|
||||
dbobj-recognized comparison
|
||||
"""
|
||||
"""
|
||||
try:
|
||||
return other == self or other == GA(self, dbobj) or other == GA(self, dbobj).user
|
||||
except AttributeError:
|
||||
# if self.dbobj.user fails it means the two previous comparisons failed already
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def __delattr__(self, propname):
|
||||
"""
|
||||
Transparently deletes data from the typeclass or dbobj by first searching on the typeclass,
|
||||
secondly on the dbobj.db.
|
||||
Will not allow deletion of properties stored directly on dbobj.
|
||||
Will not allow deletion of properties stored directly on dbobj.
|
||||
"""
|
||||
if propname in PROTECTED:
|
||||
string = "%s: '%s' is a protected attribute name."
|
||||
string = "%s: '%s' is a protected attribute name."
|
||||
string += " (protected: [%s])" % (", ".join(PROTECTED))
|
||||
log_errmsg(string % (self.name, propname))
|
||||
return
|
||||
return
|
||||
|
||||
try:
|
||||
DA(self, propname)
|
||||
|
|
@ -181,10 +178,10 @@ class TypeClass(object):
|
|||
try:
|
||||
dbobj = GA(self, 'dbobj')
|
||||
except AttributeError:
|
||||
log_trace("This is probably due to an unsafe reload.")
|
||||
return # ignore delete
|
||||
log_trace("This is probably due to an unsafe reload.")
|
||||
return # ignore delete
|
||||
try:
|
||||
dbobj.del_attribute_raise(propname)
|
||||
dbobj.del_attribute_raise(propname)
|
||||
except AttributeError:
|
||||
string = "Object: '%s' not found on %s(%s), nor on its typeclass %s."
|
||||
raise AttributeError(string % (propname, dbobj,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue