<commit_before>import os
import sys
import sip
sip.setapi("QString", 2)

from PyQt4 import QtCore, QtGui
from customtableview import CustomTableView, TripleTableWidget, tr

from openquake.nrmllib.node import node_from_nrml, node_to_nrml
from openquake.nrmllib.record import TableSet


class Dialog(QtGui.QDialog):
    # left for reference, since I (MB) think that the main window of an
    # application is better modelled with a QMainWindow
    def __init__(self, t1, t2, t3):
        QtGui.QDialog.__init__(self)
        self.tt = TripleTableWidget(t1, t2, t3, self)
        self.setWindowTitle(tr("TripleTableWidget Example"))
        # this was the missing call where you set the layout of the tripletable
        # widget to the dialog
        self.setLayout(self.tt.layout())


class MainWindow(QtGui.QMainWindow):
    def __init__(self, nrmlfile):
        QtGui.QMainWindow.__init__(self)
        self.set_central_widget(nrmlfile)
        self.setWindowTitle(tr("Input Tool Window"))
        self.setupMenu()

        # menu actions
        self.actionOpen.triggered.connect(self.open_nrml)
        self.actionSave.triggered.connect(self.save_nrml)
        self.actionCopy.triggered.connect(self.copy)
        self.actionPaste.triggered.connect(self.paste)

    def set_central_widget(self, nrmlfile):
        self.nrmlfile = nrmlfile
        node = node_from_nrml(nrmlfile)[0]
        self.tableset = TableSet.from_node(node)
        if len(self.tableset.tables) != 3:
            # only models with three tables are implemented
            raise NotImplementedError(node.tag)
        self.widget = TripleTableWidget(self.tableset, nrmlfile, self)
        self.setCentralWidget(self.widget)

    def setupMenu(self):
        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setObjectName("menubar")
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.setMenuBar(self.menubar)
        self.actionOpen = QtGui.QAction(self)
        self.actionOpen.setObjectName("actionOpen")
        self.actionSave = QtGui.QAction(self)
        self.actionSave.setObjectName("actionSave")
        self.actionCopy = QtGui.QAction(self)
        self.actionCopy.setObjectName("actionCopy")
        self.actionPaste = QtGui.QAction(self)
        self.actionPaste.setObjectName("actionPaste")
        self.actionUndo = QtGui.QAction(self)
        self.actionUndo.setObjectName("actionUndo")
        self.actionQuit = QtGui.QAction(self)
        self.actionQuit.setObjectName("actionQuit")
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave)
        self.menuFile.addAction(self.actionCopy)
        self.menuFile.addAction(self.actionPaste)
        self.menuFile.addAction(self.actionUndo)
        self.menuFile.addAction(self.actionQuit)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menuFile.setTitle(tr("InputToolWindow"))

        # retranslateUi
        self.menuFile.setTitle(tr("File"))
        self.actionOpen.setText(tr("&Open"))
        self.actionOpen.setShortcut(tr("Ctrl+O"))
        self.actionSave.setText(tr("&Save"))
        self.actionSave.setShortcut(tr("Ctrl+S"))
        self.actionCopy.setText(tr("&Copy"))
        self.actionCopy.setShortcut(tr("Ctrl+C"))
        self.actionPaste.setText(tr("&Paste"))
        self.actionPaste.setShortcut(tr("Ctrl+V"))
        self.actionUndo.setText(tr("&Undo"))
        self.actionUndo.setShortcut(tr("Ctrl+U"))
        self.actionQuit.setText(tr("&Quit"))
        self.actionQuit.setShortcut(tr("Ctrl+Q"))

    def copy(self):
        widget = QtGui.QApplication.focusWidget()
        if not isinstance(widget, QtGui.QTableView):
            return
        currsel = widget.parent().current_selection()
        QtGui.QApplication.clipboard().setText(currsel)

    def paste(self):
        widget = QtGui.QApplication.focusWidget()
        if not isinstance(widget, QtGui.QTableView):
            return
        widget = widget.parent()  # CustomTableView
        lines = QtGui.QApplication.clipboard().text().split('\n')
        rows = [line.split('\t') for line in lines]
        if not rows:
            return
        ncolumns = len(widget.table.recordtype) - widget.table.ordinal
        for row in rows:
            assert len(row) <= ncolumns, 'Got %d columns, expected %d' % (
                len(row), ncolumns)
        indexes = widget.appendRows(len(rows))
        for index, row in zip(indexes, rows):
            widget.tableModel.set_row(index, row)

    def open_nrml(self):
        nrmlfile = unicode(QtGui.QFileDialog.getOpenFileName(
            self, 'Choose file',
            QtCore.QDir.homePath(),
            "Model file (*.xml);;Config files (*.ini)"))
        self.set_central_widget(nrmlfile)

    # TODO: save on a different file
    def save_nrml(self):
        """
        Save the current content of the tableset in NRML format
        """
        with open(self.nrmlfile + '~', 'w') as f:
            node_to_nrml(self.tableset.to_node(), f)
        # only if there were no errors override the original file
        os.rename(self.nrmlfile + '~', self.nrmlfile)


