]> mj.ucw.cz Git - moe.git/commitdiff
Added ConfigTree.remove, adapted parse and parse_file
authorTomas Gavenciak <gavento@ucw.cz>
Wed, 22 Sep 2010 21:26:32 +0000 (23:26 +0200)
committerTomas Gavenciak <gavento@ucw.cz>
Wed, 22 Sep 2010 21:26:32 +0000 (23:26 +0200)
parse(_file) returns list of operations.
remove removes these from the config

Includes a testcase

t/moe/config.py
t/moe/config_test.py

index b2f788ef81e1bfea9de21b02aba8268f287fdad3..04e363f1887ef53446392b202a34df97faa00630 100644 (file)
@@ -101,11 +101,21 @@ class ConfigTree(object):
     for key in keys:
       self.lookup(key, create=True).fix()
 
+  def remove(self, parsed):
+    """Given a list [(varname, `Operation`)] as returned by `parse` or `parse_file`, 
+    removes the operations from the respective variables config tree.
+    Variables/operations not present int the tree raise ValueError.
+    """
+    for vname, o in parsed:
+      v = self.lookup(vname, create = True)
+      v.remove_operation(o)
+
   def parse(self, s, source=None, level=0):
-    """Parse `s` (stream/string) into the tree, see `moe.confparser.ConfigParser` for details."""
+    """Parse `s` (stream/string) into the tree, see `moe.confparser.ConfigParser` for details.
+    Returns list of parset operations: [(varname, `Operation`)]"""
     import moe.config_parser
     p = moe.config_parser.ConfigParser(s, self, source=source, level=level)
-    p.parse()
+    return p.parse()
 
   def parse_file(self, filename, desc=None, level=0):
     """Parse an utf-8 file into the tree, see `moe.confparser.ConfigParser` for details. 
@@ -113,7 +123,7 @@ class ConfigTree(object):
     with open(filename, 'rt') as f:
       if desc: 
        filename += " <" + desc + ">" 
-      self.parse(f, source=filename, level=level)
+      return self.parse(f, source=filename, level=level)
 
 
 class ConfigElem(object):
index c19645a375e2d106eafe36d787a2f17b4b69c815..2a0325b8b57e923b9ce76ccad47683e72eb1451e 100644 (file)
@@ -82,6 +82,23 @@ class TestParser(TestConfig):
     s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' ornot ''!='') {}")
     s.assertRaises(ConfigSyntaxError, s.parse, "if 'a'<>'b' {}")
 
+  def test_parse_remove(s):
+    s.parse('a="000"')
+    d1 = s.parse("a='A'; b='B'; c='C'", level=10)
+    d2 = s.parse('c="{a}{a}"; b="XX" ', level=20)
+    d3 = s.parse('b+=c ', level=30)
+    assert s.val('b') == "XXAA"
+    s.t.remove(d2)
+    assert s.val('b') == "BC"
+    s.assertRaises(ValueError, s.t.remove, [('d', d1[1][0])])
+    s.assertRaises(ValueError, s.t.remove, [('b', d1[1][0])])
+    # partially remove d1
+    s.t.remove([('c', d1[2][1])])
+    # try to remove rest - 'a' and 'b' should get removed
+    s.assertRaises(ValueError, s.t.remove, d1)
+    assert s.val('a') == "000"
+    s.t.remove(d3)
+
 
 class TestConfigEval(TestConfig):