<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.
<spanid="evennia-utils-batchprocessors"></span><h1>evennia.utils.batchprocessors<aclass="headerlink"href="#module-evennia.utils.batchprocessors"title="Permalink to this headline">¶</a></h1>
<p>This module contains the core methods for the Batch-command- and
Batch-code-processors respectively. In short, these are two different ways to
build a game world using a normal text-editor without having to do so ‘on the
fly’ in-game. They also serve as an automatic backup so you can quickly
recreate a world also after a server reset. The functions in this module is
meant to form the backbone of a system called and accessed through game
commands.</p>
<p>The Batch-command processor is the simplest. It simply runs a list of in-game
commands in sequence by reading them from a text file. The advantage of this is
that the builder only need to remember the normal in-game commands. They are
also executing with full permission checks etc, making it relatively safe for
builders to use. The drawback is that in-game there is really a
builder-character walking around building things, and it can be important to
create rooms and objects in the right order, so the character can move between
them. Also objects that affects players (such as mobs, dark rooms etc) will
affect the building character too, requiring extra care to turn off/on.</p>
<p>The Batch-code processor is a more advanced system that accepts full
Python code, executing in chunks. The advantage of this is much more
power; practically anything imaginable can be coded and handled using
the batch-code processor. There is no in-game character that moves and
that can be affected by what is being built - the database is
populated on the fly. The drawback is safety and entry threshold - the
code is executed as would any server code, without mud-specific
permission-checks, and you have full access to modifying objects
etc. You also need to know Python and Evennia’s API. Hence it’s
recommended that the batch-code processor is limited only to
superusers or highly trusted staff.</p>
<sectionid="batch-command-processor-file-syntax">
<h2>Batch-command processor file syntax<aclass="headerlink"href="#batch-command-processor-file-syntax"title="Permalink to this headline">¶</a></h2>
<spanclass="c1"># Done, the box is in the warehouse! (this last comment is not necessary to</span>
<spanclass="c1"># close the drop command since it's the end of the file)</span>
</pre></div>
</div>
<p>An example batch file is <strong>contrib/examples/batch_example.ev</strong>.</p>
</section>
</section>
<sectionid="batch-code-processor-file-syntax">
<h2>Batch-code processor file syntax<aclass="headerlink"href="#batch-code-processor-file-syntax"title="Permalink to this headline">¶</a></h2>
<p>The Batch-code processor accepts full python modules (e.g. <strong>batch.py</strong>)
that looks identical to normal Python files. The difference from
importing and running any Python module is that the batch-code module
is loaded as a file and executed directly, so changes to the file will
apply immediately without a server @reload.</p>
<p>Optionally, one can add some special commented tokens to split the
execution of the code for the benefit of the batchprocessor’s
interactive- and debug-modes. This allows to conveniently step through
the code and re-run sections of it easily during development.</p>
<p>Code blocks are marked by commented tokens alone on a line:</p>
<ulclass="simple">
<li><p><strong>#HEADER</strong> - This denotes code that should be pasted at the top of all
other code. Multiple HEADER statements - regardless of where
it exists in the file - is the same as one big block.
Observe that changes to variables made in one block is not
preserved between blocks!</p></li>
<li><p><strong>#CODE</strong> - This designates a code block that will be executed like a
stand-alone piece of code together with any HEADER(s)
defined. It is mainly used as a way to mark stop points for
the interactive mode of the batchprocessor. If no CODE block
is defined in the module, the entire module (including HEADERS)
is assumed to be a CODE block.</p></li>
<li><p><strong>#INSERT path.filename</strong> - This imports another batch_code.py file and
runs it in the given position. The inserted file will retain
its own HEADERs which will not be mixed with the headers of
this file.</p></li>
</ul>
<p>Importing works as normal. The following variables are automatically
made available in the script namespace.</p>
<ulclass="simple">
<li><p><strong>caller</strong> - The object executing the batchscript</p></li>
<li><dlclass="simple">
<dt><strong>DEBUG</strong> - This is a boolean marking if the batchprocessor is running</dt><dd><p>in debug mode. It can be checked to e.g. delete created objects
when running a CODE block multiple times during testing.
(avoids creating a slew of same-named db objects)</p>
</dd>
</dl>
</li>
</ul>
<sectionid="example-batch-py-file">
<h3>Example batch.py file<aclass="headerlink"href="#example-batch-py-file"title="Permalink to this headline">¶</a></h3>
<spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"The object was created!"</span><spanclass="p">)</span>
<codeclass="sig-prename descclassname">evennia.utils.batchprocessors.</code><codeclass="sig-name descname">read_batchfile</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">pythonpath</span></em>, <emclass="sig-param"><spanclass="n">file_ending</span><spanclass="o">=</span><spanclass="default_value">'.py'</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#read_batchfile"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.read_batchfile"title="Permalink to this definition">¶</a></dt>
<dd><p>This reads the contents of a batch-file. Filename is considered
to be a python path to a batch file relative the directory
specified in <strong>settings.py</strong>.</p>
<p>file_ending specify which batchfile ending should be assumed (.ev
or .py). The ending should not be included in the python path.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><ulclass="simple">
<li><p><strong>pythonpath</strong> (<em>str</em>) – A dot-python path to a file.</p></li>
<li><p><strong>file_ending</strong> (<em>str</em>) – The file ending of this file (.ev or .py)</p></li>
</ul>
</dd>
<dtclass="field-even">Returns</dt>
<ddclass="field-even"><p><em>text (str)</em>– The text content of the batch file.</p>
</dd>
<dtclass="field-odd">Raises</dt>
<ddclass="field-odd"><p><strong>IOError</strong>– If problems reading file.</p>
<emclass="property">class </em><codeclass="sig-prename descclassname">evennia.utils.batchprocessors.</code><codeclass="sig-name descname">BatchCommandProcessor</code><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#BatchCommandProcessor"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.BatchCommandProcessor"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">parse_file</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">pythonpath</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#BatchCommandProcessor.parse_file"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.BatchCommandProcessor.parse_file"title="Permalink to this definition">¶</a></dt>
<dd><p>This parses the lines of a batch-command-file.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><p><strong>pythonpath</strong> (<em>str</em>) – The dot-python path to the file.</p>
</dd>
<dtclass="field-even">Returns</dt>
<ddclass="field-even"><p><em>list</em>– A list of all parsed commands with arguments, as strings.</p>
</dd>
</dl>
<pclass="rubric">Notes</p>
<p>Parsing follows the following rules:</p>
<olclass="arabic simple">
<li><p>A <strong>#</strong> at the beginning of a line marks the end of the command before
it. It is also a comment and any number of # can exist on
subsequent lines (but not inside comments).</p></li>
<li><p>#INSERT at the beginning of a line imports another
batch-cmd file file and pastes it into the batch file as if
it was written there.</p></li>
<li><p>Commands are placed alone at the beginning of a line and their
arguments are considered to be everything following (on any
number of lines) until the next comment line beginning with #.</p></li>
<li><p>Newlines are ignored in command definitions</p></li>
<li><p>A completely empty line in a command line definition is condered
a newline (so two empty lines is a paragraph).</p></li>
<li><p>Excess spaces and indents inside arguments are stripped.</p></li>
<codeclass="sig-prename descclassname">evennia.utils.batchprocessors.</code><codeclass="sig-name descname">tb_filename</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">tb</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#tb_filename"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.tb_filename"title="Permalink to this definition">¶</a></dt>
<dd><p>Helper to get filename from traceback</p>
</dd></dl>
<dlclass="py function">
<dtid="evennia.utils.batchprocessors.tb_iter">
<codeclass="sig-prename descclassname">evennia.utils.batchprocessors.</code><codeclass="sig-name descname">tb_iter</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">tb</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#tb_iter"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.tb_iter"title="Permalink to this definition">¶</a></dt>
<emclass="property">class </em><codeclass="sig-prename descclassname">evennia.utils.batchprocessors.</code><codeclass="sig-name descname">BatchCodeProcessor</code><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#BatchCodeProcessor"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.BatchCodeProcessor"title="Permalink to this definition">¶</a></dt>
<codeclass="sig-name descname">parse_file</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">pythonpath</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#BatchCodeProcessor.parse_file"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.BatchCodeProcessor.parse_file"title="Permalink to this definition">¶</a></dt>
<dd><p>This parses the lines of a batch-code file</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><p><strong>pythonpath</strong> (<em>str</em>) – The dot-python path to the file.</p>
</dd>
<dtclass="field-even">Returns</dt>
<ddclass="field-even"><p><p><em>list</em>–</p>
<dlclass="simple">
<dt>A list of all <strong>#CODE</strong> blocks, each with</dt><dd><p>prepended <strong>#HEADER</strong> block data. If no <strong>#CODE</strong>
blocks were found, this will be a list of one element
containing all code in the file (so a normal Python file).</p>
</dd>
</dl>
</p>
</dd>
</dl>
<pclass="rubric">Notes</p>
<p>Parsing is done according to the following rules:</p>
<olclass="arabic simple">
<li><p>Code before a #CODE/HEADER block are considered part of
the first code/header block or is the ONLY block if no
<strong>#CODE/HEADER</strong> blocks are defined.</p></li>
<li><p>Lines starting with #HEADER starts a header block (ends other blocks)</p></li>
<li><p>Lines starting with #CODE begins a code block (ends other blocks)</p></li>
<li><p>Lines starting with #INSERT are on form #INSERT filename. Code from
this file are processed with their headers <em>separately</em> before
being inserted at the point of the #INSERT.</p></li>
<li><p>Code after the last block is considered part of the last header/code
<codeclass="sig-name descname">code_exec</code><spanclass="sig-paren">(</span><emclass="sig-param"><spanclass="n">code</span></em>, <emclass="sig-param"><spanclass="n">extra_environ</span><spanclass="o">=</span><spanclass="default_value">None</span></em>, <emclass="sig-param"><spanclass="n">debug</span><spanclass="o">=</span><spanclass="default_value">False</span></em><spanclass="sig-paren">)</span><aclass="reference internal"href="../_modules/evennia/utils/batchprocessors.html#BatchCodeProcessor.code_exec"><spanclass="viewcode-link">[source]</span></a><aclass="headerlink"href="#evennia.utils.batchprocessors.BatchCodeProcessor.code_exec"title="Permalink to this definition">¶</a></dt>
<dd><p>Execute a single code block, including imports and appending
global vars.</p>
<dlclass="field-list simple">
<dtclass="field-odd">Parameters</dt>
<ddclass="field-odd"><ulclass="simple">
<li><p><strong>code</strong> (<em>str</em>) – Code to run.</p></li>
<li><p><strong>extra_environ</strong> (<em>dict</em>) – Environment variables to run with code.</p></li>
<li><p><strong>debug</strong> (<em>bool</em><em>, </em><em>optional</em>) – Set the DEBUG variable in the execution
namespace.</p></li>
</ul>
</dd>
<dtclass="field-even">Returns</dt>
<ddclass="field-even"><p><em>err (str or None)</em>– An error code or None (ok).</p>
<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.
</div>
<divclass="footer"role="contentinfo">
© Copyright 2023, The Evennia developer community.
Created using <ahref="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.