Further cleanup of source; making class methods _private for clarity in the API.

This commit is contained in:
Griatch 2012-03-31 15:09:22 +02:00
parent fc156b5a54
commit c8df141e89
18 changed files with 607 additions and 588 deletions

View file

@ -4,18 +4,19 @@ The custom manager for Scripts.
from src.typeclasses.managers import TypedObjectManager
from src.typeclasses.managers import returns_typeclass_list
__all__ = ("ScriptManager",)
VALIDATE_ITERATION = 0
class ScriptManager(TypedObjectManager):
"""
This Scriptmanager implements methods for searching
This Scriptmanager implements methods for searching
and manipulating Scripts directly from the database.
Evennia-specific search methods (will return Typeclasses or
lists of Typeclasses, whereas Django-general methods will return
Querysets or database objects).
Querysets or database objects).
dbref (converter)
dbref_search
get_dbref_range
@ -38,15 +39,15 @@ class ScriptManager(TypedObjectManager):
if not obj:
return []
scripts = self.filter(db_obj=obj)
if key:
if key:
return scripts.filter(db_key=key)
return scripts
return scripts
@returns_typeclass_list
def get_all_scripts(self, key=None):
"""
Return all scripts, alternative only
scripts with a certain key/dbref or path.
scripts with a certain key/dbref or path.
"""
if key:
dbref = self.dbref(key)
@ -66,7 +67,7 @@ class ScriptManager(TypedObjectManager):
This stops and deletes a specific script directly
from the script database. This might be
needed for global scripts not tied to
a specific game object.
a specific game object.
"""
scripts = self.get_id(dbref)
for script in scripts:
@ -76,7 +77,7 @@ class ScriptManager(TypedObjectManager):
"""
This cleans up the script database of all non-persistent
scripts, or only those on obj. It is called every time the server restarts
and
and
"""
if obj:
to_stop = self.filter(db_persistent=False, db_obj=obj)
@ -86,70 +87,70 @@ class ScriptManager(TypedObjectManager):
for script in to_stop.filter(db_is_active=True):
script.stop()
for script in to_stop.filter(db_is_active=False):
script.delete()
script.delete()
return nr_deleted
def validate(self, scripts=None, obj=None, key=None, dbref=None,
def validate(self, scripts=None, obj=None, key=None, dbref=None,
init_mode=False):
"""
This will step through the script database and make sure
all objects run scripts that are still valid in the context
they are in. This is called by the game engine at regular
intervals but can also be initiated by player scripts.
intervals but can also be initiated by player scripts.
If key and/or obj is given, only update the related
script/object.
Only one of the arguments are supposed to be supplied
at a time, since they are exclusive to each other.
scripts = a list of scripts objects obtained somewhere.
obj = validate only scripts defined on a special object.
key = validate only scripts with a particular key
dbref = validate only the single script with this particular id.
dbref = validate only the single script with this particular id.
init_mode - This is used during server upstart and can have
three values:
three values:
False (no init mode). Called during run.
"reset" - server reboot. Kill non-persistent scripts
"reload" - server reload. Keep non-persistent scripts.
This method also makes sure start any scripts it validates,
this should be harmless, since already-active scripts
have the property 'is_running' set and will be skipped.
have the property 'is_running' set and will be skipped.
"""
# we store a variable that tracks if we are calling a
# validation from within another validation (avoids
# loops).
# we store a variable that tracks if we are calling a
# validation from within another validation (avoids
# loops).
global VALIDATE_ITERATION
global VALIDATE_ITERATION
if VALIDATE_ITERATION > 0:
# we are in a nested validation. Exit.
VALIDATE_ITERATION -= 1
return None, None
return None, None
VALIDATE_ITERATION += 1
# not in a validation - loop. Validate as normal.
nr_started = 0
nr_stopped = 0
nr_stopped = 0
if init_mode:
if init_mode == 'reset':
# special mode when server starts or object logs in.
# This deletes all non-persistent scripts from database
# special mode when server starts or object logs in.
# This deletes all non-persistent scripts from database
nr_stopped += self.remove_non_persistent(obj=obj)
# turn off the activity flag for all remaining scripts
scripts = self.get_all_scripts()
for script in scripts:
script.dbobj.is_active = False
script.dbobj.is_active = False
elif not scripts:
# normal operation
# normal operation
if dbref and self.dbref(dbref):
scripts = self.get_id(dbref)
elif obj:
scripts = self.get_all_scripts_on_obj(obj, key=key)
scripts = self.get_all_scripts_on_obj(obj, key=key)
else:
scripts = self.get_all_scripts(key=key) #self.model.get_all_cached_instances()
@ -158,46 +159,46 @@ class ScriptManager(TypedObjectManager):
VALIDATE_ITERATION -= 1
return None, None
#print "scripts to validate: [%s]" % (", ".join(script.key for script in scripts))
#print "scripts to validate: [%s]" % (", ".join(script.key for script in scripts))
for script in scripts:
#print "validating %s (%i) (init_mode=%s)" % (script.key, id(script.dbobj), init_mode)
if script.is_valid():
nr_started += script.start(force_restart=init_mode)
#print "validating %s (%i) (init_mode=%s)" % (script.key, id(script.dbobj), init_mode)
if script.is_valid():
nr_started += script.start(force_restart=init_mode)
#print "back from start. nr_started=", nr_started
else:
script.stop()
nr_stopped += 1
VALIDATE_ITERATION -= 1
return nr_started, nr_stopped
@returns_typeclass_list
def script_search(self, ostring, obj=None, only_timed=False):
"""
Search for a particular script.
ostring - search criterion - a script ID or key
obj - limit search to scripts defined on this object
only_timed - limit search only to scripts that run
on a timer.
on a timer.
"""
ostring = ostring.strip()
dbref = self.dbref(ostring)
if dbref:
# this is a dbref, try to find the script directly
dbref_match = self.dbref_search(dbref)
if dbref_match:
ok = True
ok = True
if obj and obj != dbref_match.obj:
ok = False
if only_timed and dbref_match.interval:
ok = False
ok = False
if ok:
return [dbref_match]
# not a dbref; normal search
scripts = self.filter(db_key__iexact=ostring)
if obj:
scripts = scripts.exclude(db_obj=None).filter(db_obj__db_key__iexact=ostring)
if only_timed: