From: Tomas Gavenciak Date: Sat, 29 May 2010 16:59:57 +0000 (-0400) Subject: New ConfigError subclasses, move re_VARNAME X-Git-Tag: python-dummy-working~50 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=0208316ef336f906abff8f2516f08b40e8fdf348;p=moe.git New ConfigError subclasses, move re_VARNAME Moved re_VARNAME to conf.py Minor fix VARNAME checking in ConfigTree.lookup() --- diff --git a/t/moe/conf.py b/t/moe/conf.py index 3f145c8..1564068 100644 --- a/t/moe/conf.py +++ b/t/moe/conf.py @@ -26,7 +26,6 @@ TODO: Test conditions and unicode import types, itertools, re, bisect import logging as log -from confparser import re_VARNAME "Allowed depth of recursion - includes ALL recursive calls, so should quite high." c_maxdepth = 256 @@ -34,11 +33,14 @@ c_maxdepth = 256 "Maximum attained depth of recursion - for debug/testing" debug_maxdepth = 0 +"Variable name regexp, dots (separators) must be separated from edges and each other." +re_VARNAME = re.compile(r'\A([A-Za-z0-9_-]+\.)*[A-Za-z0-9_-]+\Z') + def check_depth(depth): "Helper to check for recursion depth." global debug_maxdepth if depth > c_maxdepth: - raise ConfigError('Too deep recursion in config evaluation (cyclic substitution?)') + raise CyclicConfigError('Too deep recursion in config evaluation (cyclic substitution?)') if depth > debug_maxdepth: debug_maxdepth = depth @@ -46,6 +48,15 @@ def check_depth(depth): class ConfigError(Exception): pass +class UndefinedError(ConfigError): + pass + +class VariableNameError(ConfigError): + pass + +class CyclicConfigError(ConfigError): + pass + class ConfigTree(object): """ @@ -60,11 +71,11 @@ class ConfigTree(object): Lookup and return a variable. If not found and `create` set, check the name and transparently create a new one. """ - if not key in self.variables: - if not create: - raise ConfigError('Config variable %r undefined.', key) + if key not in self.variables: if not re_VARNAME.match(key): - raise ConfigError('Invalid variable identifier %r in config', key) + raise VariableNameError('Invalid variable identifier %r in config', key) + if not create: + raise UndefinedError('Config variable %r undefined.', key) self.variables[key] = ConfigVar(key) return self.variables[key] def dump(self, prefix=''): @@ -106,7 +117,7 @@ class ConfigElem(object): self.cached_val = self.evaluate(depth=depth+1) self.cached = True if self.cached_val == None: - raise ConfigError("Unable to evaluate %r."%(self.name,)) + raise UndefinedError("Unable to evaluate %r."%(self.name,)) return self.cached_val def __str__(self): return self.name diff --git a/t/moe/confparser.py b/t/moe/confparser.py index e6ff566..e2f81b8 100644 --- a/t/moe/confparser.py +++ b/t/moe/confparser.py @@ -46,8 +46,7 @@ import re, types, itertools, logging as log import traceback import conf -class ConfigSyntaxError(Exception): - # TODO: choose a better superclass +class ConfigSyntaxError(conf.ConfigError): def __init__(self, msg, fname='', line=None, column=None): self.msg = msg self.fname = fname @@ -56,9 +55,6 @@ class ConfigSyntaxError(Exception): def __str__(self): return('ConfigSyntaxError %s:%d:%d: %s'%(self.fname, self.line, self.column, self.msg)) -"Variable name regexp, dots (separators) must be separated from edges and each other." -re_VARNAME = re.compile(r'\A([A-Za-z0-9_-]+\.)*[A-Za-z0-9_-]+\Z') - class ConfigParser(object): c_varname_sep = u'.' c_comment = u'#' @@ -114,7 +110,7 @@ class ConfigParser(object): def next(self, l = 1): "Eat and return next `l` unicode characters. Raise exception on EOF." if not self.preread(l): - raise ConfigSyntaxError("Unexpected end of file") + self.syntax_error("Unexpected end of file") s = self.buf[self.bufpos:self.bufpos+l] self.bufpos += l rnl = s.rfind('\n') @@ -260,7 +256,7 @@ class ConfigParser(object): while self.peek().isalnum() or self.peek() in u'-_.': vnl.append(self.next()) vn = u''.join(vnl) - if not re_VARNAME.match(vn): + if not conf.re_VARNAME.match(vn): self.syntax_error('Invalid variable name %r', vn) return vn def p_EXPRESSION(self):