def _export_csv(self, filepath, data): """CSV import workflow""" # Get csv info csv_info = self.main_window.interfaces.get_csv_export_info(data) if csv_info is None: return try: dialect, digest_types, has_header = csv_info except TypeError: return # Export CSV file csv_interface = CsvInterface(filepath, dialect, digest_types, has_header) try: csv_interface.write(data) except IOError, err: msg = _("The file {} could not be fully written\n \n" "Error message:\n{}").format(filepath, err) short_msg = _('Error writing CSV file') self.main_window.interfaces.display_warning(msg, short_msg)
def setup_method(self, method): self.main_window = MainWindow(None, -1) self.grid = self.main_window.grid self.code_array = self.grid.code_array filepath = TESTPATH + 'test1.csv' self.dialect, __ = sniff(filepath) self.digest_types = [types.UnicodeType] has_header = True self.interface = CsvInterface(self.main_window, filepath, self.dialect, self.digest_types, has_header)
def test_write(self): """Unit test for write""" filepath = TESTPATH + "dummy.csv" interface = CsvInterface(self.main_window, filepath, self.dialect, self.digest_types, False) interface.write([["test", "world"], ["", "hello"]]) dummy = open(filepath, "w") interface.write(["test", "world"]) dummy.close() os.remove(filepath)
def setup_method(self, method): self.main_window = MainWindow(None, -1) self.grid = self.main_window.grid self.code_array = self.grid.code_array filepath = TESTPATH + "test1.csv" self.dialect, __ = sniff(filepath) self.digest_types = [types.UnicodeType] has_header = True self.interface = CsvInterface(self.main_window, filepath, self.dialect, self.digest_types, has_header)
def _export_csv(self, filepath, data, preview_data): """CSV export of code_array results Parameters ---------- filepath: String \tPath of export file data: Object \tCode array result object slice, i. e. one object or iterable of \tsuch objects """ # Get csv info csv_info = \ self.main_window.interfaces.get_csv_export_info(preview_data) if csv_info is None: return try: dialect, digest_types, has_header = csv_info except TypeError: return # Export CSV file csv_interface = CsvInterface(self.main_window, filepath, dialect, digest_types, has_header) try: csv_interface.write(data) except IOError, err: msg = _("The file {filepath} could not be fully written\n \n" "Error message:\n{msg}") msg = msg.format(filepath=filepath, msg=err) short_msg = _('Error writing CSV file') self.main_window.interfaces.display_warning(msg, short_msg)
class TestCsvInterface(object): """Unit tests for CsvInterface""" def setup_method(self, method): self.main_window = MainWindow(None, -1) self.grid = self.main_window.grid self.code_array = self.grid.code_array filepath = TESTPATH + 'test1.csv' self.dialect, __ = sniff(filepath) self.digest_types = [types.UnicodeType] has_header = True self.interface = CsvInterface(self.main_window, filepath, self.dialect, self.digest_types, has_header) # def test_iter(self): # """Unit test for __iter__""" # # testline = [u"Test1", u"234", u"3.34", u"2012/12/04"] # # for i, line in enumerate(self.interface): # if i: # for j, ele in enumerate(line): # assert ele == repr(testline[j]) def test_get_csv_cells_gen(self): """Unit test for _get_csv_cells_gen""" data = [u'324', u'234', u'sdfg'] res = self.interface._get_csv_cells_gen(data) for ele, rele in zip(data, res): assert repr(ele) == rele def test_write(self): """Unit test for write""" filepath = TESTPATH + 'dummy.csv' interface = CsvInterface(self.main_window, filepath, self.dialect, self.digest_types, False) interface.write([["test", "world"], ["", "hello"]]) dummy = open(filepath, "w") interface.write(["test", "world"]) dummy.close() os.remove(filepath)
def test_write(self): """Unit test for write""" filepath = TESTPATH + 'dummy.csv' interface = CsvInterface(self.main_window, filepath, self.dialect, self.digest_types, False) interface.write([["test", "world"], ["", "hello"]]) dummy = open(filepath, "w") interface.write(["test", "world"]) dummy.close() os.remove(filepath)
def _import_csv(self, path): """CSV import workflow""" # If path is not set, do nothing if not path: return # Get csv info try: dialect, has_header, digest_types, encoding = \ self.main_window.interfaces.get_csv_import_info(path) except IOError: msg = _("Error opening file {filepath}.").format(filepath=path) post_command_event(self.main_window, self.StatusBarMsg, text=msg) return except TypeError: return # Import is aborted or empty return CsvInterface(self.main_window, path, dialect, digest_types, has_header, encoding)