From c904e59d4b936357c9a85dabe66a5cdb9a6491a5 Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Fri, 10 Sep 2010 23:55:03 +0200 Subject: [PATCH] Add ConfigTree.fix, improve parse_file and use it in Eval.init --- t/moe/config.py | 14 +++++++++----- t/moe/eval.py | 3 +-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/t/moe/config.py b/t/moe/config.py index bcef93b..d17d1c8 100644 --- a/t/moe/config.py +++ b/t/moe/config.py @@ -94,6 +94,10 @@ class ConfigTree(object): self.variables[k].dump(prefix) for k in sorted(self.variables.keys()) ]) + def fix(self, key): + "Fix variable value. Fixing undefined variable raises `UndefinedError`." + self.lookup(key, create=True).fix() + def parse(self, s, source=None, level=0): """Parse `s` (stream/string) into the tree, see `moe.confparser.ConfigParser` for details.""" import moe.confparser @@ -103,10 +107,10 @@ class ConfigTree(object): def parse_file(self, filename, desc=None, level=0): """Parse an utf-8 file into the tree, see `moe.confparser.ConfigParser` for details. Names the source "`filename` <`desc`>". """ - f = open(filename, 'rt') - if desc: - filename += " <" + desc + ">" - self.parse(f, source=filename, level=level) + with open(filename, 'rt') as f: + if desc: + filename += " <" + desc + ">" + self.parse(f, source=filename, level=level) class ConfigElem(object): @@ -274,7 +278,7 @@ class ConfigVar(ConfigElem): self.fixed = True def unfix(self): - "Set the variable to be modifiable again." + "Make the variable modifiable again." self.fixed = False def value(self, depth=0): diff --git a/t/moe/eval.py b/t/moe/eval.py index 9a964ff..1e54b4e 100644 --- a/t/moe/eval.py +++ b/t/moe/eval.py @@ -43,8 +43,7 @@ class Eval: # load config file self.config.fix('CONFIG') - with open(self['CONFIG'], 'r') as f: - self.config.parse(f, source=self['CONFIG'], level=30) + self.config.parse_file(self['CONFIG'], level=30) # fix variables self.config.fix(['LOG', 'USER_LOG', 'VERBOSE', 'HOME', 'DEBUG_LEVEL', 'TDIR']) # start logging -- 2.39.5