From 88c0087fbd3b90690b5fc680e1262fda288ea75b Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 25 Mar 2012 13:09:17 +0200 Subject: [PATCH] Updated game/ for the ev API. There will likely be some changes happening in the game/folder, in the way new objects are inherited. This should use the API rather than inherit from the basecommand/baseobject modules (these will probably into the example folders as vanilla templates instead). --- ev.py | 48 ++++++++++++------- game/gamesrc/commands/basecmdset.py | 11 ++--- game/gamesrc/commands/basecommand.py | 8 ++-- .../commands/examples/cmdset_red_button.py | 4 +- game/gamesrc/objects/baseobjects.py | 10 ++-- game/gamesrc/objects/examples/red_button.py | 2 +- game/gamesrc/scripts/basescript.py | 2 +- .../gamesrc/scripts/examples/bodyfunctions.py | 2 +- .../scripts/examples/red_button_scripts.py | 2 +- game/gamesrc/world/examples/batch_code.py | 2 +- game/manage.py | 3 +- src/commands/default/__init__.py | 10 +++- src/utils/utils.py | 5 +- 13 files changed, 66 insertions(+), 43 deletions(-) diff --git a/ev.py b/ev.py index 6ade229545..2b78afb725 100644 --- a/ev.py +++ b/ev.py @@ -2,34 +2,40 @@ Central API for Evennia MUD/MUX/MU* system. +This basically a set of shortcuts to the main modules in src/. Import this from ./manage.py shell or set DJANGO_SETTINGS_MODULE manually for proper functionality. -This module tries to group the most commonly useful modules in a flat(ter) structure. -Some notes: + 1) You should import things excplicitly from the root of this module - you can generally + not use dot-notation to import deeper. Hence, to access a default command, you can do + the following: - 1) db_* are shortcuts to initiated versions of Evennia's django database managers (e.g. + import ev + ev.default_cmds.CmdLook + or + from ev import default_cmds + default_cmds.CmdLook + + 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. + 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 - various ways. Please note that - the evennia-specific methods in the 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 typeclass.dbobj, but - it's worth to remember. - 2) You -have- to use the methods of the "create" module to create new Typeclassed game + various ways. Please note that the evennia-specific methods in the 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 + typeclass.dbobj, but it's worth to remember this difference. + 3) You -have- to use the methods of the "create" module to create new 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 can be created normally, but the "create" module offers convenient methods for those too. - 3) The API accesses all relevant methods/classes, but might not always include all helper-methods + 4) The API accesses all relevant methods/classes, but might not always include all helper-methods 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 this API. -Philosophy is to keep the API as flat as possible, so as to not have to remember which nested -packages to traverse. Most of the important stuff should be made visible from this module. - -As said, one can of course still import from src/ directly should one prefer! - """ +import sys, os + # Stop erroneous direct run (would give a traceback since django is # not yet initialized) @@ -41,12 +47,20 @@ if __name__ == "__main__": To start the server, see game/manage.py and game/evennia.py. More help can be found at http://www.evennia.com. """ - import sys sys.exit() - +try: + f = open(os.path.dirname(os.path.abspath(__file__)) + os.sep + "VERSION", 'r') + __version__ = "Evennia %s-r%s" % (f.read().strip(), os.popen("hg id -i").read().strip()) + f.close() + del f +except IOError: + __version__ = "Evennia (unknown version)" +del sys, os # Start Evennia API (easiest is to import this module interactively to explore it) +README = "Evennia flat API. See the module header for usage information." + # help entries from src.help.models import HelpEntry db_helpentries = HelpEntry.objects diff --git a/game/gamesrc/commands/basecmdset.py b/game/gamesrc/commands/basecmdset.py index 93b602de42..0621c9dc4a 100644 --- a/game/gamesrc/commands/basecmdset.py +++ b/game/gamesrc/commands/basecmdset.py @@ -18,15 +18,14 @@ new cmdset class. """ -from src.commands.cmdset import CmdSet -from src.commands.default import cmdset_default, cmdset_unloggedin, cmdset_ooc -from game.gamesrc.commands.basecommand import Command +from ev import CmdSet, Command +from ev import default_cmds #from contrib import menusystem, lineeditor #from contrib import misc_commands #from contrib import chargen -class DefaultCmdSet(cmdset_default.DefaultCmdSet): +class DefaultCmdSet(default_cmds.DefaultCmdSet): """ This is an example of how to overload the default command set defined in src/commands/default/cmdset_default.py. @@ -52,7 +51,7 @@ class DefaultCmdSet(cmdset_default.DefaultCmdSet): #self.add(lineeditor.CmdEditor()) #self.add(misc_commands.CmdQuell()) -class UnloggedinCmdSet(cmdset_unloggedin.UnloggedinCmdSet): +class UnloggedinCmdSet(default_cmds.UnloggedinCmdSet): """ This is an example of how to overload the command set of the unlogged in commands, defined in @@ -76,7 +75,7 @@ class UnloggedinCmdSet(cmdset_unloggedin.UnloggedinCmdSet): # any commands you add below will overload the default ones. # -class OOCCmdSet(cmdset_ooc.OOCCmdSet): +class OOCCmdSet(default_cmds.OOCCmdSet): """ This is set is available to the player when they have no character connected to them (i.e. they are out-of-character, ooc). diff --git a/game/gamesrc/commands/basecommand.py b/game/gamesrc/commands/basecommand.py index d5f1b51731..93b8ef987e 100644 --- a/game/gamesrc/commands/basecommand.py +++ b/game/gamesrc/commands/basecommand.py @@ -8,11 +8,11 @@ See src/commands/default/muxcommand.py for an example. """ -from src.commands.command import Command as BaseCommand -from src.commands.default.muxcommand import MuxCommand as BaseMuxCommand -from src.utils import utils +from ev import Command as BaseCommand +from ev import default_cmd +from ev import utils -class MuxCommand(BaseMuxCommand): +class MuxCommand(default_cmd.MuxCommand): """ This sets up the basis for a Evennia's 'MUX-like' command style. The idea is that most other Mux-related commands should diff --git a/game/gamesrc/commands/examples/cmdset_red_button.py b/game/gamesrc/commands/examples/cmdset_red_button.py index 111437f290..d9883267d5 100644 --- a/game/gamesrc/commands/examples/cmdset_red_button.py +++ b/game/gamesrc/commands/examples/cmdset_red_button.py @@ -8,8 +8,8 @@ cmdset - this way you can often re-use the commands too. """ import random -from src.commands.cmdset import CmdSet -from game.gamesrc.commands.basecommand import Command +from ev import CmdSet +from ev import Command # Some simple commands for the red button diff --git a/game/gamesrc/objects/baseobjects.py b/game/gamesrc/objects/baseobjects.py index d9a2046cab..f7faf814a2 100644 --- a/game/gamesrc/objects/baseobjects.py +++ b/game/gamesrc/objects/baseobjects.py @@ -20,11 +20,11 @@ New instances of Objects (inheriting from these typeclasses) are created with src.utils.create.create_object(typeclass, ...) where typeclass is the python path to the class you want to use. """ -from src.objects.objects import Object as BaseObject -from src.objects.objects import Character as BaseCharacter -from src.objects.objects import Room as BaseRoom -from src.objects.objects import Exit as BaseExit -from src.players.player import Player as BasePlayer +from ev import Object as BaseObject +from ev import Character as BaseCharacter +from ev import Room as BaseRoom +from ev import Exit as BaseExit +from ev import Player as BasePlayer class Object(BaseObject): """ diff --git a/game/gamesrc/objects/examples/red_button.py b/game/gamesrc/objects/examples/red_button.py index e030a7898e..7663561dfc 100644 --- a/game/gamesrc/objects/examples/red_button.py +++ b/game/gamesrc/objects/examples/red_button.py @@ -11,7 +11,7 @@ Create this button with Note that if you must drop the button before you can see its messages! """ import random -from game.gamesrc.objects.baseobjects import Object +from ev import Object from game.gamesrc.scripts.examples import red_button_scripts as scriptexamples from game.gamesrc.commands.examples import cmdset_red_button as cmdsetexamples diff --git a/game/gamesrc/scripts/basescript.py b/game/gamesrc/scripts/basescript.py index a006ff39be..950dd95001 100644 --- a/game/gamesrc/scripts/basescript.py +++ b/game/gamesrc/scripts/basescript.py @@ -15,7 +15,7 @@ src.utils.create.create_script(scriptclass, ...) where scriptclass is the python path to the specific class of script you want to use. """ -from src.scripts.scripts import Script as BaseScript +from ev import Script as BaseScript class Script(BaseScript): """ diff --git a/game/gamesrc/scripts/examples/bodyfunctions.py b/game/gamesrc/scripts/examples/bodyfunctions.py index 297ba4b6ef..4c4d6f7b6f 100644 --- a/game/gamesrc/scripts/examples/bodyfunctions.py +++ b/game/gamesrc/scripts/examples/bodyfunctions.py @@ -12,7 +12,7 @@ or you won't see any messages! """ import random -from game.gamesrc.scripts.basescript import Script +from ev import Script class BodyFunctions(Script): """ diff --git a/game/gamesrc/scripts/examples/red_button_scripts.py b/game/gamesrc/scripts/examples/red_button_scripts.py index e0e864ba6f..f935ac3458 100644 --- a/game/gamesrc/scripts/examples/red_button_scripts.py +++ b/game/gamesrc/scripts/examples/red_button_scripts.py @@ -6,7 +6,7 @@ red_button object type in gamesrc/types/examples. A few variations on uses of scripts are included. """ -from game.gamesrc.scripts.basescript import Script +from ev import Script from game.gamesrc.commands.examples import cmdset_red_button as cmdsetexamples # diff --git a/game/gamesrc/world/examples/batch_code.py b/game/gamesrc/world/examples/batch_code.py index 09ef65d8bf..8bfd7ee217 100644 --- a/game/gamesrc/world/examples/batch_code.py +++ b/game/gamesrc/world/examples/batch_code.py @@ -43,7 +43,7 @@ # everything in this block will be appended to the beginning of # all other #CODE blocks when they are executed. -from src.utils import create, search +from ev import create, search from game.gamesrc.objects.examples import red_button from game.gamesrc.objects import baseobjects diff --git a/game/manage.py b/game/manage.py index f93f9a7305..f3598063cd 100755 --- a/game/manage.py +++ b/game/manage.py @@ -15,7 +15,8 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # Get Evennia version #------------------------------------------------------------ try: - VERSION = open("%s%s%s" % (os.pardir, os.sep, 'VERSION')).readline().strip() + with open(os.pardir + os.sep + 'VERSION') as f: + VERSION = "%s-r%s" % (f.read().strip(), os.popen("hg id -i").read().strip()) except IOError: VERSION = "Unknown version" diff --git a/src/commands/default/__init__.py b/src/commands/default/__init__.py index a9e174ce13..de9b0251eb 100644 --- a/src/commands/default/__init__.py +++ b/src/commands/default/__init__.py @@ -1,5 +1,9 @@ """ -Central package for importing the default commands from the API. +Groups all default commands for access from the API. + +To use via ev API, import this module with + from ev import default_cmds, +you can then access the command classes as members of default_cmds. """ from src.commands.default.cmdset_default import DefaultCmdSet @@ -17,3 +21,7 @@ from src.commands.default.help import * from src.commands.default import syscommands from src.commands.default.system import * from src.commands.default.unloggedin import * + +del cmdset_default, cmdset_ooc, cmdset_unloggedin, muxcommand +del admin, batchprocess, building, comms, general, +del help, system, unloggedin diff --git a/src/utils/utils.py b/src/utils/utils.py index 469e562e6d..02606be569 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -222,9 +222,10 @@ def get_evennia_version(): """ Check for the evennia version info. """ - version_file_path = "%s%s%s" % (settings.BASE_PATH, os.sep, "VERSION") try: - return open(version_file_path).readline().strip('\n').strip() + with open(settings.BASE_PATH + os.sep + "VERSION") as f: + return "%s-r%s" % (f.read().strip(), os.popen("hg id -i").read().strip()) + return except IOError: return "Unknown version"