def _test_outout(self, source_type): """Test all outputs""" self.assertEqual(source_type, self.iohandler.source_type, 'Source type properly set') self.assertEqual(self._value, self.iohandler.data, 'Data obtained') if self.iohandler.source_type == SOURCE_TYPE.STREAM: source = StringIO(text_type(self._value)) self.iohandler.stream = source file_handler = open(self.iohandler.file) self.assertEqual(self._value, file_handler.read(), 'File obtained') file_handler.close() if self.iohandler.source_type == SOURCE_TYPE.STREAM: source = StringIO(text_type(self._value)) self.iohandler.stream = source stream_val = self.iohandler.stream.read() if type(stream_val) == type(b''): self.assertEqual(str.encode(self._value), stream_val, 'Stream obtained') else: self.assertEqual(self._value, stream_val, 'Stream obtained') self.iohandler.stream.close() if self.iohandler.source_type == SOURCE_TYPE.STREAM: source = StringIO(text_type(self._value)) self.iohandler.stream = source self.skipTest('Memory object not implemented') self.assertEqual(data, self.iohandler.memory_object, 'Memory object obtained')
def get_stream(self): """Get source as stream object""" if self.source_type == SOURCE_TYPE.FILE: from io import FileIO return FileIO(self.source, mode='r', closefd=True) elif self.source_type == SOURCE_TYPE.STREAM: return self.source elif self.source_type == SOURCE_TYPE.DATA: return StringIO(text_type(self.source))
def _test_outout(self, source_type, suffix=''): """Test all outputs""" self.assertEqual(source_type, self.iohandler.source_type, 'Source type properly set') self.assertEqual(self._value, self.iohandler.data, 'Data obtained') if self.iohandler.source_type == SOURCE_TYPE.URL: self.assertEqual('http', urlparse(self.iohandler.url).scheme) else: self.assertEqual('file', urlparse(self.iohandler.url).scheme) if self.iohandler.source_type == SOURCE_TYPE.STREAM: source = StringIO(text_type(self._value)) self.iohandler.stream = source file_path = self.iohandler.file self.assertTrue(file_path.endswith(suffix)) file_handler = open(file_path) self.assertEqual(self._value, file_handler.read(), 'File obtained') file_handler.close() if self.iohandler.source_type == SOURCE_TYPE.STREAM: source = StringIO(text_type(self._value)) self.iohandler.stream = source stream_val = self.iohandler.stream.read() self.iohandler.stream.close() if PY2 and isinstance(stream_val, str): self.assertEqual(self._value, stream_val.decode('utf-8'), 'Stream obtained') elif not PY2 and isinstance(stream_val, bytes): self.assertEqual(self._value, stream_val.decode('utf-8'), 'Stream obtained') else: self.assertEqual(self._value, stream_val, 'Stream obtained') if self.iohandler.source_type == SOURCE_TYPE.STREAM: source = StringIO(text_type(self._value)) self.iohandler.stream = source
def get_stream(self): """Get source as stream object""" if self.source_type == SOURCE_TYPE.FILE: if self._stream and not self._stream.closed: self._stream.close() from io import FileIO self._stream = FileIO(self.source, mode='r', closefd=True) return self._stream elif self.source_type == SOURCE_TYPE.STREAM: return self.source elif self.source_type == SOURCE_TYPE.DATA: return StringIO(text_type(self.source))
def test_complex_input(self): the_data = E.TheData("hello world") request_doc = WPS.Execute( OWS.Identifier('foo'), WPS.DataInputs( WPS.Input( OWS.Identifier('name'), WPS.Data(WPS.ComplexData(the_data, mimeType='text/foobar'))))) rv = get_inputs_from_xml(request_doc) self.assertEqual(rv['name'][0]['mimeType'], 'text/foobar') rv_doc = lxml.etree.parse(StringIO(rv['name'][0]['data'])).getroot() self.assertEqual(rv_doc.tag, 'TheData') self.assertEqual(rv_doc.text, 'hello world')
def test_complex_input_stream(self): complex = self.make_complex_input() complex.stream = StringIO("some data") complex2 = inout.inputs.ComplexInput.from_json(complex.json) # the serialized stream becomes a data type # we hard-code it for the testing comparison complex.prop = 'data' # the data is enclosed by a CDATA tag complex._data = u'<![CDATA[{}]]>'.format(complex.data) # it's expected that the file path changed complex._file = complex2.file self.assert_complex_equals(complex, complex2)
def get_input_from_xml(doc): the_input = MultiDict() for input_el in xpath_ns(doc, '/wps:Execute/wps:DataInputs/wps:Input'): [identifier_el] = xpath_ns(input_el, './ows:Identifier') literal_data = xpath_ns(input_el, './wps:Data/wps:LiteralData') if literal_data: value_el = literal_data[0] the_input.update({identifier_el.text: text_type(value_el.text)}) continue complex_data = xpath_ns(input_el, './wps:Data/wps:ComplexData') if complex_data: complex_data_el = complex_data[0] value_el = complex_data_el[0] tmp = StringIO(lxml.etree.tounicode(value_el)) tmp.mime_type = complex_data_el.attrib.get('mimeType') the_input.update({identifier_el.text: tmp}) continue # TODO bounding box data return the_input
def stream(self): """Return a stream representation of the data.""" if not PY2 and isinstance(self.data, bytes): return BytesIO(self.data) else: return StringIO(text_type(self.data))
def test_stream(self): """Test stream input IOHandler""" source = StringIO(text_type(self._value)) self.iohandler.stream = source self._test_outout(SOURCE_TYPE.STREAM)