Esempio n. 1
0
    def accept(self):
        """ Check the table details are valid and import the data into a new table or
        append to an existing table. """

        if not self.success:
            super(DialogImportSurvey, self).accept()
            return

        # check for duplicate field names
        if len(self.fields) != len(set(self.fields)):
            msg = "There are duplicate attribute names."
            Message(self.app, _("Attribute name error"), msg,
                    "warning").exec_()
            logger.info(
                _("Survey Not Imported. Attribute duplicate name error: ") +
                msg)
            self.parent_textEdit.append(msg)
            self.fields = []
            return

        # check for appropriate quote format
        # inappropriate may produce error IndexError: list index out of range
        quote_format_error = False
        for val in self.data:
            if len(val) != len(self.fields_type):
                quote_format_error = True
        if quote_format_error:
            msg = _(
                "Number of fields does not match header\nPossible wrong quote format"
            )
            logger.error(_("Survey not loaded: ") + msg)
            Message(self.app, _("Survey not loaded"), msg, "warning").exec_()
            return
        self.insert_data()
        super(DialogImportSurvey, self).accept()
Esempio n. 2
0
    def change_fieldname(self):
        """ Change the fieldname. """

        fieldname, ok = QtWidgets.QInputDialog.getText(
            None, _("Change field name"), _("New name:"),
            QtWidgets.QLineEdit.Normal, self.fields[self.headerIndex])
        if not ok:
            return
        # check valid values
        if re.match("^[a-zA-Z_\s][a-zA-Z0-9_\s]*$",
                    fieldname) is None or fieldname == "":
            msg = _(
                "Name must contain only letters and numbers or '_' and must not start with a number"
            )
            Message(self.app, _("Field name invalid"), msg, "warning").exec_()
            return
        if fieldname in self.preexisting_fields or fieldname in self.fields:
            msg = fieldname + _(" Already in use")
            Message(self.app, _("Field name invalid."), msg, "warning").exec_()
            return

        self.fields[self.headerIndex] = fieldname
        item = QtWidgets.QTableWidgetItem(self.fields[self.headerIndex] + "\n" + \
                                          self.fields_type[self.headerIndex])
        self.ui.tableWidget.setHorizontalHeaderItem(self.headerIndex, item)
    def send_telemetry(self) -> None:
        messages_to_send = 20

        # keep track of all our messages
        outstanding_messages = []

        for i in range(0, messages_to_send):
            logger.info("Sending telemetry {}".format(i))

            # Make a message
            payload = {"index": i, "text": "This is message # {}".format(i)}
            msg = Message(payload)

            # Get our telemetry topic
            telemetry_topic = topic_builder.build_telemetry_publish_topic(
                self.auth.device_id, self.auth.module_id, msg)

            # publish.  Don't wait for the PUBACK
            mi = self.mqtt_client.publish(telemetry_topic,
                                          msg.get_binary_payload(),
                                          qos=1)

            if mi.rc:
                raise Exception(mqtt.error_string(mi.rc))
            # Remember the message info for later
            outstanding_messages.append(mi)

        # Now go through and wait for everything to publish.
        for mi in outstanding_messages:
            mi.wait_for_publish()
