From ccbdf72416b4f4674ea8d7e443a93e6946bad6c0 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Sat, 14 Jan 2023 17:05:29 +0100 Subject: [PATCH] agr files with skipped set/graph numbers were not read nor written; fixes T252 --- src/gui_qt/graphs/graphwindow.py | 1 + src/gui_qt/io/filereaders.py | 2 +- src/gui_qt/io/gracereader.py | 9 ++++- src/nmreval/io/graceeditor.py | 59 ++++++++++++++++++++++---------- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/gui_qt/graphs/graphwindow.py b/src/gui_qt/graphs/graphwindow.py index 1ea3c4c..581efbc 100644 --- a/src/gui_qt/graphs/graphwindow.py +++ b/src/gui_qt/graphs/graphwindow.py @@ -530,6 +530,7 @@ class QGraphWindow(QtWidgets.QGraphicsView, Ui_GraphWindow): path = '' outfile = None f = FileDialog(caption='Export graphic', directory=str(path), filter=filters, mode='save') + f.setOption(FileDialog.DontConfirmOverwrite) mode = f.exec() if mode == QtWidgets.QDialog.Accepted: outfile = f.save_file() diff --git a/src/gui_qt/io/filereaders.py b/src/gui_qt/io/filereaders.py index 11d28c2..7ebed93 100755 --- a/src/gui_qt/io/filereaders.py +++ b/src/gui_qt/io/filereaders.py @@ -58,7 +58,7 @@ class QFileReader(QtCore.QObject): try: # If QAsciiReader.skip = True it accepts automatically and returns None r(f).exec() - except AttributeError: + except ImportError: pass self.data_read.emit(self.data) diff --git a/src/gui_qt/io/gracereader.py b/src/gui_qt/io/gracereader.py index 9196e61..6de6008 100644 --- a/src/gui_qt/io/gracereader.py +++ b/src/gui_qt/io/gracereader.py @@ -20,6 +20,8 @@ class QGraceReader(QtWidgets.QDialog, Ui_Dialog): self.treeWidget.installEventFilter(self) def __call__(self, fname, *args, **kwargs): + self.treeWidget.clear() + self.show_property(self.treeWidget.invisibleRootItem(), 0) self.read(fname) return self @@ -43,12 +45,17 @@ class QGraceReader(QtWidgets.QDialog, Ui_Dialog): def read(self, fname): self._reader.parse(fname) + print('halio', len(self._reader.graphs)) for graphs in self._reader.graphs: item = QtWidgets.QTreeWidgetItem([f'Graph {graphs.idx} (Title "{graphs.get_property("title")}")']) for gset in graphs.set: + ds = self._reader.dataset(graphs.idx, gset.idx) + if ds is None: + continue + item_2 = QtWidgets.QTreeWidgetItem([f'Set {gset.idx} (Label: {gset.get_property("legend")}, ' - f'shape: {self._reader.dataset(graphs.idx, gset.idx).shape})']) + f'shape: {ds.shape})']) item_2.setCheckState(0, QtCore.Qt.Checked) item_2.setData(0, QtCore.Qt.UserRole, (graphs.idx, gset.idx)) item.addChild(item_2) diff --git a/src/nmreval/io/graceeditor.py b/src/nmreval/io/graceeditor.py index 8427c6a..59c25e9 100644 --- a/src/nmreval/io/graceeditor.py +++ b/src/nmreval/io/graceeditor.py @@ -19,6 +19,7 @@ class GraceEditor: _RE_REGION_START = re.compile(r'@r(\w+) (on|off)', re.IGNORECASE) _RE_GRAPH_START = re.compile(r'@g(\w+) (on|off)', re.IGNORECASE) _RE_SET_START = re.compile(r'@target G(\d+).S(\d+)', re.IGNORECASE) + _RE_SET_END = re.compile(r'^\s*&\s*$') _RE_COLOR = re.compile(r'@map color (?P\d+) to ' r'\((?P\d+), (?P\d+), (?P\d+)\),\s+\"(?P.+)\"', re.MULTILINE) @@ -113,9 +114,7 @@ class GraceEditor: if self._RE_HEADER_END.match(line): current_pos = 'header_end' elif self._RE_GRAPH_START.match(line): - current_pos = 'graph' - self.graphs.append(GraceGraph(int(self._RE_GRAPH_START.match(line).group(1)))) - self.graphs[-1].append(line) + current_pos = self._make_graph(line) elif current_pos == 'header_end': # what comes after the header? region, graph or drawing object? @@ -124,9 +123,7 @@ class GraceEditor: self.regions.append(GraceRegion()) self.regions[-1].append(line) elif self._RE_GRAPH_START.match(line): - current_pos = 'graph' - self.graphs.append(GraceGraph(int(self._RE_GRAPH_START.match(line).group(1)))) - self.graphs[-1].append(line) + current_pos = self._make_graph(line) elif self._RE_OBJECT_START.match(line): current_pos = 'drawing' self.drawing_objects.append(GraceDrawing()) @@ -152,10 +149,8 @@ class GraceEditor: elif current_pos == 'region': # regions are followed by regions or graphs - m = self._RE_GRAPH_START.match(line) - if m: - current_pos = 'graph' - self.graphs.append(GraceGraph(int(m.group(1)))) + if self._RE_GRAPH_START.match(line): + current_pos = self._make_graph(line) self.graphs[-1].append(line) else: if self._RE_REGION_START.match(line): @@ -169,17 +164,41 @@ class GraceEditor: self.sets.append(GraceSet(int(m.group(1)), int(m.group(2)))) self.sets[-1].append(line) else: - m = self._RE_GRAPH_START.match(line) - if m: - self.graphs.append(GraceGraph(int(m.group(1)))) + if self._RE_GRAPH_START.match(line): + current_pos = self._make_graph(line) self.graphs[-1].append(line) elif current_pos == 'set': + m = self._RE_SET_END.match(line) + if m: + current_pos = 'set_end' + self.sets[-1].append(line) + + elif current_pos == 'set_end': m = self._RE_SET_START.match(line) if m: current_pos = 'set' self.sets.append(GraceSet(int(m.group(1)), int(m.group(2)))) - self.sets[-1].append(line) + self.sets[-1].append(line) + else: + if self._RE_GRAPH_START.match(line): + current_pos = self._make_graph(line) + else: + if GraceGraph._RE_SET_START.match(line): + current_pos = 'graph' + else: + raise ValueError(f'Cannot parse line {line}') + + self.graphs[-1].append(line) + + def _make_graph(self, line: str): + m = self._RE_GRAPH_START.match(line) + g_idx = int(m.group(1)) + while g_idx != len(self.graphs): + self.graphs.append(GraceGraph(len(self.graphs))) + self.graphs.append(GraceGraph(g_idx)) + + return 'graph' @property def colors(self): @@ -329,12 +348,8 @@ class GraceProperties(list): if k == key: props.append(_convert_to_value(m.group('val'))) - if len(props) == 0: - raise KeyError(f'No attribute `{key}` found') - - elif len(props) == 1: + if len(props) == 1: return props[0] - else: return @@ -426,6 +441,10 @@ class GraceGraph(GraceProperties): if self.__current_pos == 'graph' or self.__current_idx != m.group(1): self.__current_pos = 'set' self.__current_idx = m.group(1) + + while int(self.__current_idx) != len(self.set): + self.set.append(GraceSetProps(len(self.set))) + self.set.append(GraceSetProps(self.__current_idx)) if self.__current_pos == 'graph': @@ -438,6 +457,8 @@ class GraceGraph(GraceProperties): def new_set(self): max_set_idx = -1 for s in self.set: + if s is None: + continue max_set_idx = max(max_set_idx, s.idx) self.set.append(GraceSetProps(max_set_idx+1))