def _read_json_policy_reform_text(text_string, arrays_not_lists, growdiff_baseline_dict, growdiff_response_dict): """ Strip //-comments from text_string and return 1 dict based on the JSON. Specified text is JSON with at least 1 high-level string:object pair: a "policy": {...} pair. Other high-level pairs will be ignored by this method, except that a "consumption", "behavior", "growdiff_baseline" or "growdiff_response" key will raise a ValueError. The {...} object may be empty (that is, be {}), or may contain one or more pairs with parameter string primary keys and string years as secondary keys. See tests/test_calculate.py for an extended example of a commented JSON policy reform text that can be read by this method. Returned dictionary prdict has integer years as primary keys and string parameters as secondary keys. This returned dictionary is suitable as the argument to the Policy implement_reform(prdict) method ONLY if the function argument arrays_not_lists is True. """ # strip out //-comments without changing line numbers json_str = re.sub('//.*', ' ', text_string) # convert JSON text into a Python dictionary try: raw_dict = json.loads(json_str) except ValueError as valerr: msg = 'Policy reform text below contains invalid JSON:\n' msg += str(valerr) + '\n' msg += 'Above location of the first error may be approximate.\n' msg += 'The invalid JSON reform text is between the lines:\n' bline = 'XX----.----1----.----2----.----3----.----4' bline += '----.----5----.----6----.----7' msg += bline + '\n' linenum = 0 for line in json_str.split('\n'): linenum += 1 msg += '{:02d}{}'.format(linenum, line) + '\n' msg += bline + '\n' raise ValueError(msg) # check key contents of dictionary actual_keys = raw_dict.keys() for rkey in Calculator.REQUIRED_REFORM_KEYS: if rkey not in actual_keys: msg = 'key "{}" is not in policy reform file' raise ValueError(msg.format(rkey)) for rkey in actual_keys: if rkey in Calculator.REQUIRED_ASSUMP_KEYS: msg = 'key "{}" should be in economic assumption file' raise ValueError(msg.format(rkey)) # convert raw_dict['policy'] dictionary into prdict tdict = Policy.translate_json_reform_suffixes(raw_dict['policy'], growdiff_baseline_dict, growdiff_response_dict) prdict = Calculator._convert_parameter_dict(tdict, arrays_not_lists) return prdict
def _read_json_policy_reform_text(text_string): """ Strip //-comments from text_string and return 1 dict based on the JSON. Specified text is JSON with at least 1 high-level key:object pair: a "policy": {...} pair. Other keys will raise a ValueError. The {...} object may be empty (that is, be {}), or may contain one or more pairs with parameter string primary keys and string years as secondary keys. See tests/test_calculator.py for an extended example of a commented JSON policy reform text that can be read by this method. Returned dictionary prdict has integer years as primary keys and string parameters as secondary keys. This returned dictionary is suitable as the argument to the Policy implement_reform(prdict) method. """ # pylint: disable=too-many-locals # strip out //-comments without changing line numbers json_str = re.sub('//.*', ' ', text_string) # convert JSON text into a Python dictionary try: raw_dict = json.loads(json_str) except ValueError as valerr: msg = 'Policy reform text below contains invalid JSON:\n' msg += str(valerr) + '\n' msg += 'Above location of the first error may be approximate.\n' msg += 'The invalid JSON reform text is between the lines:\n' bline = 'XX----.----1----.----2----.----3----.----4' bline += '----.----5----.----6----.----7' msg += bline + '\n' linenum = 0 for line in json_str.split('\n'): linenum += 1 msg += '{:02d}{}'.format(linenum, line) + '\n' msg += bline + '\n' raise ValueError(msg) # check key contents of dictionary actual_keys = set(raw_dict.keys()) missing_keys = Calculator.REQUIRED_REFORM_KEYS - actual_keys if missing_keys: msg = 'required key(s) "{}" missing from policy reform file' raise ValueError(msg.format(missing_keys)) illegal_keys = actual_keys - Calculator.REQUIRED_REFORM_KEYS if illegal_keys: msg = 'illegal key(s) "{}" in policy reform file' raise ValueError(msg.format(illegal_keys)) # convert raw_dict['policy'] dictionary into prdict raw_dict_policy = raw_dict['policy'] tdict = Policy.translate_json_reform_suffixes(raw_dict['policy']) prdict = Calculator._convert_parameter_dict(tdict) return prdict