Esempio n. 4
0
    def link_clicked(self, position):
        """ View image or audio/video media in dialog.
        For A/V, added try block in case VLC bindings do not work.
        Also check existence of media, as particularly, linked files may have bad links. """

        cursor = self.ui.textBrowser.cursorForPosition(position)
        menu = QtWidgets.QMenu()
        menu.setStyleSheet("QMenu {font-size:" +
                           str(self.app.settings['fontsize']) + "pt} ")
        action_link = None
        for item in self.display_text_links:
            if cursor.position() >= item['pos0'] and cursor.position(
            ) <= item['pos1']:
                action_link = menu.addAction(_("Open"))
        action = menu.exec_(self.ui.textBrowser.mapToGlobal(position))
        if action is None:
            return

        for item in self.display_text_links:
            if cursor.position() >= item['pos0'] and cursor.position(
            ) <= item['pos1']:
                try:
                    ui = None
                    abs_path = ""
                    if item['mediapath'][:6] == "video:":
                        abs_path = item['mediapath'].split(':')[1]
                        if not os.path.exists(abs_path):
                            return
                        ui = DialogViewAV(self.app, item)
                    if item['mediapath'][:6] == "/video":
                        abs_path = self.app.project_path + item['mediapath']
                        if not os.path.exists(abs_path):
                            return
                        ui = DialogViewAV(self.app, item)
                    if item['mediapath'][:6] == "audio:":
                        abs_path = item['mediapath'].split(':')[1]
                        if not os.path.exists(abs_path):
                            return
                        ui = DialogViewAV(self.app, item)
                    if item['mediapath'][0:6] == "/audio":
                        abs_path = self.app.project_path + item['mediapath']
                        if not os.path.exists(abs_path):
                            return
                        ui = DialogViewAV(self.app, item)
                    if item['mediapath'][0:7] == "images:":
                        abs_path = item['mediapath'].split(':')[1]
                        if not os.path.exists(abs_path):
                            return
                        ui = DialogViewImage(self.app, item)
                    if item['mediapath'][0:7] == "/images":
                        abs_path = self.app.project_path + item['mediapath']
                        if not os.path.exists(abs_path):
                            return
                        ui = DialogViewImage(self.app, item)
                    ui.exec_()
                except Exception as e:
                    logger.debug(str(e))
                    print(e)
                    Message(self.app, 'view av/images error', str(e),
                            "warning").exec_()
Esempio n. 5
0
    def cell_modified(self):
        """ If the case name has been changed in the table widget update the database.
         Cells that can be changed directly are the case name, and attributes. """

        x = self.ui.tableWidget.currentRow()
        y = self.ui.tableWidget.currentColumn()
        if y == self.NAME_COLUMN:  # update case name
            new_text = str(self.ui.tableWidget.item(x, y).text()).strip()
            # check that no other case name has this text and this is not empty
            update = True
            if new_text == "":
                update = False
            for c in self.cases:
                if c['name'] == new_text:
                    update = False
            if update:
                cur = self.app.conn.cursor()
                cur.execute("update cases set name=? where caseid=?",
                            (new_text, self.cases[x]['caseid']))
                self.app.conn.commit()
                self.cases[x]['name'] = new_text
            else:  # put the original text in the cell
                self.ui.tableWidget.item(x, y).setText(self.cases[x]['name'])
        if y > 2:  # update attribute value
            value = str(self.ui.tableWidget.item(x, y).text()).strip()
            attribute_name = self.header_labels[y]
            #print(attribute_name)
            cur = self.app.conn.cursor()

            # Check numeric for numeric attributes, clear "" if cannot be cast
            cur.execute(
                "select valuetype from attribute_type where caseOrFile='case' and name=?",
                (attribute_name, ))
            result = cur.fetchone()
            if result is None:
                return
            if result[0] == "numeric":
                try:
                    float(value)
                except Exception as e:
                    self.ui.tableWidget.item(x, y).setText("")
                    value = ""
                    msg = _("This attribute is numeric")
                    Message(self.app, _("Warning"), msg, "warning").exec_()

            cur.execute(
                "update attribute set value=? where id=? and name=? and attr_type='case'",
                (value, self.cases[x]['caseid'], attribute_name))
            self.app.conn.commit()
            # Reload attributes
            sql = "select attribute.name, value, id from attribute where attr_type='case'"
            cur.execute(sql)
            result = cur.fetchall()
            self.attributes = []
            for row in result:
                self.attributes.append(row)
        self.app.delete_backup = False
