From: Christian Herdtweck Date: Thu, 31 Jan 2019 13:10:20 +0000 (+0100) Subject: Convert cnfvar names to lowercase on json import X-Git-Tag: v1.4~12^2~7 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=b05d27ab0d02f2d57016f0e1a07155c9246352b8;p=pyi2ncommon Convert cnfvar names to lowercase on json import read_cnf already converts all variable names to lower case. Do the same for read_cnf_json to get consistent behaviour. SimpleCnf already relies on this. --- diff --git a/src/cnfvar.py b/src/cnfvar.py index 483a96f..ffd967d 100644 --- a/src/cnfvar.py +++ b/src/cnfvar.py @@ -267,6 +267,27 @@ def count_vars (root): # the easy part: JSON reader for get_cnf -j +def make_varname_lowercase(cnfvar): + """ + Custom hook for json decoder: convert variable name to lowercase. + + Since variable names are case insensitive, :py:func:`read_cnf` converts + them all to lower case. Downstream users of :py:func:`read_cnf_json` (e.g. + :py:class:`simple_cnf.SimpleCnf`) rely on lowercase variable names. + + :param dict cnfvar: JSON "object" converted into a dict + :returns: same as input but if field `varname` is present, its value is + converted to lower case + :rtype: dict with str keys + """ + try: + cnfvar['varname'] = cnfvar['varname'].lower() + except KeyError: # there is no "varname" field + pass + except AttributeError: # cnfvar['varname'] is not a string + pass + return cnfvar + def read_cnf_json(cnfdata): """ @@ -280,7 +301,7 @@ def read_cnf_json(cnfdata): of Python 3 so we handle the decoding ourselves. """ cnfdata = cnfdata.decode('iso-8859-1') - cnf_json = json.loads(cnfdata) + cnf_json = json.loads(cnfdata, object_hook=make_varname_lowercase) if is_cnf(cnf_json) is False: raise TypeError("Invalid CNF_VAR.") return cnf_json