Merge pull request #44 from Ede123/default_options

Mechanism for sanitizing options
This commit is contained in:
Tobias Oberstein 2016-02-19 14:07:29 +01:00
commit 7c3331d6f6
2 changed files with 39 additions and 25 deletions

View file

@ -2866,8 +2866,9 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
# input is a string representation of the input XML # input is a string representation of the input XML
# returns a string representation of the output XML # returns a string representation of the output XML
def scourString(in_string, options=None): def scourString(in_string, options=None):
if options is None: # sanitize options (take missing attributes from defaults, discard unknown attributes)
options = _options_parser.get_default_values() options = sanitizeOptions(options)
getcontext().prec = options.digits getcontext().prec = options.digits
global numAttrsRemoved global numAttrsRemoved
global numStylePropsFixed global numStylePropsFixed
@ -3289,6 +3290,18 @@ def generateDefaultOptions():
return Struct(**d) 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): def start(options, input, output):
start = walltime() start = walltime()

View file

@ -45,30 +45,31 @@ def walkTree(elem, func):
if walkTree(child, func) == False: return False if walkTree(child, func) == False: return False
return True return True
class ScourOptions: class ScourOptions:
simple_colors = True pass
style_to_xml = True
group_collapse = True
group_create = False class EmptyOptions(unittest.TestCase):
strip_ids = False def runTest(self):
strip_comments = False options = ScourOptions
shorten_ids = False try:
shorten_ids_prefix = "" scour.scourXmlFile('unittests/ids-to-strip.svg', options)
embed_rasters = True fail = False
keep_defs = False except:
keep_editor_data = False fail = True
remove_metadata = False self.assertEqual(fail, False, 'Exception when calling Scour with empty options object')
renderer_workaround = True
strip_xml_prolog = False class InvalidOptions(unittest.TestCase):
enable_viewboxing = False def runTest(self):
digits = 5 options = ScourOptions
indent_type = "space" options.invalidOption = "invalid value"
indent_depth = 1 try:
newlines = True scour.scourXmlFile('unittests/ids-to-strip.svg', options)
strip_xml_space_attribute = False fail = False
protect_ids_noninkscape = False except:
protect_ids_list = None fail = True
protect_ids_prefix = None self.assertEqual(fail, False, 'Exception when calling Scour with invalid options')
class NoInkscapeElements(unittest.TestCase): class NoInkscapeElements(unittest.TestCase):
def runTest(self): def runTest(self):