Editing the ev.py API, removing db_* manager shortcuts in favour of a 'managers' container holding them all. It makes the API slightly less "flat", but makes for a cleaner interface.

This commit is contained in:
Griatch 2012-03-31 16:50:53 +02:00
parent c728524c72
commit 551a91caef
3 changed files with 532 additions and 496 deletions

View file

@ -31,7 +31,7 @@ the initial splash screen.
import re import re
import traceback import traceback
from django.conf import settings from django.conf import settings
from ev import db_players, db_serverconfigs, db_channels from ev import managers
from ev import utils, logger, create_player from ev import utils, logger, create_player
from ev import Command, CmdSet from ev import Command, CmdSet
from ev import syscmdkeys from ev import syscmdkeys
@ -75,7 +75,7 @@ class CmdUsernameSelect(Command):
locks = "cmd:all()" locks = "cmd:all()"
def func(self): def func(self):
"Execute the command" "Execute the command"
player = db_players.get_player_from_name(self.args) player = managers.players.get_player_from_name(self.args)
if not player: if not player:
self.caller.msg("{rThis account name couldn't be found. Did you create it? If you did, make sure you spelled it right (case doesn't matter).{n") self.caller.msg("{rThis account name couldn't be found. Did you create it? If you did, make sure you spelled it right (case doesn't matter).{n")
self.menutree.goto("node1a") self.menutree.goto("node1a")
@ -115,7 +115,7 @@ class CmdPasswordSelect(Command):
return return
# before going on, check eventual bans # before going on, check eventual bans
bans = db_serverconfigs.conf("server_bans") bans = managers.serverconfigs.conf("server_bans")
if bans and (any(tup[0]==player.name for tup in bans) if bans and (any(tup[0]==player.name for tup in bans)
or or
any(tup[2].match(player.sessions[0].address[0]) for tup in bans if tup[2])): any(tup[2].match(player.sessions[0].address[0]) for tup in bans if tup[2])):
@ -160,7 +160,7 @@ class CmdUsernameCreate(Command):
its and @/./+/-/_ only.{n") # this echoes the restrictions made by django's auth module. its and @/./+/-/_ only.{n") # this echoes the restrictions made by django's auth module.
self.menutree.goto("node2a") self.menutree.goto("node2a")
return return
if db_players.get_player_from_name(playername): if managers.players.get_player_from_name(playername):
self.caller.msg("\n\r {rAccount name %s already exists.{n" % playername) self.caller.msg("\n\r {rAccount name %s already exists.{n" % playername)
self.menutree.goto("node2a") self.menutree.goto("node2a")
return return
@ -216,7 +216,7 @@ class CmdPasswordCreate(Command):
# join the new player to the public channel # join the new player to the public channel
pchanneldef = settings.CHANNEL_PUBLIC pchanneldef = settings.CHANNEL_PUBLIC
if pchanneldef: if pchanneldef:
pchannel = db_channels.get_channel(pchanneldef[0]) pchannel = managers.channels.get_channel(pchanneldef[0])
if not pchannel.connect_to(new_player): if not pchannel.connect_to(new_player):
string = "New player '%s' could not connect to public channel!" % new_player.key string = "New player '%s' could not connect to public channel!" % new_player.key
logger.log_errmsg(string) logger.log_errmsg(string)

152
ev.py
View file

