def _write(self, raw_data, filename) : """ Write raw data to a compressed file. @arg raw_data: The raw_data to be compressed and written @type raw_data: byte string @arg filename: The intended name of the outfile @type filename: unicode @return: outfile ; The full path and name of the file written @rtype: unicode """ result = chardet.detect(raw_data) if result['confidence'] > 0.5: encoding = result['encoding'] else: encoding = 'utf-8' if not util.is_utf8_alias(encoding): raw_data = raw_data.decode(encoding).encode('utf-8') # Compress the data to save disk space. comp = bz2.BZ2Compressor() data = comp.compress(raw_data) data += comp.flush() out_handle = open(self._nametofile(filename), "wb") out_handle.write(data) out_handle.close() return out_handle.name # return the full path to the file
def _write(self, raw_data, filename): """ Write raw data to a compressed file. :arg str raw_data: The raw_data to be compressed and written. :arg unicode filename: The intended name of the output filename. :returns: The full path and name of the file written. :rtype: unicode """ result = chardet.detect(raw_data) if result['confidence'] > 0.5: encoding = unicode(result['encoding']) else: encoding = 'utf-8' if not util.is_utf8_alias(encoding): try: raw_data = raw_data.decode(encoding).encode('utf-8') except UnicodeDecodeError: self._output.addMessage( __file__, 4, 'ENOPARSE', 'Could not decode file (using {} encoding).'.format( encoding)) return None # Compress the data to save disk space. comp = bz2.BZ2Compressor() data = comp.compress(raw_data) data += comp.flush() out_handle = open(self._name_to_file(filename), 'wb') out_handle.write(data) out_handle.close() # Return the full path to the file. return out_handle.name