def main(argv):
    if not argv[1:]:
        sys.exit('Please give the input NRML file')

    app = QtGui.QApplication(argv, True)
    app.setStyleSheet('''
QTableWidget::item:selected
{ background-color: palette(highlight)}
''')
    mw = MainWindow(sys.argv[1])
    mw.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main(sys.argv)
<commit_msg>Write functionality<commit_after>import os
import sys
import sip
sip.setapi("QString", 2)

from PyQt4 import QtCore, QtGui
from customtableview import TripleTableWidget, tr

from openquake.nrmllib.node import node_from_nrml, node_to_nrml
from openquake.nrmllib.record import TableSet


class Dialog(QtGui.QDialog):
    # left for reference, since I (MB) think that the main window of an
    # application is better modelled with a QMainWindow
    def __init__(self, t1, t2, t3):
        QtGui.QDialog.__init__(self)
        self.tt = TripleTableWidget(t1, t2, t3, self)
        self.setWindowTitle(tr("TripleTableWidget Example"))
        # this was the missing call where you set the layout of the tripletable
        # widget to the dialog
        self.setLayout(self.tt.layout())


class MainWindow(QtGui.QMainWindow):
    def __init__(self, nrmlfile):
        QtGui.QMainWindow.__init__(self)
        self.set_central_widget(nrmlfile)
        self.setWindowTitle(tr("Input Tool Window"))
        self.setupMenu()

        # menu actions
        self.actionOpen.triggered.connect(self.open_nrml)
        self.actionSave.triggered.connect(self.save_nrml)
        self.actionWrite.triggered.connect(self.write_nrml)
        self.actionCopy.triggered.connect(self.copy)
        self.actionPaste.triggered.connect(self.paste)

    def set_central_widget(self, nrmlfile):
        self.nrmlfile = nrmlfile
        node = node_from_nrml(nrmlfile)[0]
        self.tableset = TableSet.from_node(node)
        if len(self.tableset.tables) != 3:
            # only models with three tables are implemented
            raise NotImplementedError(node.tag)
        self.widget = TripleTableWidget(self.tableset, nrmlfile, self)
        self.setCentralWidget(self.widget)

    def setupMenu(self):
        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setObjectName("menubar")
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.setMenuBar(self.menubar)
        self.actionOpen = QtGui.QAction(self)
        self.actionOpen.setObjectName("actionOpen")
        self.actionSave = QtGui.QAction(self)
        self.actionSave.setObjectName("actionSave")
        self.actionWrite = QtGui.QAction(self)
        self.actionWrite.setObjectName("actionWrite")
        self.actionCopy = QtGui.QAction(self)
        self.actionCopy.setObjectName("actionCopy")
        self.actionPaste = QtGui.QAction(self)
        self.actionPaste.setObjectName("actionPaste")
        self.actionUndo = QtGui.QAction(self)
        self.actionUndo.setObjectName("actionUndo")
        self.actionQuit = QtGui.QAction(self)
        self.actionQuit.setObjectName("actionQuit")
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave)
        self.menuFile.addAction(self.actionWrite)
        self.menuFile.addAction(self.actionCopy)
        self.menuFile.addAction(self.actionPaste)
        self.menuFile.addAction(self.actionUndo)
        self.menuFile.addAction(self.actionQuit)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menuFile.setTitle(tr("InputToolWindow"))

        # retranslateUi
        self.menuFile.setTitle(tr("File"))
        self.actionOpen.setText(tr("&Open"))
        self.actionOpen.setShortcut(tr("Ctrl+O"))
        self.actionSave.setText(tr("&Save"))
        self.actionSave.setShortcut(tr("Ctrl+S"))
        self.actionWrite.setText(tr("&Write"))
        self.actionWrite.setShortcut(tr("Ctrl+W"))
        self.actionCopy.setText(tr("&Copy"))
        self.actionCopy.setShortcut(tr("Ctrl+C"))
        self.actionPaste.setText(tr("&Paste"))
        self.actionPaste.setShortcut(tr("Ctrl+V"))
        self.actionUndo.setText(tr("&Undo"))
        self.actionUndo.setShortcut(tr("Ctrl+U"))
        self.actionQuit.setText(tr("&Quit"))
        self.actionQuit.setShortcut(tr("Ctrl+Q"))

    def copy(self):
        widget = QtGui.QApplication.focusWidget()
        if not isinstance(widget, QtGui.QTableView):
            return
        currsel = widget.parent().current_selection()
        QtGui.QApplication.clipboard().setText(currsel)

    def paste(self):
        widget = QtGui.QApplication.focusWidget()
        if not isinstance(widget, QtGui.QTableView):
            return
        widget = widget.parent()  # CustomTableView
        lines = QtGui.QApplication.clipboard().text().split('\n')
        rows = [line.split('\t') for line in lines]
        if not rows:
            return
        ncolumns = len(widget.table.recordtype) - widget.table.ordinal
        for row in rows:
            assert len(row) <= ncolumns, 'Got %d columns, expected %d' % (
                len(row), ncolumns)
        indexes = widget.appendRows(len(rows))
        for index, row in zip(indexes, rows):
            widget.tableModel.set_row(index, row)

    def open_nrml(self):
        nrmlfile = unicode(QtGui.QFileDialog.getOpenFileName(
            self, 'Choose file',
            QtCore.QDir.homePath(),
            "Model file (*.xml);;Config files (*.ini)"))
        self.set_central_widget(nrmlfile)

    def save_nrml(self):
        """
        Save the current content of the tableset in NRML format
        """
        with open(self.nrmlfile + '~', 'w') as f:
            node_to_nrml(self.tableset.to_node(), f)
        # only if there were no errors override the original file
        os.rename(self.nrmlfile + '~', self.nrmlfile)

    def write_nrml(self):
        """
        Save the current content of the tableset in NRML format
        """
        nrmlfile = QtGui.QFileDialog.getSaveFileName(
            self, 'Save NRML', '', 'XML (*.xml)')
        with open(nrmlfile, 'w') as f:
            node_to_nrml(self.tableset.to_node(), f)


def main(argv):
    if not argv[1:]:
        sys.exit('Please give the input NRML file')

    app = QtGui.QApplication(argv, True)
    app.setStyleSheet('''
QTableWidget::item:selected
{ background-color: palette(highlight)}
''')
    mw = MainWindow(sys.argv[1])
    mw.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main(sys.argv)

{'fraction_non_alphanumeric': 0.11779341111873713, 'fraction_numerical': 0.002059025394646534, 'mean_word_length': 2.820714519829564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5363325', 'n_tokens_mistral': 3684, 'n_tokens_neox': 3667, 'n_words': 653}