<commit_before>#!/usr/bin/python3
#
#  Copyright (C) 2015-2022 Sustainable Energy Now Inc., Angus King
#
#  editini.py - This file is part of SIREN.
#
#  SIREN is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as
#  published by the Free Software Foundation, either version 3 of
#  the License, or (at your option) any later version.
#
#  SIREN is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General
#  Public License along with SIREN.  If not, see
#  <http://www.gnu.org/licenses/>.
#

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QDesktopWidget
import configparser   # decode .ini file
import datetime
import os
import sys

from displaytable import Table
from getmodels import getModelFile, commonprefix
import inisyntax
from senutils import ClickableQLabel, getParents, getUser, techClean


class EdtDialog(QtWidgets.QDialog):
    def __init__(self, in_file, parent=None, line=None, section=None):
        self.in_file = in_file
        try:
            s = open(self.in_file, 'r').read()
            bits = s.split('\n')
            ln = 0
            for l in range(len(bits)):
                if len(bits[l]) > ln:
                    ln = len(bits[l])
                if section != None:
                    if bits[l] == section:
                        line = l
            ln2 = len(bits)
        except:
            if self.in_file[self.in_file.rfind('.'):] == '.ini':
                s = ''
            else:
                s = ''
            ln = 36
            ln2 = 5
        QtWidgets.QDialog.__init__(self, parent)
        self.saveButton = QtWidgets.QPushButton(self.tr('&Save'))
        self.cancelButton = QtWidgets.QPushButton(self.tr('Cancel'))
        buttonLayout = QtWidgets.QHBoxLayout()
        buttonLayout.addStretch(1)
        buttonLayout.addWidget(self.saveButton)
        buttonLayout.addWidget(self.cancelButton)
        self.saveButton.clicked.connect(self.accept)
        self.cancelButton.clicked.connect(self.reject)
        self.widget = QtWidgets.QPlainTextEdit()
        highlight = inisyntax.IniHighlighter(self.widget.document(), line=line)
        if sys.platform == 'linux' or sys.platform == 'linux2':
            self.widget.setFont(QtGui.QFont('Ubuntu Mono 13', 12))
        else:
            self.widget.setFont(QtGui.QFont('Courier New', 12))
        fnt = self.widget.fontMetrics()
        ln = (ln + 5) * fnt.maxWidth()
        ln2 = (ln2 + 4) * fnt.height()
        screen = QDesktopWidget().availableGeometry()
        if ln > screen.width() * .67:
            ln = int(screen.width() * .67)
        if ln2 > screen.height() * .67:
            ln2 = int(screen.height() * .67)
        self.widget.resize(ln, ln2)
        self.widget.setPlainText(s)
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.widget)
        layout.addLayout(buttonLayout)
        self.setLayout(layout)
        self.setWindowTitle('SIREN - Edit - ' + self.in_file[self.in_file.rfind('/') + 1:])
        self.setWindowIcon(QtGui.QIcon('sen_icon32.ico'))
        size = self.geometry()
        self.setGeometry(1, 1, ln + 10, ln2 + 35)
        size = self.geometry()
        self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))
        self.widget.show()

    def accept(self):
        t = self.widget.toPlainText()
        bits = t.split('\n')
        if self.in_file[self.in_file.rfind('.'):] != '.ini':
            bits.sort()
            while len(bits[0]) == 0:
                del bits[0]
        s = open(self.in_file, 'w')
        for lin in bits:<commit_msg>Move cursor to display highlighted line or section<commit_after>#!/usr/bin/python3
#
#  Copyright (C) 2015-2022 Sustainable Energy Now Inc., Angus King
#
#  editini.py - This file is part of SIREN.
#
#  SIREN is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as
#  published by the Free Software Foundation, either version 3 of
#  the License, or (at your option) any later version.
#
#  SIREN is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General
#  Public License along with SIREN.  If not, see
#  <http://www.gnu.org/licenses/>.
#

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QDesktopWidget
import configparser   # decode .ini file
import datetime
import os
import sys

from displaytable import Table
from getmodels import getModelFile, commonprefix
import inisyntax
from senutils import ClickableQLabel, getParents, getUser, techClean


class EdtDialog(QtWidgets.QDialog):
    def __init__(self, in_file, parent=None, line=None, section=None):
        self.in_file = in_file
        try:
            s = open(self.in_file, 'r').read()
            bits = s.split('\n')
            ln = 0
            for l in range(len(bits)):
                if len(bits[l]) > ln:
                    ln = len(bits[l])
                if section is not None:
                    if bits[l] == section:
                        line = l
            ln2 = len(bits)
        except:
            if self.in_file[self.in_file.rfind('.'):] == '.ini':
                s = ''
            else:
                s = ''
            ln = 36
            ln2 = 5
        QtWidgets.QDialog.__init__(self, parent)
        self.saveButton = QtWidgets.QPushButton(self.tr('&Save'))
        self.cancelButton = QtWidgets.QPushButton(self.tr('Cancel'))
        buttonLayout = QtWidgets.QHBoxLayout()
        buttonLayout.addStretch(1)
        buttonLayout.addWidget(self.saveButton)
        buttonLayout.addWidget(self.cancelButton)
        self.saveButton.clicked.connect(self.accept)
        self.cancelButton.clicked.connect(self.reject)
        self.widget = QtWidgets.QPlainTextEdit()
        highlight = inisyntax.IniHighlighter(self.widget.document(), line=line)
        if sys.platform == 'linux' or sys.platform == 'linux2':
            self.widget.setFont(QtGui.QFont('Ubuntu Mono 13', 12))
        else:
            self.widget.setFont(QtGui.QFont('Courier New', 12))
        fnt = self.widget.fontMetrics()
        ln = (ln + 5) * fnt.maxWidth()
        ln2 = (ln2 + 4) * fnt.height()
        screen = QDesktopWidget().availableGeometry()
        if ln > screen.width() * .67:
            ln = int(screen.width() * .67)
        if ln2 > screen.height() * .67:
            ln2 = int(screen.height() * .67)
        self.widget.resize(ln, ln2)
        self.widget.setPlainText(s)
        if section is not None:
            where = self.widget.find(section)
        elif line is not None:
            for l in range(line):
                self.widget.moveCursor(QtGui.QTextCursor.NextBlock)
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.widget)
        layout.addLayout(buttonLayout)
        self.setLayout(layout)
        self.setWindowTitle('SIREN - Edit - ' + self.in_file[self.in_file.rfind('/') + 1:])
        self.setWindowIcon(QtGui.QIcon('sen_icon32.ico'))
        size = self.geometry()
        self.setGeometry(1, 1, ln + 10, ln2 + 35)
        size = self.geometry()
        self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))
        self.widget.show()

    def accept(self):
        t = self.widget.toPlainText()
        bits = t.split('\n')
        if self.in_file[self.in_file.rfind('.'):] != '.ini':
            bits.sort()
            while len(bits[0]) == 0:
                del bits[0]
        s = open(self.in_file, 'w')
        for lin in bits:
{'fraction_non_alphanumeric': 0.11754473161033797, 'fraction_numerical': 0.013916500994035786, 'mean_word_length': 2.2573856738162688, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '5338849', 'n_tokens_mistral': 2580, 'n_tokens_neox': 2452, 'n_words': 651}