def save(self, event): """Saves a file that is specified in event.attr Parameters ---------- event.attr: Dict \tkey filepath contains file path of file to be saved """ filepath = event.attr["filepath"] io_error_text = _("Error writing to file {filepath}.") io_error_text = io_error_text.format(filepath=filepath) # Set state to file saving self.saving = True # Make sure that old save file does not get lost on abort save tmpfile = filepath + "~" try: wx.BeginBusyCursor() self.grid.Disable() with Bz2AOpen(tmpfile, "wb", main_window=self.main_window) \ as outfile: try: pys = Pys(self.grid.code_array, outfile) pys.from_code_array() except ValueError, err: post_command_event(self.main_window, self.StatusBarMsg, text=err) # Move save file from temp file to filepath try: os.rename(tmpfile, filepath) except OSError: # No tmp file present pass
def test_from_code_array(self): """Test from_code_array method""" self.pys_infile.seek(0) self.pys_in.to_code_array() outfile = bz2.BZ2File(self.pys_outfile_path, "w") pys_out = Pys(self.code_array, outfile) pys_out.from_code_array() outfile.close() self.pys_infile.seek(0) in_data = self.pys_infile.read() outfile = bz2.BZ2File(self.pys_outfile_path) out_data = outfile.read() outfile.close() # Clean up the test dir os.remove(self.pys_outfile_path) assert in_data == out_data