Removed repetitive imports in the utils.create functions. I experimented with making these methods yield more, but it's hard to integrate this behaviour with actual production code.

This commit is contained in:
Griatch 2012-05-01 22:51:18 +02:00
parent a8139feb1a
commit 4acfe48d9c

View file

@ -21,23 +21,37 @@ Models covered:
Channel Channel
Players Players
""" """
from twisted.internet.defer import inlineCallbacks, returnValue
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import IntegrityError from django.db import IntegrityError
from src.utils.idmapper.models import SharedMemoryModel from src.utils.idmapper.models import SharedMemoryModel
from src.utils import utils, logger from src.utils import utils, logger
# delayed imports
_User = None
_Object = None
_ObjectDB = None
_Script = None
_ScriptDB = None
_HelpEntry = None
_Msg = None
_Player = None
_PlayerDB = None
_to_object = None
_Channel = None
_channelhandler = None
# limit symbol import from API # limit symbol import from API
__all__ = ("create_object", "create_script", "create_help_entry", "create_message", "create_channel", "create_player") __all__ = ("create_object", "create_script", "create_help_entry", "create_message", "create_channel", "create_player")
GA = object.__getattribute__ _GA = object.__getattribute__
# #
# Game Object creation # Game Object creation
# #
def create_object(typeclass, key=None, location=None, def create_object(typeclass, key=None, location=None,
home=None, player=None, permissions=None, locks=None, home=None, player=None, permissions=None, locks=None,
aliases=None, destination=None, report_to=None): aliases=None, destination=None, report_to=None):
@ -57,21 +71,23 @@ def create_object(typeclass, key=None, location=None,
containing the error message. If set, this method will return containing the error message. If set, this method will return
None upon errors. None upon errors.
""" """
# deferred import to avoid loops global _Object, _ObjectDB
from src.objects.objects import Object if not _Object:
from src.objects.models import ObjectDB from src.objects.objects import Object as _Object
if not _ObjectDB:
from src.objects.models import ObjectDB as _ObjectDB
if not typeclass: if not typeclass:
typeclass = settings.BASE_OBJECT_TYPECLASS typeclass = settings.BASE_OBJECT_TYPECLASS
elif isinstance(typeclass, ObjectDB): elif isinstance(typeclass, _ObjectDB):
# this is already an objectdb instance, extract its typeclass # this is already an objectdb instance, extract its typeclass
typeclass = typeclass.typeclass.path typeclass = typeclass.typeclass.path
elif isinstance(typeclass, Object) or utils.inherits_from(typeclass, Object): elif isinstance(typeclass, _Object) or utils.inherits_from(typeclass, _Object):
# this is already an object typeclass, extract its path # this is already an object typeclass, extract its path
typeclass = typeclass.path typeclass = typeclass.path
# create new database object # create new database object
new_db_object = ObjectDB() new_db_object = _ObjectDB()
# assign the typeclass # assign the typeclass
typeclass = utils.to_unicode(typeclass) typeclass = utils.to_unicode(typeclass)
@ -82,20 +98,20 @@ def create_object(typeclass, key=None, location=None,
if key: if key:
new_db_object.key = key new_db_object.key = key
else: else:
new_db_object.key = "#%i" % new_db_object.id new_db_object.key = "#%i" % new_db_object.dbid
# this will either load the typeclass or the default one # this will either load the typeclass or the default one
new_object = new_db_object.typeclass new_object = new_db_object.typeclass
if not GA(new_object, "is_typeclass")(typeclass, exact=True): if not _GA(new_object, "is_typeclass")(typeclass, exact=True):
# this will fail if we gave a typeclass as input and it still gave us a default # this will fail if we gave a typeclass as input and it still gave us a default
SharedMemoryModel.delete(new_db_object) SharedMemoryModel.delete(new_db_object)
if report_to: if report_to:
GA(report_to, "msg")("Error creating %s (%s):\n%s" % (new_db_object.key, typeclass, _GA(report_to, "msg")("Error creating %s (%s):\n%s" % (new_db_object.key, typeclass,
GA(new_db_object, "typeclass_last_errmsg"))) _GA(new_db_object, "typeclass_last_errmsg")))
return None return None
else: else:
raise Exception(GA(new_db_object, "typeclass_last_errmsg")) raise Exception(_GA(new_db_object, "typeclass_last_errmsg"))
# from now on we can use the typeclass object # from now on we can use the typeclass object
# as if it was the database object. # as if it was the database object.
@ -117,7 +133,7 @@ def create_object(typeclass, key=None, location=None,
if permissions: if permissions:
new_object.permissions = permissions new_object.permissions = permissions
if locks: if locks:
new_object.locks.add(locks) new_object.locks.add(locks)
if aliases: if aliases:
new_object.aliases = aliases new_object.aliases = aliases
@ -125,11 +141,11 @@ def create_object(typeclass, key=None, location=None,
if home: if home:
new_object.home = home new_object.home = home
else: else:
new_object.home = settings.CHARACTER_DEFAULT_HOME new_object.home = settings.CHARACTER_DEFAULT_HOME
if location: if location:
new_object.move_to(location, quiet=True) new_object.move_to(location, quiet=True)
else: else:
# rooms would have location=None. # rooms would have location=None.
new_object.location = None new_object.location = None
@ -174,23 +190,23 @@ def create_script(typeclass, key=None, obj=None, locks=None,
error will be raised. If set, this method will error will be raised. If set, this method will
return None upon errors. return None upon errors.
""" """
global _Script, _ScriptDB
# deferred import to avoid loops. if not _Script:
from src.scripts.scripts import Script from src.scripts.scripts import Script as _Script
#print "in create_script", typeclass if not _ScriptDB:
from src.scripts.models import ScriptDB from src.scripts.models import ScriptDB as _ScriptDB
if not typeclass: if not typeclass:
typeclass = settings.BASE_SCRIPT_TYPECLASS typeclass = settings.BASE_SCRIPT_TYPECLASS
elif isinstance(typeclass, ScriptDB): elif isinstance(typeclass, _ScriptDB):
# this is already an scriptdb instance, extract its typeclass # this is already an scriptdb instance, extract its typeclass
typeclass = typeclass.typeclass.path typeclass = typeclass.typeclass.path
elif isinstance(typeclass, Script) or utils.inherits_from(typeclass, Script): elif isinstance(typeclass, _Script) or utils.inherits_from(typeclass, _Script):
# this is already an object typeclass, extract its path # this is already an object typeclass, extract its path
typeclass = typeclass.path typeclass = typeclass.path
# create new database script # create new database script
new_db_script = ScriptDB() new_db_script = _ScriptDB()
# assign the typeclass # assign the typeclass
typeclass = utils.to_unicode(typeclass) typeclass = utils.to_unicode(typeclass)
@ -206,15 +222,15 @@ def create_script(typeclass, key=None, obj=None, locks=None,
# this will either load the typeclass or the default one # this will either load the typeclass or the default one
new_script = new_db_script.typeclass new_script = new_db_script.typeclass
if not GA(new_db_script, "is_typeclass")(typeclass, exact=True): if not _GA(new_db_script, "is_typeclass")(typeclass, exact=True):
# this will fail if we gave a typeclass as input and it still gave us a default # this will fail if we gave a typeclass as input and it still gave us a default
SharedMemoryModel.delete(new_db_script) SharedMemoryModel.delete(new_db_script)
if report_to: if report_to:
GA(report_to, "msg")("Error creating %s (%s): %s" % (new_db_script.key, typeclass, _GA(report_to, "msg")("Error creating %s (%s): %s" % (new_db_script.key, typeclass,
GA(new_db_script, "typeclass_last_errmsg"))) _GA(new_db_script, "typeclass_last_errmsg")))
return None return None
else: else:
raise Exception(GA(new_db_script, "typeclass_last_errmsg")) raise Exception(_GA(new_db_script, "typeclass_last_errmsg"))
if obj: if obj:
try: try:
@ -262,10 +278,12 @@ def create_help_entry(key, entrytext, category="General", locks=None):
general help on the game, more extensive info, in-game setting information general help on the game, more extensive info, in-game setting information
and so on. and so on.
""" """
global _HelpEntry
if not _HelpEntry:
from src.help.models import HelpEntry as _HelpEntry
from src.help.models import HelpEntry
try: try:
new_help = HelpEntry() new_help = _HelpEntry()
new_help.key = key new_help.key = key
new_help.entrytext = entrytext new_help.entrytext = entrytext
new_help.help_category = category new_help.help_category = category
@ -308,13 +326,17 @@ def create_message(senderobj, message, channels=None,
at the same time, it's up to the command definitions to limit this as at the same time, it's up to the command definitions to limit this as
desired. desired.
""" """
from src.comms.models import Msg global _Msg, _PlayerDB, _to_object
from src.players.models import PlayerDB if not _Msg:
from src.comms.managers import to_object from src.comms.models import Msg as _Msg
if not _PlayerDB:
from src.players.models import PlayerDB as _PlayerDB
if not _to_object:
from src.comms.managers import to_object as _to_object
def to_player(obj): def to_player(obj):
"Make sure the object is a player object" "Make sure the object is a player object"
if isinstance(obj, PlayerDB): if isinstance(obj, _PlayerDB):
return obj return obj
elif hasattr(obj, 'user'): elif hasattr(obj, 'user'):
return obj.dbobj return obj.dbobj
@ -327,7 +349,7 @@ def create_message(senderobj, message, channels=None,
# we don't allow empty messages. # we don't allow empty messages.
return return
new_message = Msg() new_message = _Msg()
new_message.sender = to_player(senderobj) new_message.sender = to_player(senderobj)
new_message.message = message new_message.message = message
new_message.save() new_message.save()
@ -335,7 +357,7 @@ def create_message(senderobj, message, channels=None,
if not utils.is_iter(channels): if not utils.is_iter(channels):
channels = [channels] channels = [channels]
new_message.channels = [channel for channel in new_message.channels = [channel for channel in
[to_object(channel, objtype='channel') [_to_object(channel, objtype='channel')
for channel in channels] if channel] for channel in channels] if channel]
if receivers: if receivers:
#print "Found receiver:", receivers #print "Found receiver:", receivers
@ -343,7 +365,7 @@ def create_message(senderobj, message, channels=None,
receivers = [receivers] receivers = [receivers]
#print "to_player: %s" % to_player(receivers[0]) #print "to_player: %s" % to_player(receivers[0])
new_message.receivers = [to_player(receiver) for receiver in new_message.receivers = [to_player(receiver) for receiver in
[to_object(receiver) for receiver in receivers] [_to_object(receiver) for receiver in receivers]
if receiver] if receiver]
if locks: if locks:
new_message.locks.add(locks) new_message.locks.add(locks)
@ -366,11 +388,13 @@ def create_channel(key, aliases=None, desc=None,
aliases - list of alternative (likely shorter) keynames. aliases - list of alternative (likely shorter) keynames.
locks - lock string definitions locks - lock string definitions
""" """
global _Channel, _channelhandler
from src.comms.models import Channel if not _Channel:
from src.comms import channelhandler from src.comms.models import Channel as _Channel
if not _channelhandler:
from src.comms import channelhandler as _channelhandler
try: try:
new_channel = Channel() new_channel = _Channel()
new_channel.key = key new_channel.key = key
if aliases: if aliases:
if not utils.is_iter(aliases): if not utils.is_iter(aliases):
@ -385,7 +409,7 @@ def create_channel(key, aliases=None, desc=None,
if locks: if locks:
new_channel.locks.add(locks) new_channel.locks.add(locks)
new_channel.save() new_channel.save()
channelhandler.CHANNELHANDLER.add_channel(new_channel) _channelhandler.CHANNELHANDLER.add_channel(new_channel)
return new_channel return new_channel
channel = create_channel channel = create_channel
@ -445,9 +469,11 @@ def create_player(name, email, password,
# The system should already have checked so the name/email # The system should already have checked so the name/email
# isn't already registered, and that the password is ok before # isn't already registered, and that the password is ok before
# getting here. # getting here.
global _PlayerDB, _Player
from src.players.models import PlayerDB if not _PlayerDB:
from src.players.player import Player from src.players.models import PlayerDB as _PlayerDB
if not _Player:
from src.players.player import Player as _Player
if not email: if not email:
email = "dummy@dummy.com" email = "dummy@dummy.com"
@ -462,17 +488,17 @@ def create_player(name, email, password,
try: try:
if not typeclass: if not typeclass:
typeclass = settings.BASE_PLAYER_TYPECLASS typeclass = settings.BASE_PLAYER_TYPECLASS
elif isinstance(typeclass, PlayerDB): elif isinstance(typeclass, _PlayerDB):
# this is already an objectdb instance, extract its typeclass # this is already an objectdb instance, extract its typeclass
typeclass = typeclass.typeclass.path typeclass = typeclass.typeclass.path
elif isinstance(typeclass, Player) or utils.inherits_from(typeclass, Player): elif isinstance(typeclass, _Player) or utils.inherits_from(typeclass, _Player):
# this is already an object typeclass, extract its path # this is already an object typeclass, extract its path
typeclass = typeclass.path typeclass = typeclass.path
if player_dbobj: if player_dbobj:
new_db_player = player_dbobj new_db_player = player_dbobj
else: else:
new_db_player = PlayerDB(db_key=name, user=new_user) new_db_player = _PlayerDB(db_key=name, user=new_user)
new_db_player.save() new_db_player.save()
# assign the typeclass # assign the typeclass
@ -482,15 +508,15 @@ def create_player(name, email, password,
# this will either load the typeclass or the default one # this will either load the typeclass or the default one
new_player = new_db_player.typeclass new_player = new_db_player.typeclass
if not GA(new_db_player, "is_typeclass")(typeclass, exact=True): if not _GA(new_db_player, "is_typeclass")(typeclass, exact=True):
# this will fail if we gave a typeclass as input and it still gave us a default # this will fail if we gave a typeclass as input and it still gave us a default
SharedMemoryModel.delete(new_db_player) SharedMemoryModel.delete(new_db_player)
if report_to: if report_to:
GA(report_to, "msg")("Error creating %s (%s):\n%s" % (new_db_player.key, typeclass, _GA(report_to, "msg")("Error creating %s (%s):\n%s" % (new_db_player.key, typeclass,
GA(new_db_player, "typeclass_last_errmsg"))) _GA(new_db_player, "typeclass_last_errmsg")))
return None return None
else: else:
raise Exception(GA(new_db_player, "typeclass_last_errmsg")) raise Exception(_GA(new_db_player, "typeclass_last_errmsg"))
new_player.basetype_setup() # setup the basic locks and cmdset new_player.basetype_setup() # setup the basic locks and cmdset
# call hook method (may override default permissions) # call hook method (may override default permissions)