Esempio n. 6
0
    def __init__(self, app, data, title, selectionmode):
        """ present list of names to user for selection.

        params:
            data: list of dictionaries containing the key 'name'
            title: Dialog title, String
            selectionmode: 'single' or anything else for 'multiple', String
        """

        sys.excepthook = exception_handler
        QtWidgets.QDialog.__init__(self)
        self.ui = Ui_Dialog_selectitems()
        self.ui.setupUi(self)
        font = 'font: ' + str(app.settings['fontsize']) + 'pt '
        font += '"' + app.settings['font'] + '";'
        self.setStyleSheet(font)
        self.setWindowFlags(self.windowFlags()
                            & ~QtCore.Qt.WindowContextHelpButtonHint)
        self.setWindowTitle(title)
        self.selection_mode = selectionmode
        # Check data exists
        if len(data) == 0:
            Message(app, _('Dictionary is empty'), _("No data to select from"),
                    "warning")
        # Check for 'name' key
        no_name_key = False

        for d in data:
            if not d['name']:
                no_name_key = True
        if no_name_key:
            text = _("This data does not contain names to select from")
            Message(app, _('Dictionary has no "name" key'), text, "warning")

        self.dict_list = data
        self.model = list_model(self.dict_list)
        self.ui.listView.setModel(self.model)
        if self.selection_mode == "single":
            self.ui.listView.setSelectionMode(
                QtWidgets.QAbstractItemView.SingleSelection)
        else:
            self.ui.listView.setSelectionMode(
                QtWidgets.QAbstractItemView.ExtendedSelection)
        self.ui.listView.doubleClicked.connect(self.accept)
Esempio n. 7
0
    def send_telemetry(self) -> None:
        messages_to_send = 20
        outstanding_messages = []
        start = time.time()
        for i in range(0, messages_to_send):
            logger.info("Sending telemetry {}".format(i))
            payload = {
                "index": i,
                "text": "Hello from sample_4.  This is message # {}".format(i),
            }
            msg = Message(payload)

            telemetry_topic = topic_builder.build_telemetry_publish_topic(
                self.auth.device_id, self.auth.module_id, msg
            )
            mi = self.mqtt_client.publish(
                telemetry_topic, msg.get_binary_payload(), qos=1
            )

            outstanding_messages.append(mi)
            outstanding_messages = self.remove_completed_messages(
                outstanding_messages
            )

            print(
                "{} sent, {} awaiting PUBACK".format(
                    i + 1, len(outstanding_messages)
                )
            )

        while len(outstanding_messages):
            print("Waiting for {} messages".format(len(outstanding_messages)))
            outstanding_messages[0].wait_for_publish()
            outstanding_messages = self.remove_completed_messages(
                outstanding_messages
            )
        end = time.time()

        print("Done sending telemetry.")
        print(
            "{} messages sent in {} seconds.  {} messages per second".format(
                messages_to_send, end - start, messages_to_send / (end - start)
            )
        )
Esempio n. 8
0
def worker_process():
    while True:
        data = comm.bcast(None, root=0)
        board_message = Message.from_dict(data)

        if board_message.type == EVENT.SEND_BOARD:
            # get board copy from payload
            board: Board = board_message.payload
            while True:
                # send request for task
                helpers.send_msg_to_master(Message(EVENT.SEND_TASK))
                # receive task
                task_message = helpers.recv_msg(MASTER_PID)
                # if current move is calculated, go request new board
                if task_message.type == EVENT.BOARD_COMPLETE:
                    break

                moves_made = 0
                last_mover = Mover.CPU

                tasks: List = task_message.payload
                for i, move in enumerate(tasks):
                    # play moves from task and check if move leeds to game over
                    mover = Mover.CPU if i % 2 == 0 else Mover.PLAYER
                    if board.is_move_legal(move):
                        board.move(move, mover)
                        moves_made += 1
                        last_mover = mover
                        if board.is_game_over():
                            send_result = Winner.CPU if mover == Mover.CPU else Winner.PLAYER
                            helpers.send_msg_to_master(
                                Message(EVENT.SEND_RESULT,
                                        (send_result, tasks[0])))
                            break

                result = board.evaluate(last_mover, DEPTH - len(tasks))
                helpers.send_msg_to_master(
                    Message(EVENT.SEND_RESULT, (result, tasks[0])))

                for _ in range(moves_made):
                    board.undo_move()  # clean moves

        time.sleep(1)  # only while waiting for board
