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.
This commit is contained in:
Eduard Braun 2016-02-19 04:03:59 +01:00
parent 1a9d6119e0
commit e701acdc25

View file

@ -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()