For safe constructon configuration commands as strings, tested, documented.
=================
.. contents::
+ :local:
-------------------
Configuration logic
Internals
^^^^^^^^^
+These classes should not be used directly.
+
.. autoclass:: ConfigElem
:members:
----------------------------------------
-Config parser module `moe.config_parser`
+Configuration parser
----------------------------------------
.. automodule:: moe.config_parser
+
+.. autoclass:: ConfigSyntaxError
+
+.. autofunction:: config_escape
import moe.config as cf
+def config_escape(s):
+ """
+ Escape any ``{``, ``}``, ``"`` and ``\\`` in the given string, making it safe for parsing.
+ """
+ s = s.replace('\\', '\\\\')
+ s = s.replace('{', '\\{')
+ s = s.replace('}', '\\}')
+ s = s.replace('"', '\\"')
+ return s
+
class ConfigSyntaxError(cf.ConfigError):
def __init__(self, msg, source='<unknown>', line=None, column=None):
for v in 'abcd':
assert len(s.var(v).operations) == 0
+ def test_escape(s):
+ for tst in ['{B}"#\n\\"', '', '\\\n\\\\"\\{\\}', '"#"', s.s1, s.s2]:
+ s.parse('A="%s"; A+="0"'%config_escape(tst))
+ assert s.val('A') == tst+'0'
class TestConfigEval(TestConfig):