Esempio n. 9
0
def CPU_move():
    board = deepcopy(BOARD)  # make copy of original board to simulate plays
    message = Message(EVENT.SEND_BOARD, board)
    comm.bcast(message.to_dict(), root=MASTER_PID)

    tasks = helpers.generate_tasks(BOARD.total_columns, DEPTH)
    results = dict()
    for i in range(1, BOARD.total_columns + 1):
        results[i] = list()

    while len(tasks):
        status = MPI.Status()
        # wait on message from any worker
        data = comm.recv(source=MPI.ANY_SOURCE, status=status)
        # parse message
        message = Message.from_dict(data)
        # get ready worker ID
        from_pid = status.Get_source()

        if message.type == EVENT.SEND_TASK:
            # if message was request for task
            send_task_msg = Message(EVENT.SEND_TASK, tasks.pop())
            # send that worker next task
            helpers.send_msg_to_worker(send_task_msg, from_pid)

        if message.type == EVENT.SEND_RESULT:
            data = message.payload  # [result, column]
            results[data[1]].append(data[0])

    message_board_complete = Message(EVENT.BOARD_COMPLETE)
    for pid in range(1, SIZE):
        helpers.send_msg_to_worker(message_board_complete, pid)

    return helpers.calculate_best_move(results)
Esempio n. 10
0
    def export_attributes(self):
        """ Export attributes from table as a csv file. """

        shortname = self.app.project_name.split(".qda")[0]
        filename = shortname + "_case_attributes.csv"
        options = QtWidgets.QFileDialog.DontResolveSymlinks | QtWidgets.QFileDialog.ShowDirsOnly
        directory = QtWidgets.QFileDialog.getExistingDirectory(
            None, _("Select directory to save file"),
            self.app.last_export_directory, options)
        if directory == "":
            return
        if directory != self.app.last_export_directory:
            self.app.last_export_directory = directory
        filename = directory + "/" + filename
        if os.path.exists(filename):
            mb = QtWidgets.QMessageBox()
            mb.setWindowTitle(_("File exists"))
            mb.setText(_("Overwrite?"))
            mb.setStandardButtons(QtWidgets.QMessageBox.Yes
                                  | QtWidgets.QMessageBox.No)
            mb.setStyleSheet("* {font-size:" +
                             str(self.app.settings['fontsize']) + "pt} ")
            if mb.exec_() == QtWidgets.QMessageBox.No:
                return

        cols = self.ui.tableWidget.columnCount()
        rows = self.ui.tableWidget.rowCount()
        header = []
        for i in range(0, cols):
            header.append(self.ui.tableWidget.horizontalHeaderItem(i).text())
        with open(filename, mode='w') as f:
            writer = csv.writer(f,
                                delimiter=',',
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL)
            writer.writerow(header)
            for r in range(0, rows):
                data = []
                for c in range(0, cols):
                    # Table cell may be a None type
                    cell = ""
                    try:
                        cell = self.ui.tableWidget.item(r, c).text()
                    except AttributeError:
                        pass
                    data.append(cell)
                writer.writerow(data)
        logger.info("Report exported to " + filename)
        Message(self.app, _('Csv file Export'),
                filename + _(" exported")).exec_()
        self.parent_textEdit.append(
            _("Case attributes csv file exported to: ") + filename)
