]> mj.ucw.cz Git - moe.git/commitdiff
Added function config_escape
authorTomas Gavenciak <gavento@ucw.cz>
Tue, 23 Nov 2010 19:12:06 +0000 (20:12 +0100)
committerTomas Gavenciak <gavento@ucw.cz>
Tue, 23 Nov 2010 19:12:06 +0000 (20:12 +0100)
For safe constructon configuration commands as strings, tested, documented.

t/doc/config.rst
t/moe/config_parser.py
t/moe/config_test.py

index 3216a468e8b31af6220ddd3f2898c14727646aba..87e825ef8fcc7e46b369ef4d4554dc79da8021f8 100644 (file)
@@ -7,6 +7,7 @@ Moe configuration
 =================
 
 .. contents:: 
+  :local: 
 
 -------------------
 Configuration logic
@@ -184,6 +185,8 @@ nested inside another. In this case the parent condition is checked first and th
 Internals
 ^^^^^^^^^
 
+These classes should not be used directly.
+
 .. autoclass:: ConfigElem
   :members:
 
@@ -192,7 +195,11 @@ Internals
 
 
 ----------------------------------------
-Config parser module `moe.config_parser`
+Configuration parser
 ----------------------------------------
 
 .. automodule:: moe.config_parser
+
+.. autoclass:: ConfigSyntaxError
+
+.. autofunction:: config_escape
index b3131cccf78e7e8ed616b0d03ae2faa4aa0dbe4f..838d76b661edfc6b04120632d675f8ee96e5dcc6 100644 (file)
@@ -47,6 +47,16 @@ import traceback
 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):
index f1c836d33c204021ce1dfd32dff3ff38f80e9979..d6eb51ba47ab465079bf46b435ae9d28abb833da 100644 (file)
@@ -106,6 +106,10 @@ class TestParser(TestConfig):
     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):