Skip to content

Commit

Permalink
Fix importer fewer columns than expected Traceback (#160)
Browse files Browse the repository at this point in the history
Now when an importer spec is modified to have a column
reference that is out of range for the source data, an
error is shown. If such importer spec is executed an
error will also be thrown and logged.

Re spine-tools/Spine-Toolbox#2333
  • Loading branch information
PiispaH authored Oct 5, 2023
1 parent d1acf76 commit 53945d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
14 changes: 14 additions & 0 deletions spine_items/importer/mvcmodels/mappings_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,20 @@ def set_filter_re(self, table_row, list_row, row, filter_re):
index = self.index(row, FlattenedColumn.REGEXP, list_index)
self.dataChanged.emit(index, index, [Qt.ItemDataRole.DisplayRole])

def check_validity_of_columns(self, list_index, header):
"""Checks that the mapping doesn't have column refs
that are larger than the source table column count
Args:
list_index (QModelIndex): index to mappings list
header (Iterable): source table header
Returns:
bool: True if no source ref. is out of range, False otherwise
"""
mapping_list_item = list_index.internalPointer()
table_name = mapping_list_item.source_table_item.name
return mapping_list_item.flattened_mappings.root_mapping.check_for_invalid_column_refs(header, table_name)

@staticmethod
def polish_mapping(list_index, header):
"""Polishes flattened mappings.
Expand Down
23 changes: 20 additions & 3 deletions spine_items/importer/widgets/import_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
IntegerSequenceDateTimeConvertSpec,
StringConvertSpec,
)
from spinedb_api.exception import InvalidMappingComponent
from .custom_menus import SourceListMenu, SourceDataTableMenu
from .mime_types import MAPPING_LIST_MIME_TYPE, TABLE_OPTIONS_MIME_TYPE
from .options_widget import OptionsWidget
Expand Down Expand Up @@ -86,7 +87,7 @@ def __init__(self, mappings_model, ui, undo_stack, parent):
# connect signals
self._mappings_model.modelAboutToBeReset.connect(self._store_source_list_current_index)
self._mappings_model.modelReset.connect(self._restore_source_list_current_index)
self._mappings_model.dataChanged.connect(self._update_source_table_colors)
self._mappings_model.dataChanged.connect(self._handle_mapping_data_changed)
self._mappings_model.row_or_column_type_recommended.connect(self._source_data_model.set_type)
self._mappings_model.multi_column_type_recommended.connect(self._source_data_model.set_all_column_types)
self._ui_options_widget.options_changed.connect(lambda _: self._clear_source_data_model())
Expand All @@ -103,6 +104,20 @@ def __init__(self, mappings_model, ui, undo_stack, parent):
self._source_data_model.row_types_updated.connect(self._new_row_types)
self._source_data_model.polish_mapping_requested.connect(self._polish_mappings_in_list)

@Slot(QModelIndex, QModelIndex, list)
def _handle_mapping_data_changed(self, top_left, bottom_right, roles):
self._update_source_table_colors(top_left, bottom_right, roles)
table_index = self._ui.source_list.selectionModel().currentIndex()
if not table_index.isValid() or table_index.row() == 0:
return
header = self._source_data_model.header
for list_row in range(self._mappings_model.rowCount(table_index)):
list_index = self._mappings_model.index(list_row, 0, table_index)
msg = self._mappings_model.check_validity_of_columns(list_index, header)
if msg:
self.parent().show_error(msg)
return

def set_connector(self, connector, mapping):
"""Sets connector.
Expand Down Expand Up @@ -141,7 +156,10 @@ def _polish_mappings_in_list(self):
header = self._source_data_model.header
for list_row in range(self._mappings_model.rowCount(table_index)):
list_index = self._mappings_model.index(list_row, 0, table_index)
self._mappings_model.polish_mapping(list_index, header)
try:
self._mappings_model.polish_mapping(list_index, header)
except InvalidMappingComponent as error:
self.parent().show_error(str(error))

@Slot(str)
def _select_table_for_undo(self, table_name):
Expand Down Expand Up @@ -247,7 +265,6 @@ def _reset_default_column_type(self, table_name):
self._ui.default_column_type_combo_box.setCurrentText(column_type)
self._ui.default_column_type_combo_box.currentTextChanged.connect(self._set_default_column_type)

@Slot(QModelIndex, QModelIndex, list)
def _update_source_table_colors(self, top_left, bottom_right, roles):
"""Notifies source table model that colors have changed.
Expand Down

0 comments on commit 53945d0

Please sign in to comment.