From e701acdc25edf923a1169e877cb2255b398bc57d Mon Sep 17 00:00:00 2001 From: Eduard Braun Date: Fri, 19 Feb 2016 04:03:59 +0100 Subject: [PATCH] Add a mechanism to sanitize options. This simplifies usage of the Scour module while avoiding any compatibility issues that might be caused by adding/removing/renaming options. --- scour/scour.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index ec5e5f8..8ab6c81 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -2866,8 +2866,9 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False): # input is a string representation of the input XML # returns a string representation of the output XML def scourString(in_string, options=None): - if options is None: - options = _options_parser.get_default_values() + # sanitize options (take missing attributes from defaults, discard unknown attributes) + options = sanitizeOptions(options) + getcontext().prec = options.digits global numAttrsRemoved global numStylePropsFixed @@ -3289,6 +3290,18 @@ def generateDefaultOptions(): return Struct(**d) + +# sanitizes options by updating attributes in a set of defaults options while discarding unknown attributes +def sanitizeOptions(options): + optionsDict = dict((key, getattr(options, key)) for key in dir(options) if not key.startswith('__')) + + sanitizedOptions = _options_parser.get_default_values() + sanitizedOptions._update_careful(optionsDict) + + return sanitizedOptions + + + def start(options, input, output): start = walltime()