Esempio n. 11
0
    def read_csv_file(self):
        """ Read the data from the csv file.
         Fill Class variables self.fields, self.data """

        self.data = []
        with open(self.filepath, 'r', newline='') as f:
            delimiter_ = self.ui.lineEdit_delimiter.text()
            if delimiter_ == '':
                msg = _("A column delimiter has not been set.")
                Message(self.app, _("Warning"), msg, "warning").exec_()
                return False
            if delimiter_ in ('ta', 'tab'):
                delimiter_ = "\t"
            # The English text is in the GUI - do not translate with qt linguist
            quoting_ = csv.QUOTE_MINIMAL
            quote_type = self.ui.comboBox_quote.currentText()
            if quote_type == "NONE":
                quoting_ = csv.QUOTE_NONE
            if quote_type == "ALL":
                quoting_ = csv.QUOTE_ALL
            reader = csv.reader(f, delimiter=delimiter_, quoting=quoting_)
            try:
                for row in reader:
                    self.data.append(row)
            except csv.Error as e:
                logger.error(('file %s, line %d: %s' %
                              (self.filepath, reader.line_num, e)))
                self.parent_textEdit.append(
                    _("Row error: ") + str(reader.line_num) + "  " + str(e))
                self.fail_msg(_("Row error: ") + str(e))
                return False
        # Get field names and replace blacks with a placeholder
        self.fields = []
        for i, f in enumerate(self.data[0]):
            if f != '':
                self.fields.append(str(f))
            else:
                self.fields.append("Field_" + str(i))
        self.data = self.data[1:]
        return True
Esempio n. 12
0
 def startGame(self):
     msg = Message()
     msg.command = Command.START
     return self.encodeMessage(msg)
Esempio n. 13
0
    def run_SQL(self):
        """ Run the sql text and add the results to the results text edit. """

        # clear tableWidget and file data
        numRows = self.ui.tableWidget_results.rowCount()
        for row in range(0, numRows):
            self.ui.tableWidget_results.removeRow(0)
        self.ui.tableWidget_results.setHorizontalHeaderLabels([""])
        self.file_data = []
        self.ui.label.setText(_("Running query. Please wait."))
        QtWidgets.QApplication.processEvents()  # stops gui freeze
        self.sql = self.ui.textEdit_sql.toPlainText()
        cur = self.app.conn.cursor()
        self.sql = str(self.sql)
        QtWidgets.QApplication.processEvents()  # stops gui freeze
        try:
            time0 = datetime.now()
            cur.execute(self.sql)
            self.ui.label.setToolTip("")
            self.results = cur.fetchall()
            time1 = datetime.now()
            timediff = time1 - time0
            self.queryTime = "Time:" + str(timediff)
            self.ui.label.setToolTip(self.queryTime)
            self.ui.label.setText(str(len(self.results)) + _(" rows"))
            #extra messaging where rows will be zero
            if self.sql[0:12].upper() == "CREATE TABLE":
                self.ui.label.setText(_("Table created"))
                self.app.delete_backup = False
            if self.sql[0:12].upper() == "CREATE INDEX":
                self.ui.label.setText(_("Index created"))
                self.app.delete_backup = False
                self.app.conn.commit()
            if self.sql[0:6].upper() == "DELETE":
                self.ui.label.setText(str(cur.rowcount) + _(" rows deleted"))
                self.app.delete_backup = False
                self.app.conn.commit()
            if self.sql[0:6].upper() == "UPDATE":
                self.ui.label.setText(str(cur.rowcount) + _(" rows updated"))
                self.app.delete_backup = False
                self.app.conn.commit()
            colNames = []
            if cur.description is not None:
                colNames = list(map(lambda x: x[0],
                                    cur.description))  # gets column names
            self.ui.tableWidget_results.setColumnCount(len(colNames))
            self.ui.tableWidget_results.setHorizontalHeaderLabels(colNames)
            self.file_data.append(colNames)
            for row, row_results in enumerate(self.results):
                self.file_data.append(row_results)
                self.ui.tableWidget_results.insertRow(row)
                for col, value in enumerate(row_results):
                    if value is None:
                        value = ""
                    cell = QtWidgets.QTableWidgetItem(str(value))
                    cell.setFlags(QtCore.Qt.ItemIsSelectable
                                  | QtCore.Qt.ItemIsEnabled)
                    self.ui.tableWidget_results.setItem(row, col, cell)
            self.ui.tableWidget_results.resizeColumnsToContents()
            self.ui.tableWidget_results.resizeRowsToContents()
            sqlString = str(self.sql).upper()
            if sqlString.find("CREATE ") == 0 or sqlString.find(
                    "DROP ") == 0 or sqlString.find("ALTER ") == 0:
                self.get_schema_update_treeWidget()
        except (sqlite3.OperationalError, sqlite3.IntegrityError) as e:
            Message(self.app, _("Error"), str(e), "warning").exec_()
            self.ui.label.setText(_("SQL Error"))
            self.ui.label.setToolTip(str(e))
        self.results = None
        self.app.conn.commit()
