From 579e82e2af7367a1209e32a27136ca1b7b181221 Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Sat, 24 Dec 2011 00:22:45 -0500 Subject: [PATCH] New settings definitions and validators. Will be used by a new settings.py parser. In progress --- overviewer_core/settingsDefinition.py | 30 +++++++++++++ overviewer_core/settingsValidators.py | 61 +++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 overviewer_core/settingsDefinition.py create mode 100644 overviewer_core/settingsValidators.py diff --git a/overviewer_core/settingsDefinition.py b/overviewer_core/settingsDefinition.py new file mode 100644 index 0000000..3b03eb7 --- /dev/null +++ b/overviewer_core/settingsDefinition.py @@ -0,0 +1,30 @@ +# This file defines all of the things than can +# appear in a settings.py file, as well as the +# function that can validate them + +# the validator should raise an exception if there +# is a problem parsing or validating the value. +# it should return the value to use (which gives +# the validator an oppertunity to cleanup/normalize/ +# whaterver) + +# if a setting is not required, the validator is +# expected to return the default option + +from settingsValidators import * + + +render = { + "path": dict(required=True, validator=validatePath), + "rendermodes": dict(required=False, validator=validateRenderMode), + "north-direction": dict(required=False, validator=validateNorthDirection), + "render-range": dict(required=False, validator=validateRenderRange), + "force-render": dict(required=False, validator=bool), + "stochastic-render": dict(required=False, validator=validateStochastic), + "imgformat": dict(required=False, validator=validateImgFormat), + "imgquality": dict(required=False, validator=validateImgQuality), + "bg-color": dict(required=False, validator=validateBGColor), + "optimize-img": dict(required=False, validator=validateOptImg), + "no-markers": dict(required=False, validator=bool), + } + diff --git a/overviewer_core/settingsValidators.py b/overviewer_core/settingsValidators.py new file mode 100644 index 0000000..5af3e1f --- /dev/null +++ b/overviewer_core/settingsValidators.py @@ -0,0 +1,61 @@ +# see settingsDefinition.py +import os +import os.path + +class ValidationException(Exception): + pass + +def validatePath(path): + if not os.path.exists(path): + raise ValidationException("%r does not exist" % path) + if not os.path.isdir(path): + raise ValidationException("%r is not a directory" % path) + return os.abspath(path) + +def validateRenderMode(mode): + # TODO get list of valid rendermodes + raise NotImplementedError("validateRenderMode") + +def validateNorthDirection(direction): + # normalize to integers + intdir = 0 #default + if type(direction) == int: + intdir = direction + else: + if direction == "upper-left": intdir = 0 + if direction == "upper-right": intdir = 1 + if direction == "lower-right": intdir = 2 + if direction == "lower-left": intdir = 3 + if intdir < o or intdir > 3: + raise ValidationException("%r is not a valid north direction" % direction) + return intdir + +def validateRenderRange(r): + raise NotImplementedError("render range") + +def validateStochastic(s): + val = float(s) + if val < 0 or val > 1: + raise ValidationException("%r is not a valid stochastic value. Should be between 0.0 and 1.0" % s) + return val + +def validateImgFormat(fmt): + if fmt not in ("png", "jpg", "jpeg"): + raise ValidationException("%r is not a valid image format" % fmt) + if fmt == "jpeg": fmt = "jpg" + return fmt + +def validateImgQuality(qual): + intqual = int(qual) + if (intqual < 0 or intqual > 100): + raise ValidationException("%r is not a valid image quality" % intqual) + return intqual + +def validateBGColor(color): + raise NotImplementedError("bg color") + +def validateOptImg(opt): + return bool(opt) + + +