embedded in a string on the form <codeclass="docutils literal notranslate"><spanclass="pre">$funcname(args,</span><spanclass="pre">kwargs)</span></code>. Under the hood, this will
lead to a call to a Python function you control. The inline function call will be replaced by
<p>Next, just pass a string into the parser, optionally containing <codeclass="docutils literal notranslate"><spanclass="pre">$func(...)</span></code> markers:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">parser</span><spanclass="o">.</span><spanclass="n">parse</span><spanclass="p">(</span><spanclass="s2">"We have that 4 x 4 x 4 is $pow(4, power=3)."</span><spanclass="p">)</span>
<p>To show a <codeclass="docutils literal notranslate"><spanclass="pre">$func()</span></code> verbatim in your code without parsing it, escape it as either <codeclass="docutils literal notranslate"><spanclass="pre">$$func()</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">\$func()</span></code>:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">parser</span><spanclass="o">.</span><spanclass="n">parse</span><spanclass="p">(</span><spanclass="s2">"This is an escaped $$pow(4) and so is this \$pow(3)"</span><spanclass="p">)</span>
callable is provided the <aclass="reference internal"href="Sessions.html"><spanclass="doc std std-doc">Session</span></a> of the object receiving the message. This potentially
<li><p><em>Prototype values</em>. A <aclass="reference internal"href="Prototypes.html"><spanclass="doc std std-doc">Prototype</span></a> dict’s values are run through the parser such that every
the outgoing string is parsed for special <codeclass="docutils literal notranslate"><spanclass="pre">$You()</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">$conj()</span></code> callables to decide if a given recipient
should see “You” or the character’s name.</p></li>
</ul>
<divclass="admonition important">
<pclass="admonition-title">Important</p>
<p>The inline-function parser is not intended as a ‘softcode’ programming language. It does not
have things like loops and conditionals, for example. While you could in principle extend it to
do very advanced things and allow builders a lot of power, all-out coding is something
Evennia expects you to do in a proper text editor, outside of the game, not from inside it.</p>
<p>Here, <codeclass="docutils literal notranslate"><spanclass="pre">callables</span></code> points to a collection of normal Python functions (see next section) for you to make
available to the parser as you parse strings with it. It can either be</p>
<ulclass="simple">
<li><p>A <codeclass="docutils literal notranslate"><spanclass="pre">dict</span></code> of <codeclass="docutils literal notranslate"><spanclass="pre">{"functionname":</span><spanclass="pre">callable,</span><spanclass="pre">...}</span></code>. This allows you do pick and choose exactly which callables
to include and how they should be named. Do you want a callable to be available under more than one name?
Just add it multiple times to the dict, with a different key.</p></li>
<li><p>A <codeclass="docutils literal notranslate"><spanclass="pre">module</span></code> or (more commonly) a <codeclass="docutils literal notranslate"><spanclass="pre">python-path</span></code> to a module. This module can define a dict
<codeclass="docutils literal notranslate"><spanclass="pre">FUNCPARSER_CALLABLES</span><spanclass="pre">=</span><spanclass="pre">{"funcname":</span><spanclass="pre">callable,</span><spanclass="pre">...}</span></code> - this will be imported and used like the <codeclass="docutils literal notranslate"><spanclass="pre">dict</span></code> above.
If no such variable is defined, <em>every</em> top-level function in the module (whose name doesn’t start with
an underscore <codeclass="docutils literal notranslate"><spanclass="pre">_</span></code>) will be considered a suitable callable. The name of the function will be the <codeclass="docutils literal notranslate"><spanclass="pre">$funcname</span></code>
by which it can be called.</p></li>
<li><p>A <codeclass="docutils literal notranslate"><spanclass="pre">list</span></code> of modules/paths. This allows you to pull in modules from many sources for your parsing.</p></li>
</ul>
<p>The other arguments to the parser:</p>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">raise_errors</span></code> - By default, any errors from a callable will be quietly ignored and the result
will be that the failing function call will show verbatim. If <codeclass="docutils literal notranslate"><spanclass="pre">raise_errors</span></code> is set,
then parsing will stop and whatever exception happened will be raised. It’d be up to you to handle
this properly.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">escape</span></code> - Returns a string where every <codeclass="docutils literal notranslate"><spanclass="pre">$func(...)</span></code> has been escaped as <codeclass="docutils literal notranslate"><spanclass="pre">\$func()</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">strip</span></code> - Remove all <codeclass="docutils literal notranslate"><spanclass="pre">$func(...)</span></code> calls from string (as if each returned <codeclass="docutils literal notranslate"><spanclass="pre">''</span></code>).</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">return_str</span></code> - When <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> (default), <codeclass="docutils literal notranslate"><spanclass="pre">parser</span></code> always returns a string. If <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>, it may return
the return value of a single function call in the string. This is the same as using the <codeclass="docutils literal notranslate"><spanclass="pre">.parse_to_any</span></code>
method.</p></li>
<li><p>The <codeclass="docutils literal notranslate"><spanclass="pre">**default/reserved_keywords</span></code> are optional and allow you to pass custom data into <em>every</em> function
call. This is great for including things like the current session or config options. Defaults can be
replaced if the user gives the same-named kwarg in the string’s function call. Reserved kwargs are always passed,
ignoring defaults or what the user passed. In addition, the <codeclass="docutils literal notranslate"><spanclass="pre">funcparser</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">raise_errors</span></code>
reserved kwargs are always passed - the first is a back-reference to the <codeclass="docutils literal notranslate"><spanclass="pre">FuncParser</span></code> instance and the second
is the <codeclass="docutils literal notranslate"><spanclass="pre">raise_errors</span></code> boolean passed into <codeclass="docutils literal notranslate"><spanclass="pre">FuncParser.parse</span></code>.</p></li>
</ul>
<p>Here’s an example of using the default/reserved keywords:</p>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">mydefault=2</span></code> kwarg could be overwritten if we made the call as <codeclass="docutils literal notranslate"><spanclass="pre">$test(mydefault=...)</span></code>
but <codeclass="docutils literal notranslate"><spanclass="pre">myreserved=[1,</span><spanclass="pre">2,</span><spanclass="pre">3]</span></code> will <em>always</em> be sent as-is and will override a call <codeclass="docutils literal notranslate"><spanclass="pre">$test(myreserved=...)</span></code>.
The <codeclass="docutils literal notranslate"><spanclass="pre">funcparser</span></code>/<codeclass="docutils literal notranslate"><spanclass="pre">raise_errors</span></code> kwargs are also always included as reserved kwargs.</p>
<div><p>The <codeclass="docutils literal notranslate"><spanclass="pre">*args</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">**kwargs</span></code> must always be included. If you are unsure how <codeclass="docutils literal notranslate"><spanclass="pre">*args</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">**kwargs</span></code> work in Python,
<aclass="reference external"href="https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3">read about them here</a>.</p>
</div></blockquote>
<p>The input from the innermost <codeclass="docutils literal notranslate"><spanclass="pre">$funcname(...)</span></code> call in your callable will always be a <codeclass="docutils literal notranslate"><spanclass="pre">str</span></code>. Here’s
an example of an <codeclass="docutils literal notranslate"><spanclass="pre">$toint</span></code> function; it converts numbers to integers.</p>
<p>What will enter the <codeclass="docutils literal notranslate"><spanclass="pre">$toint</span></code> callable (as <codeclass="docutils literal notranslate"><spanclass="pre">args[0]</span></code>) is the <em>string</em><codeclass="docutils literal notranslate"><spanclass="pre">"22.0"</span></code>. The function is responsible
for converting this to a number so that we can convert it to an integer. We must also properly handle invalid
inputs (like non-numbers).</p>
<p>If you want to mark an error, raise <codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.funcparser.ParsingError</span></code>. This stops the entire parsing
of the string and may or may not raise the exception depending on what you set <codeclass="docutils literal notranslate"><spanclass="pre">raise_errors</span></code> to when you
created the parser.</p>
<p>However, if you <em>nest</em> functions, the return of the innermost function may be something other than
a string. Let’s introduce the <codeclass="docutils literal notranslate"><spanclass="pre">$eval</span></code> function, which evaluates simple expressions using
<p>Since the <codeclass="docutils literal notranslate"><spanclass="pre">$eval</span></code> is the innermost call, it will get a string as input - the string <codeclass="docutils literal notranslate"><spanclass="pre">"10</span><spanclass="pre">*</span><spanclass="pre">2.2"</span></code>.
It evaluates this and returns the <codeclass="docutils literal notranslate"><spanclass="pre">float</span></code><codeclass="docutils literal notranslate"><spanclass="pre">22.0</span></code>. This time the outermost <codeclass="docutils literal notranslate"><spanclass="pre">$toint</span></code> will be called with
this <codeclass="docutils literal notranslate"><spanclass="pre">float</span></code> instead of with a string.</p>
<blockquote>
<div><p>It’s important to safely validate your inputs since users may end up nesting your callables in any order.
See the next section for useful tools to help with this.</p>
</div></blockquote>
<p>In these examples, the result will be embedded in the larger string, so the result of the entire parsing
<p>However, if you use the <codeclass="docutils literal notranslate"><spanclass="pre">parse_to_any</span></code> (or <codeclass="docutils literal notranslate"><spanclass="pre">parse(...,</span><spanclass="pre">return_str=True)</span></code>) and <em>don’t add any extra string around the outermost function call</em>,
you’ll get the return type of the outermost callable back:</p>
<aclass="reference internal"href="../api/evennia.utils.utils.html#evennia.utils.utils.safe_convert_to_types"title="evennia.utils.utils.safe_convert_to_types"><spanclass="xref myst py py-func">safe_convert_to_types</span></a>. This function
<p>Each converter should be a callable taking one argument - this will be the arg/kwarg-value to convert. The
special converter <codeclass="docutils literal notranslate"><spanclass="pre">"py"</span></code> will try to convert a string argument to a Python structure with the help of the
following tools (which you may also find useful to experiment with on your own):</p>
<ulclass="simple">
<li><p><aclass="reference external"href="https://docs.python.org/3.8/library/ast.html#ast.literal_eval">ast.literal_eval</a> is an in-built Python
with <codeclass="docutils literal notranslate"><spanclass="pre">+-/*</span></code> as well as do simple comparisons like <codeclass="docutils literal notranslate"><spanclass="pre">4</span><spanclass="pre">></span><spanclass="pre">3</span></code> and more. It does <em>not</em> accept more complex
containers like lists/dicts etc, so this and <codeclass="docutils literal notranslate"><spanclass="pre">literal_eval</span></code> are complementary to each other.</p></li>
</ul>
<divclass="admonition warning">
<pclass="admonition-title">Warning</p>
<p>It may be tempting to run use Python’s in-built <codeclass="docutils literal notranslate"><spanclass="pre">eval()</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">exec()</span></code> functions as converters since
these are able to convert any valid Python source code to Python. NEVER DO THIS unless you really, really
know that ONLY developers will ever modify the string going into the callable. The parser is intended
for untrusted users (if you were trusted you’d have access to Python already). Letting untrusted users
pass strings to <codeclass="docutils literal notranslate"><spanclass="pre">eval</span></code>/<codeclass="docutils literal notranslate"><spanclass="pre">exec</span></code> is a MAJOR security risk. It allows the caller to run arbitrary
Python code on your server. This is the path to maliciously deleted hard drives. Just don’t do it and
<h2>Default callables<aclass="headerlink"href="#default-callables"title="Permalink to this headline">¶</a></h2>
<p>These are some example callables you can import and add your parser. They are divided into
global-level dicts in <codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.funcparser</span></code>. Just import the dict(s) and merge/add one or
more to them when you create your <codeclass="docutils literal notranslate"><spanclass="pre">FuncParser</span></code> instance to have those callables be available.</p>
<h3><codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.funcparser.FUNCPARSER_CALLABLES</span></code><aclass="headerlink"href="#evennia-utils-funcparser-funcparser-callables"title="Permalink to this headline">¶</a></h3>
this uses <codeclass="docutils literal notranslate"><spanclass="pre">literal_eval</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">simple_eval</span></code> (see previous section) attemt to convert a string expression
to a python object. This handles e.g. lists of literals <codeclass="docutils literal notranslate"><spanclass="pre">[1,</span><spanclass="pre">2,</span><spanclass="pre">3]</span></code> and simple expressions like <codeclass="docutils literal notranslate"><spanclass="pre">"1</span><spanclass="pre">+</span><spanclass="pre">2"</span></code>.</p></li>
this adds/subtracts/multiplies and divides to elements together. While simple addition could be done with
<codeclass="docutils literal notranslate"><spanclass="pre">$eval</span></code>, this could for example be used also to add two lists together, which is not possible with <codeclass="docutils literal notranslate"><spanclass="pre">eval</span></code>;
for example <codeclass="docutils literal notranslate"><spanclass="pre">$add($eval([1,2,3]),</span><spanclass="pre">$eval([4,5,6]))</span><spanclass="pre">-></span><spanclass="pre">[1,</span><spanclass="pre">2,</span><spanclass="pre">3,</span><spanclass="pre">4,</span><spanclass="pre">5,</span><spanclass="pre">6]</span></code>.</p></li>
rounds an input float into the number of provided significant digits. For example <codeclass="docutils literal notranslate"><spanclass="pre">$round(3.54343,</span><spanclass="pre">3)</span><spanclass="pre">-></span><spanclass="pre">3.543</span></code>.</p></li>
this works like the Python <codeclass="docutils literal notranslate"><spanclass="pre">random()</span></code> function, but will randomize to an integer value if both start/end are
integers. Without argument, will return a float between 0 and 1.</p></li>
works like the <codeclass="docutils literal notranslate"><spanclass="pre">randint()</span></code> python function and always returns an integer.</p></li>
the input will automatically be parsed the same way as <codeclass="docutils literal notranslate"><spanclass="pre">$eval</span></code> and is expected to be an iterable. A random
this will pad content. <codeclass="docutils literal notranslate"><spanclass="pre">$pad("Hello",</span><spanclass="pre">30,</span><spanclass="pre">c,</span><spanclass="pre">-)</span></code> will lead to a text centered in a 30-wide block surrounded by <codeclass="docutils literal notranslate"><spanclass="pre">-</span></code>
this will crop a text longer than the width, by default ending it with a <codeclass="docutils literal notranslate"><spanclass="pre">[...]</span></code>-suffix that also fits within
the width. If no width is given, the client width or <codeclass="docutils literal notranslate"><spanclass="pre">settings.DEFAULT_CLIENT_WIDTH</span></code> will be used.</p></li>
justifies the text to a given width, aligning it left/right/center or ‘f’ for full (spread text across width).</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">$ljust</span></code> - shortcut to justify-left. Takes all other kwarg of <codeclass="docutils literal notranslate"><spanclass="pre">$just</span></code>.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">$rjust</span></code> - shortcut to right justify.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">$cjust</span></code> - shortcut to center justify.</p></li>
color text. The color is given with one or two characters without the preceeding <codeclass="docutils literal notranslate"><spanclass="pre">|</span></code>. If no endcolor is
given, the string will go back to neutral, so <codeclass="docutils literal notranslate"><spanclass="pre">$clr(r,</span><spanclass="pre">Hello)</span></code> is equivalent to <codeclass="docutils literal notranslate"><spanclass="pre">|rHello|n</span></code>.</p></li>
<h3><codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.funcparser.SEARCHING_CALLABLES</span></code><aclass="headerlink"href="#evennia-utils-funcparser-searching-callables"title="Permalink to this headline">¶</a></h3>
<p>These are callables that requires access-checks in order to search for objects. So they require some
extra reserved kwargs to be passed when running the parser:</p>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">caller</span></code> is required, it’s the the object to do the access-check for. The <codeclass="docutils literal notranslate"><spanclass="pre">access</span></code> kwarg is the
this will look up and try to match an object by key or alias. Use the <codeclass="docutils literal notranslate"><spanclass="pre">type</span></code> kwarg to
search for <codeclass="docutils literal notranslate"><spanclass="pre">account</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">script</span></code> instead. By default this will return nothing if there are more than one
match; if <codeclass="docutils literal notranslate"><spanclass="pre">return_list</span></code> is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> a list of 0, 1 or more matches will be returned instead.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">$objlist(query)</span></code> - legacy alias for <codeclass="docutils literal notranslate"><spanclass="pre">$search</span></code>, always returning a list.</p></li>
<h3><codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.funcparser.ACTOR_STANCE_CALLABLES</span></code><aclass="headerlink"href="#evennia-utils-funcparser-actor-stance-callables"title="Permalink to this headline">¶</a></h3>
<p>These are used to implement actor-stance emoting. They are used by the
<p>Here the <codeclass="docutils literal notranslate"><spanclass="pre">caller</span></code> is the one sending the message and <codeclass="docutils literal notranslate"><spanclass="pre">receiver</span></code> the one to see it. The <codeclass="docutils literal notranslate"><spanclass="pre">mapping</span></code> contains
references to other objects accessible via these callables.</p>
if no <codeclass="docutils literal notranslate"><spanclass="pre">key</span></code> is given, this represents the <codeclass="docutils literal notranslate"><spanclass="pre">caller</span></code>, otherwise an object from <codeclass="docutils literal notranslate"><spanclass="pre">mapping</span></code>
will be used. As this message is sent to different recipients, the <codeclass="docutils literal notranslate"><spanclass="pre">receiver</span></code> will change and this will
be replaced either with the string <codeclass="docutils literal notranslate"><spanclass="pre">you</span></code> (if you and the receiver is the same entity) or with the
result of <codeclass="docutils literal notranslate"><spanclass="pre">you_obj.get_display_name(looker=receiver)</span></code>. This allows for a single string to echo differently
depending on who sees it, and also to reference other people in the same way.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">$You([key])</span></code> - same as <codeclass="docutils literal notranslate"><spanclass="pre">$you</span></code> but always capitalized.</p></li>
sees the string. For example <codeclass="docutils literal notranslate"><spanclass="pre">"$You()</span><spanclass="pre">$conj(smiles)".</span></code> will show as “You smile.” and “Tom smiles.” depending
on who sees it. This makes use of the tools in <aclass="reference internal"href="../api/evennia.utils.verb_conjugation.html#evennia-utils-verb-conjugation"><spanclass="std std-ref">evennia.utils.verb_conjugation</span></a>
<p>Above we define two callables <codeclass="docutils literal notranslate"><spanclass="pre">_dashline</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">_uptime</span></code> and map them to names <codeclass="docutils literal notranslate"><spanclass="pre">"dashline"</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">"uptime"</span></code>,
which is what we then can call as <codeclass="docutils literal notranslate"><spanclass="pre">$header</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">$uptime</span></code> in the string. We also have access to
all the defaults (like <codeclass="docutils literal notranslate"><spanclass="pre">$toint()</span></code>).</p>
<p>The parsed result of the above would be something like this:</p>