Esempio n. 14
0
    def export_file(self):
        """ Load result set and export results to a delimited .csv file
        using \r\n as line separators. """

        cur = self.app.conn.cursor()
        sql = self.ui.textEdit_sql.toPlainText()
        try:
            cur.execute(sql)
        except Exception as e:
            Message(self.app, _("SQL error"), str(e), "warning").exec_()
            return

        results = cur.fetchall()
        col_names = []
        if cur.description is not None:
            col_names = list(map(lambda x: x[0],
                                 cur.description))  # gets column names
        # os.getenv('HOME') does not work on Windows
        tmp_name = self.app.last_export_directory + "/TEMP.csv"
        file_tuple = QtWidgets.QFileDialog.getSaveFileName(
            None, "Save text file", tmp_name)
        if file_tuple[0] == "":
            return
        filename = file_tuple[0]
        tmp = filename.split("/")[-1]
        directory = filename[:len(filename) - len(tmp)]
        if directory != self.app.last_export_directory:
            self.app.last_export_directory = directory
        if os.path.exists(filename):
            mb = QtWidgets.QMessageBox()
            mb.setWindowTitle(_("File exists"))
            mb.setText(_("Overwrite?"))
            mb.setStandardButtons(QtWidgets.QMessageBox.Yes
                                  | QtWidgets.QMessageBox.No)
            mb.setStyleSheet("* {font-size:" +
                             str(self.app.settings['fontsize']) + "pt} ")
            if mb.exec_() == QtWidgets.QMessageBox.No:
                return

        self.delimiter = str(self.ui.comboBox_delimiter.currentText())
        if self.delimiter == "tab":
            self.delimiter = "\t"
        ''' https://stackoverflow.com/questions/39422573/python-writing-weird-unicode-to-csv
        Using a byte order mark so that other software recognised UTF-8
        '''
        f = open(filename, 'w', encoding='utf-8-sig')
        # write the header row
        file_line = ""
        for item in col_names:
            file_line += item + self.delimiter
        file_line = file_line[:len(file_line) - 1]
        f.write(file_line + "\r\n")
        # write the data rows
        for r, row in enumerate(results):
            file_line = ""
            for item in row:
                if item is None:
                    file_line += self.delimiter
                else:
                    file_line += str(item) + self.delimiter
            file_line = file_line[:len(file_line) - 1]
            f.write(file_line + "\r\n")
        f.close()
        self.parent_textEdit.append(_("SQL Results exported to: ") + filename)
        self.parent_textEdit.append(_("Query:") + "\n" + sql)
        Message(self.app, _("Text file export"), filename,
                "information").exec_()
