def convert(type, input, output, **kwargs): """ Convert data from one format to another. :param type: The type specifier string of the input data. :param input: A binding dict of the form ``{"format": format, "data", data}``, where ``format`` is the format specifier string, and ``data`` is the raw data to convert. The dict may also be of the form ``{"format": format, "uri", uri}``, where ``uri`` is the location of the data (see :py:mod:`romanesco.uri` for URI formats). :param output: A binding of the form ``{"format": format}``, where ``format`` is the format specifier string to convert the data to. The binding may also be in the form ``{"format": format, "uri", uri}``, where ``uri`` specifies where to place the converted data. :returns: The output binding dict with an additional field ``"data"`` containing the converted data. If ``"uri"`` is present in the output binding, instead saves the data to the specified URI and returns the output binding unchanged. """ if "data" not in input: input["data"] = romanesco.io.fetch(input, **kwargs) if input["format"] == output["format"]: data = input["data"] else: data_descriptor = input for c in converter_path(Validator(type, input['format']), Validator(type, output['format'])): result = romanesco.run(c, {"input": data_descriptor}, auto_convert=False, **kwargs) data_descriptor = result["output"] data = data_descriptor["data"] if "mode" in output: romanesco.io.push(data, output) else: output["data"] = data return output
def test_converter_path(self): # There is no path from validators that don't exist with self.assertRaises(NetworkXNoPath): converter_path(Validator('foo', 'bar'), Validator('foo', 'baz')) # There is no path for types which lie in different components with self.assertRaises(NetworkXNoPath): converter_path(self.stringTextValidator, Validator('graph', 'networkx')) self.assertEquals(converter_path(self.stringTextValidator, self.stringTextValidator), []) # There is a direct path from converting between these types self.assertEquals(len(converter_path(self.stringTextValidator, Validator('string', 'json'))), 1)