From: Martin Mares Date: Sun, 9 Aug 2009 23:17:11 +0000 (+0200) Subject: Cleaned up exception handling and logging X-Git-Tag: python-dummy-working~83 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=f9b0b2c89eca1c93e70e80c8c7f7775a65fdccbb;p=eval.git Cleaned up exception handling and logging --- diff --git a/t/moe/__init__.py b/t/moe/__init__.py index 02cc0ce..19957e2 100644 --- a/t/moe/__init__.py +++ b/t/moe/__init__.py @@ -1 +1,7 @@ # No initialization needed + +class MoeErr(Exception): + pass + +class SolutionErr(Exception): + pass diff --git a/t/moe/batch.py b/t/moe/batch.py index 28b585c..edb9eef 100644 --- a/t/moe/batch.py +++ b/t/moe/batch.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import os.path +import moe import moe.eval import moe.util @@ -12,9 +13,10 @@ def try_ext(e, ext): for e in e.cfgs["EXTENSIONS"].split(): if e == ext: return - raise moe.eval.MoeEvalErr, "Unknown extension: " + ext + raise moe.MoeErr, "Unknown extension: " + ext def locate(e, filename=None): + e.log.progress("Locating source... ") task = e.cfgs["TASK"] if filename is None: dir = "" @@ -27,7 +29,7 @@ def locate(e, filename=None): base, ext = os.path.splitext(file) if ext != "": if not os.path.exists(os.path.join(dir, file)): - raise moe.eval.MoeEvalErr, "No solution of %s called %s found" % (task,file) + raise moe.SolutionErr, "No solution of %s called %s found" % (task,file) ext = ext[1:] try_ext(e, ext) else: @@ -36,9 +38,9 @@ def locate(e, filename=None): if os.path.exists(os.path.join(dir, base + "." + ext)): found.append(ext) if len(found) == 0: - raise moe.eval.MoeEvalErr, "No solution of %s found" % task + raise moe.SolutionErr, "No solution of %s found" % task if len(found) > 1: - raise moe.eval.MoeEvalErr, "Multiple solutions of %s found" % task + raise moe.SolutionErr, "Multiple solutions of %s found" % task ext = found[0] file = base + "." + ext @@ -56,7 +58,8 @@ def locate(e, filename=None): e.builtins.set("EXT", norm_ext) e.cfgs.apply_overrides("EXT_" + norm_ext) - e.meta["source"] = copy + e.meta["source"] = file + e.log.progress(file + "\n") def compile(e): pass diff --git a/t/moe/config.py b/t/moe/config.py index c68a899..71cfbae 100644 --- a/t/moe/config.py +++ b/t/moe/config.py @@ -2,14 +2,15 @@ import re import sys +import moe key_pattern = re.compile("^[A-Za-z0-9_-]+$") ref_pattern = re.compile("^[A-Za-z0-9_-]+") -class MoeConfigInvalid(Exception): +class MoeConfigInvalid(moe.MoeErr): pass -class MoeConfigEvalErr(Exception): +class MoeConfigEvalErr(moe.MoeErr): pass class MoeConfig: diff --git a/t/moe/eval.py b/t/moe/eval.py index 2d5c976..3e313f3 100644 --- a/t/moe/eval.py +++ b/t/moe/eval.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import moe import moe.config import moe.log import moe.meta @@ -9,9 +10,6 @@ import moe.util import os.path import shutil -class MoeEvalErr(Exception): - pass - class Eval: def __init__(self): @@ -24,10 +22,12 @@ class Eval: pass def init(self, overrides=None): + self.log.progress("Initializing... ") self.init_global(overrides) self.init_test() self.init_logs() self.init_task() + self.log.progress("OK\n") def init_global(self, overrides): main_cfg = moe.config.MoeConfig(name = os.path.join(self.cfgs['HOME'], "config"), type="main") @@ -42,7 +42,7 @@ class Eval: try: moe.util.mkdir_tree(test) except OSError, e: - raise MoeEvalErr, "Cannot create %s: %s" % (test, e.strerror) + raise moe.MoeErr, "Cannot create %s: %s" % (test, e.strerror) def init_logs(self): self.log = moe.log.MoeLog() @@ -50,13 +50,14 @@ class Eval: self.log.verbosity = int(self.cfgs["V"]) self.log.log_file = open(os.path.join(self.cfgs["TEST_DIR"], "log"), "w") self.default_log = self.log + moe.log.default = self.log self.log_config(3, "before loading the task") def init_task(self): task = self.cfgs['TASK'] task_dir = self.cfgs['TASK_DIR'] if not os.path.isdir(task_dir): - raise MoeEvalErr, "No such task %s" % task + raise moe.MoeErr, "No such task %s" % task task_cfg = moe.config.MoeConfig(name = os.path.join(task_dir, "config"), type='task') self.cfgs.push(task_cfg) @@ -68,14 +69,15 @@ class Eval: if type == "batch" or type == "interactive": moe.batch.prepare_pipe(self) elif type == "opendata": - raise MoeEvalErr, "Opendata tasks not implemented yet" + raise moe.MoeErr, "Opendata tasks not implemented yet" else: - raise MoeEvalErr, "Unknown task type " + type + raise moe.MoeErr, "Unknown task type " + type def run(self): self.log_config(2, "for the task pipeline") self.main_pipe.configure(self.cfgs["HOOKS"]) - self.main_pipe.dump() + if self.log.verbosity >= 2: + self.main_pipe.dump(self.log.log_file, prefix="\t") self.main_pipe.run(self) def log_config(self, verb, msg): diff --git a/t/moe/log.py b/t/moe/log.py index 0695b79..295fac3 100644 --- a/t/moe/log.py +++ b/t/moe/log.py @@ -18,7 +18,11 @@ class MoeLog: self.log_file.write(msg) def progress(self, msg): - if self.progress: - self.progress.write(msg) + if self.progress_file: + self.progress_file.write(msg) + + def shout(self, msg): + self.say(msg) + self.progress(msg) default = MoeLog() diff --git a/t/moe/pipeline.py b/t/moe/pipeline.py index e7fc9a7..bc407a2 100644 --- a/t/moe/pipeline.py +++ b/t/moe/pipeline.py @@ -3,9 +3,10 @@ import sys import bisect import imp +import moe import moe.log -class MoePipeError(Exception): +class MoePipeError(moe.MoeErr): """Failure of the MoePipeline.""" class MoePipeline: @@ -23,10 +24,10 @@ class MoePipeline: raise MoePipeError, "Pipeline insert cannot alter the past" self.pipe.insert(pos, triple) - def dump(self, file=sys.stdout): + def dump(self, file=sys.stdout, prefix=""): file.write(">>> Pipeline %s\n" % self.name) for pri,name,fun in self.pipe: - file.write("%03d %s\n" % (pri,name)) + file.write("%s%03d %s\n" % (prefix,pri,name)) def run(self, *args): self.index = 0 diff --git a/t/test.py b/t/test.py index e736b32..b50fed1 100755 --- a/t/test.py +++ b/t/test.py @@ -3,27 +3,40 @@ import sys sys.path.append('.') +import moe import moe.meta import moe.config import moe.eval import moe.pipeline import moe.batch -overrides = moe.config.parse_overrides(sys.argv) - e = moe.eval.Eval() -e.builtins.set("HOME", ".") -e.builtins.set("TASK", "sum") -e.builtins.set("CONTESTANT", "mj") -e.init(overrides) -if len(sys.argv) > 1: - file = sys.argv[1] -else: - file = None -moe.batch.locate(e, file) +try: + overrides = moe.config.parse_overrides(sys.argv) + e.builtins.set("HOME", ".") + e.builtins.set("TASK", "sum") + e.builtins.set("CONTESTANT", "mj") + e.log.progress("### Evaluating task %s of contestant %s ###\n\n" % (e.cfgs['TASK'], e.cfgs['CONTESTANT'])) + e.init(overrides) +except moe.MoeErr, err: + e.log.shout("FATAL: %s\n" % err) + sys.exit(1) -e.run() +try: + if len(sys.argv) > 1: + file = sys.argv[1] + else: + file = None + moe.batch.locate(e, file) + e.run() +except moe.MoeErr, err: + e.log.shout("FATAL: %s\n" % err) + sys.exit(1) +except moe.SolutionErr, err: + ## FIXME: In this case, we might write the meta file + e.log.shout("%s\n" % err) + sys.exit(1) print "\nFinal meta file:" e.meta.write()