Esempio n. 15
0
    def import_cases_and_attributes(self):
        """ Import from a csv file with the cases and any attributes.
        The csv file must have a header row which details the attribute names.
        The csv file must be comma delimited. The first column must have the case ids.
        The attribute types are calculated from the data.
        """

        if self.cases != []:
            logger.warning(_("Cases have already been created."))
        filename = QtWidgets.QFileDialog.getOpenFileName(
            None, _('Select attributes file'), self.app.settings['directory'],
            "(*.csv)")[0]
        if filename == "":
            return
        if filename[-4:].lower() != ".csv":
            msg = filename + "\n" + _("is not a .csv file. File not imported")
            Message(self.app, _("Warning"), msg, "warning").exec_()
            self.parent_textEdit.append(msg)
            return
        values = []
        with open(filename, 'r', newline='') as f:
            reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            try:
                for row in reader:
                    values.append(row)
            except csv.Error as e:
                logger.warning(
                    ('file %s, line %d: %s' % (filename, reader.line_num, e)))
        if len(values) <= 1:
            logger.info(_("Cannot import from csv, only one row in file"))
            return
        now_date = str(
            datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S"))
        header = values[0]
        values = values[1:]
        # insert cases
        cur = self.app.conn.cursor()
        for v in values:
            item = {
                'name': v[0],
                'memo': "",
                'owner': self.app.settings['codername'],
                'date': now_date
            }
            try:
                cur.execute(
                    "insert into cases (name,memo,owner,date) values(?,?,?,?)",
                    (item['name'], item['memo'], item['owner'], item['date']))
                self.app.conn.commit()
                cur.execute("select last_insert_rowid()")
                item['caseid'] = cur.fetchone()[0]
                self.cases.append(item)
            except Exception as e:
                logger.error("item:" + str(item) + ", " + str(e))
        # determine attribute type
        attribute_value_type = ["character"] * len(header)
        for col, att_name in enumerate(header):
            numeric = True
            for val in values:
                try:
                    float(val[col])
                except ValueError:
                    numeric = False
            if numeric:
                attribute_value_type[col] = "numeric"
        # insert attribute types
        for col, att_name in enumerate(header):
            if col > 0:
                try:
                    cur.execute(
                        "insert into attribute_type (name,date,owner,memo, \
                    valueType, caseOrFile) values(?,?,?,?,?,?)",
                        (att_name, now_date, self.app.settings['codername'],
                         "", attribute_value_type[col], 'case'))
                    self.app.conn.commit()
                except Exception as e:
                    logger.error(_("attribute:") + att_name + ", " + str(e))
        # insert attributes
        sql = "select name, caseid from cases"
        cur.execute(sql)
        name_and_ids = cur.fetchall()
        for n_i in name_and_ids:
            for v in values:
                if n_i[0] == v[0]:
                    for col in range(1, len(v)):
                        sql = "insert into attribute (name, value, id, attr_type, date, owner) values (?,?,?,?,?,?)"
                        cur.execute(sql,
                                    (header[col], v[col], n_i[1], 'case',
                                     now_date, self.app.settings['codername']))
        self.app.conn.commit()
        self.load_cases_and_attributes()
        self.fill_tableWidget()
        msg = _("Cases and attributes imported from: ") + filename
        self.app.delete_backup = False
        self.parent_textEdit.append(msg)
        logger.info(msg)
Esempio n. 16
0
    def insert_data(self):
        """ Insert case, attributes, attribute values and qualitative text. """

        now_date = str(
            datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S"))
        cur = self.app.conn.cursor()
        name_and_caseids = []
        for i, c in enumerate(self.data):
            try:
                self.ui.label_msg.setText(_("Inserting cases: " + str(i)))
                cur.execute(
                    "insert into cases (name,memo,owner,date) values(?,?,?,?)",
                    (c[0], "", self.app.settings['codername'], now_date))
                self.app.conn.commit()
                cur.execute("select last_insert_rowid()")
                name_and_caseids.append([c[0], cur.fetchone()[0]])
                QtWidgets.QApplication.processEvents()
            except sqlite3.IntegrityError as e:
                self.fail_msg = str(e) + _(
                    " - Duplicate case names, either in the file, or duplicates with existing cases in the project"
                )
                logger.error(_("Survey not loaded: ") + self.fail_msg)
                mb = QtWidgets.QMessageBox()
                mb.setIcon(QtWidgets.QMessageBox.Warning)
                mb.setStyleSheet("* {font-size:" +
                                 str(self.app.settings['fontsize']) + "pt} ")
                mb.setWindowTitle(_('Survey not loaded'))
                mb.setText(self.fail_msg)
                mb.exec_()
                self.parent_textEdit.append(
                    _("Survey not loaded: ") + self.fail_msg)
                return
        # insert non-qualitative attribute types, except if they are already present
        sql = "select name from attribute_type where caseOrFile='case'"
        cur.execute(sql)
        result = cur.fetchall()
        existing_attr_names = []
        for r in result:
            existing_attr_names.append(r[0])
        sql = "insert into attribute_type (name,date,owner,memo, valueType, caseOrFile) values(?,?,?,?,?,?)"
        for col, name in enumerate(self.fields):
            if self.fields_type[
                    col] != "qualitative" and col > 0:  # col==0 is the case identifier
                if name not in existing_attr_names:
                    logger.debug(name +
                                 " is not in case attribute_types. Adding.")
                    cur.execute(
                        sql, (name, now_date, self.app.settings['codername'],
                              "", self.fields_type[col], 'case'))
        self.app.conn.commit()

        # Look for pre-existing attributes that are not in the survey and insert blank value rows if present
        survey_field_names = []
        for col, fld_name in enumerate(self.fields):
            if self.fields_type[col] != "qualitative" and col > 0:
                survey_field_names.append(fld_name)
        for name in existing_attr_names:
            if name not in survey_field_names:
                for name_id in name_and_caseids:
                    sql = "insert into attribute (name, value, id, attr_type, date, owner) values (?,'',?,?,?,?)"
                    cur.execute(sql, (name, name_id[1], 'case', now_date,
                                      self.app.settings['codername']))
        self.app.conn.commit()

        # insert non-qualitative values to each case using caseids
        sql = "insert into attribute (name, value, id, attr_type, date, owner) values (?,?,?,?,?,?)"
        for i, name_id in enumerate(name_and_caseids):
            self.ui.label_msg.setText(
                _("Inserting attributes to cases: ") + str(i))
            for val in self.data:
                if name_id[0] == val[0]:
                    for col in range(1, len(val)):
                        if self.fields_type[col] != "qualitative":
                            cur.execute(sql, (self.fields[col], val[col],
                                              name_id[1], 'case', now_date,
                                              self.app.settings['codername']))
            QtWidgets.QApplication.processEvents()
        self.app.conn.commit()

        # insert qualitative data into source table
        self.ui.label_msg.setText(_("Creating qualitative text file"))
        source_sql = "insert into source(name,fulltext,memo,owner,date, mediapath) values(?,?,?,?,?, Null)"
        for field in range(1, len(self.fields)):  # column 0 is for identifiers
            case_text_list = []
            if self.fields_type[field] == "qualitative":
                self.fields[field]
                # create one text file combining each row, prefix [case identifier] to each row.
                pos0 = 0
                pos1 = 0
                fulltext = ""
                for row in range(0, len(self.data)):
                    if self.data[row][field] != "":
                        fulltext += "[" + str(self.data[row][0]) + "] "
                        pos0 = len(fulltext) - 1
                        fulltext += str(self.data[row][field]) + "\n\n"
                        pos1 = len(fulltext) - 2
                        case_text = [
                            self.app.settings['codername'], now_date, "", pos0,
                            pos1, name_and_caseids[row][1]
                        ]
                        case_text_list.append(case_text)
                # add the current time to the file name to ensure uniqueness and to
                # prevent sqlite Integrity Error. Do not use now_date which contains colons
                now = str(datetime.datetime.now().astimezone().strftime(
                    "%Y-%m-%d %H-%M-%S"))
                cur.execute(source_sql,
                            (self.fields[field] + "_" + now, fulltext, "",
                             self.app.settings['codername'], now_date))
                self.app.conn.commit()
                cur.execute("select last_insert_rowid()")
                fid = cur.fetchone()[0]
                case_text_sql = "insert into case_text (owner, date, memo, pos0, pos1, caseid, fid) values(?,?,?,?,?,?,?)"
                for case_text in case_text_list:
                    case_text.append(fid)
                    cur.execute(case_text_sql, case_text)
                self.app.conn.commit()
        logger.info(_("Survey imported"))
        self.parent_textEdit.append(_("Survey imported."))
        Message(self.app, _("Survey imported"), _("Survey imported")).exec_()
        self.app.delete_backup = False
Esempio n. 17
0
 def endGame(self):
     msg = Message()
     msg.command = Command.YOUWIN
     return self.encodeMessage(msg)