Using Python within a Python application.
Michael B. Trausch
Thu Nov 2 15:41:40 CET 2006
I am doing some searching and winding up a little bit confused. I have
a MUD client that I am writing using Python and wxWidgets, as some of
you may remember. What I am looking to do now, is add "trigger"
functionality to it. In essence, the application receives text from the
game, and if it is in any of the trigger definitions, it acts on it by
executing Python code that the user has associated with the action.
So, the question is, what is the safe way to do this? I have found the
'compile' function, which returns a code object(?) which I would want to
execute using 'exec'.
Here is a hypothetical example: I want to have a trigger that, when the
game sends the text "That really did HURT!", executes code to instruct
the character to ingest a potion to fix that problem.
In this situation, the user would provide 'That really did HURT!' as the
regular expression, which the game would examine, find that there are no
RE qualifiers in there (e.g., no $, ^, or similar to bind the string to
something else) and turn it into '.*(That really did HURT!).*' to match
it any time the game sends it (unless it breaks in the middle of a
transmission, which I wouldn't be quite sure how to handle, yet).
The associated code, let's say, would be something like:
Which would send the text to the game.
Eventually, I would want to support complex things, like handling $1 or
$2 which might be associated with saved portions of the regex, but I
will get there later.
The question is really two-fold: How can I safely permit the user to
specify Python to be used as part of the game, and how can I create a
module that is used for the user's required functions (e.g., Tmud.Send,
as an example) without requiring each trigger to 'import' something?
I seem to be able to find tons of information on embedding Python in
C/C++/compiled language programs, but not much on safely executing it
within a Python program. What I really want to do is prevent them from
calling anything that would affect the game client's runtime environment
-- at least from a trigger. I want to later have some sort of "plug in"
mechanism that people could use to create graphs/charts/meters of
in-game events, but not yet, and that would be separate.
Any ideas? Thanks in advance!
More information about the Python-list