@ -2,9 +2,8 @@
Central API for the Evennia MUD/MUX/MU* creation system. Central API for the Evennia MUD/MUX/MU* creation system.
This basically a set of shortcuts to the main modules in src/. Import this This basically a set of shortcuts to the main modules in src/. Import this from your code or
from your code or explore it interactively from ./manage.py shell (or a normal explore it interactively from a python shell.
python shell if you set DJANGO_SETTINGS_MODULE manually).
Notes: Notes:
@ -20,21 +19,29 @@ Notes:
But trying to import CmdLook directly with "from ev.default_cmds import CmdLook" will But trying to import CmdLook directly with "from ev.default_cmds import CmdLook" will
not work since default_cmds is a property on the "ev" module, not a module of its own. not work since default_cmds is a property on the "ev" module, not a module of its own.
2) db_* are shortcuts to initiated versions of Evennia's django database managers (e.g.
db_objects is an alias for ObjectDB.objects). These allows for exploring the database in 2) "managers" is a container object that contains shortcuts to initiated versions of Evennia's django
various ways. Please note that the evennia-specific methods in the managers return database managers (e.g. managers.objects is an alias for ObjectDB.objects). These allow
typeclasses (or lists of typeclasses), whereas the default django ones (filter etc) for exploring the database in various ways. To use in code, do 'from ev import managers', then
access the managers on the managers object. Please note that the evennia-specific methods in
managers return typeclasses (or lists of typeclasses), whereas the default django ones (filter etc)
return database objects. You can convert between the two easily via dbobj.typeclass and return database objects. You can convert between the two easily via dbobj.typeclass and
typeclass.dbobj, but it's worth to remember this difference. typeclass.dbobj, but it's worth to remember this difference.
3) You -have- to use the create_* functions (shortcuts to src.utils.create) to create new
3) "syscmdkeys" is a container object holding the names of system commands. Import with
'from ev import syscmdkeys', then access the variables on the syscmdkeys object.
4) You -have- to use the create_* functions (shortcuts to src.utils.create) to create new
Typeclassed game entities (Objects, Scripts or Players). Just initializing e.g. the Player class will Typeclassed game entities (Objects, Scripts or Players). Just initializing e.g. the Player class will
-not- set up Typeclasses correctly and will lead to errors. Other types of database objects -not- set up Typeclasses correctly and will lead to errors. Other types of database objects
can be created normally, but there are conveniant create_* functions for those too, making can be created normally, but there are conveniant create_* functions for those too, making
some more error checking. some more error checking.
4) "settings" links to Evennia's game/settings file. "settings_full" shows all of django's available
5) "settings" links to Evennia's game/settings file. "settings_full" shows all of django's available
settings. Note that you cannot change settings from here in a meaningful way, you need to update settings. Note that you cannot change settings from here in a meaningful way, you need to update
game/settings.py and restart the server. game/settings.py and restart the server.
5) The API accesses all relevant and most-neeeded functions/classes from src/, but might not
6) The API accesses all relevant and most-neeeded functions/classes from src/, but might not
always include all helper-functions referenced from each such entity. To get to those, access always include all helper-functions referenced from each such entity. To get to those, access
the modules in src/ directly. You can always do this anyway, if you do not want to go through the modules in src/ directly. You can always do this anyway, if you do not want to go through
this API. this API.
@ -43,8 +50,10 @@ Notes:
import sys, os import sys, os
######################################################################
# Stop erroneous direct run (would give a traceback since django is # Stop erroneous direct run (would give a traceback since django is
# not yet initialized) # not yet initialized)
######################################################################
if __name__ == "__main__": if __name__ == "__main__":
info = __doc__ + \ info = __doc__ + \
@ -59,8 +68,10 @@ if __name__ == "__main__":
print info print info
sys.exit() sys.exit()
######################################################################
# make sure settings is available, also if starting this API stand-alone # make sure settings is available, also if starting this API stand-alone
# make settings available, and also the full django settings # make settings available, and also the full django settings
######################################################################
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from django.core.management import setup_environ from django.core.management import setup_environ
@ -69,7 +80,9 @@ setup_environ(settings)
del setup_environ del setup_environ
from django.conf import settings as settings_full from django.conf import settings as settings_full
######################################################################
# set Evennia version in __version__ property # set Evennia version in __version__ property
######################################################################
try: try:
f = open(os.path.dirname(os.path.abspath(__file__)) + os.sep + "VERSION", 'r') f = open(os.path.dirname(os.path.abspath(__file__)) + os.sep + "VERSION", 'r')
@ -80,29 +93,88 @@ except IOError:
__version__ = "Evennia (unknown version)" __version__ = "Evennia (unknown version)"
del sys, os del sys, os
# ######################################################################
# Start Evennia API (easiest is to import this module interactively to explore it) # Start Evennia API (easiest is to import this module interactively to
# # explore it)
######################################################################
README = __doc__ README = __doc__
# help entries # help entries
from src.help.models import HelpEntry from src.help.models import HelpEntry
db_helpentries = HelpEntry.objects
# players # players
from src.players.player import Player from src.players.player import Player
from src.players.models import PlayerDB, PlayerAttribute, PlayerNick from src.players.models import PlayerDB, PlayerAttribute, PlayerNick
db_players = PlayerDB.objects
#db_playerattrs = PlayerAttribute.objects
#db_playernicks = PlayerNick.objects
del PlayerDB, PlayerAttribute, PlayerNick
# commands # commands
from src.commands.command import Command from src.commands.command import Command
from src.commands.cmdset import CmdSet from src.commands.cmdset import CmdSet
from src.commands import default as default_cmds from src.commands import default as default_cmds
# locks
from src.locks import lockfuncs
# scripts
from src.scripts.scripts import Script
# comms
from src.comms.models import Msg, Channel, PlayerChannelConnection, ExternalChannelConnection
# objects
from src.objects.objects import Object, Character, Room, Exit
# utils
from src.utils.search import *
from src.utils.create import *
from src.utils import logger
from src.utils import utils
from src.utils import gametime
from src.utils import ansi
######################################################################
# API containers
######################################################################
class DBmanagers(object):
"""
Links to instantiated database managers.
helpentry - HelpEntry.objects
players - PlayerDB.objects
scripts - ScriptDB.objects
msgs - Msg.objects
channels - Channel.objects
connections - PlayerChannelConnection.objects
externalconnections - ExternalChannelConnection.objects
objects - ObjectDB.objects
Use by doing "from ev import managers",
you can then access the desired manager
on the imported managers object.
"""
from src.help.models import HelpEntry
from src.players.models import PlayerDB
from src.scripts.models import ScriptDB
from src.comms.models import Msg, Channel, PlayerChannelConnection, ExternalChannelConnection
from src.objects.models import ObjectDB
from src.server.models import ServerConfig
helpentry = HelpEntry.objects
players = PlayerDB.objects
scripts = ScriptDB.objects
msgs = Msg.objects
channels = Channel.objects
connections = PlayerChannelConnection.objects
externalconnections = ExternalChannelConnection.objects
objects = ObjectDB.objects
serverconfigs = ServerConfig.objects
del HelpEntry, PlayerDB, ScriptDB, Msg, Channel, PlayerChannelConnection,
del ExternalChannelConnection, ObjectDB, ServerConfig
managers = DBmanagers()
del DBmanagers
class SystemCmds(object): class SystemCmds(object):
""" """
Creating commands with keys set to these constants will make Creating commands with keys set to these constants will make
@ -118,6 +190,9 @@ class SystemCmds(object):
first command when a player connects to first command when a player connects to
the server. the server.
To access in code, do 'from ev import syscmdkeys' then
access the properties on the imported syscmdkeys object.
""" """
from src.commands import cmdhandler from src.commands import cmdhandler
CMD_NOINPUT = cmdhandler.CMD_NOINPUT CMD_NOINPUT = cmdhandler.CMD_NOINPUT
@ -127,43 +202,4 @@ class SystemCmds(object):
CMD_LOGINSTART = cmdhandler.CMD_LOGINSTART CMD_LOGINSTART = cmdhandler.CMD_LOGINSTART
del cmdhandler del cmdhandler
syscmdkeys = SystemCmds() syscmdkeys = SystemCmds()
del SystemCmds
# locks
from src.locks import lockfuncs
# scripts
from src.scripts.scripts import Script
from src.scripts.models import ScriptDB, ScriptAttribute
db_scripts = ScriptDB.objects
#db_scriptattrs = ScriptAttribute.objects
del ScriptDB, ScriptAttribute
# comms
from src.comms.models import Msg, Channel, PlayerChannelConnection, ExternalChannelConnection
db_msgs = Msg.objects
db_channels = Channel.objects
db_connections = PlayerChannelConnection.objects
db_externalconnections = ExternalChannelConnection.objects
# objects
from src.objects.objects import Object, Character, Room, Exit
from src.objects.models import ObjAttribute, Alias, ObjectNick, ObjectDB
db_objects = ObjectDB.objects
#db_aliases = Alias.objects
#db_objnicks = ObjectNick.objects
#db_objattrs = ObjAttribute.objects
del ObjAttribute, Alias, ObjectNick, ObjectDB
# server
from src.server.models import ServerConfig
db_serverconfigs = ServerConfig.objects
del ServerConfig
# utils
from src.utils.search import *
from src.utils.create import *
from src.utils import logger
from src.utils import utils
from src.utils import gametime
from src.utils import ansi