def test_convert_json_to_dict(self): # Should work with string path to a file dirname, filename = os.path.split(os.path.abspath(__file__)) examples = '%s/../../examples' % dirname simple = '%s/simple.json' % examples ret = utils.convert_json_to_dict(simple, {}) self.assertEquals(type(ret), dict) # Should work with file instance also dirname, filename = os.path.split(os.path.abspath(__file__)) examples = '%s/../../examples' % dirname simple = '%s/simple.json' % examples instance = open(simple) ret = utils.convert_json_to_dict(instance, {}) self.assertEquals(type(ret), dict)
def _get_config_from_json(self, json_file): """Convert a json file into a dict() with inserted ENV vars. Run the JSON dictionary through our environment parser and return back a dictionary with all of the %XX% keys swapped out with environment variables. Args: json_file: A path string to a file, or an open() file stream. Returns: Dictionary adhering to our schema. Raises: UnrecoverableActorFailure - if parsing json or inserting env vars fails. """ self.log.debug('Parsing %s' % json_file) try: config = utils.convert_json_to_dict( json_file=json_file, tokens=self.option('tokens')) except Exception as e: raise exceptions.UnrecoverableActorFailure(e) return config
def test_convert_json_to_dict_error(self): instance = StringIO.StringIO() # Empty buffer will fail demjson. with self.assertRaises(ValueError): utils.convert_json_to_dict(instance, {})