def load_configuration(self): filename = QtWidgets.QFileDialog.getOpenFileName( self.gui, "Open configuration", "C:/", "Configuration files (*.txt)") filename = filename[0] # first field is filepath/name status, data = self._get(filename) if status == OK: #MANUAL DATA SEND for i in range(1, 9): # DATA field_name = str(i) + STR_TEMPLATE_DATA #x_data fields value = data[field_name] self.gui.data_field[i - 1].setText(value) #NOTE field_name = str(i) + STR_TEMPLATE_NOTE #x_NOTE fields value = data[field_name] self.gui.note_field[i - 1].setText(value) #DELAY field_name = str(i) + STR_TEMPLATE_DELAY #x_DELAY fields value = data[field_name] self.gui.delay_field[i - 1].setText(str(value)) #SEQUENCEs for i in range(1, 4): field_name = STR_TEMPLATE_SEQUENCE + str(i) values = data[field_name] commands = values.split( ',') # split commands for selected sequence commands = [int(x) for x in commands] for j in range(1, 6): #set all commands for chosen sequence self.gui.seq_selector[i - 1][j - 1].setCurrentIndex( commands[j - 1]) #SERIAL PORT SETUP self.gui.serial_setup_dialog.bytesize = data[ STR_TEMPLATE_SER_BYTESIZE] self.gui.ui.baud_rate_selector.setCurrentIndex( data[STR_TEMPLATE_SER_DATARATE]) self.gui.serial_setup_dialog.parity = data[STR_TEMPLATE_SER_PARITY] self.gui.ui.com_port_selector.setCurrentIndex( data[STR_TEMPLATE_SER_PORT]) self.gui.serial_setup_dialog.rts_cts = data[ STR_TEMPLATE_SER_RTSCTS] self.gui.serial_setup_dialog.stopbits = data[ STR_TEMPLATE_SER_STOPBITS] self.gui.serial_setup_dialog.xon_xoff = data[ STR_TEMPLATE_SER_XONXOFF] self.gui.serial_setup_dialog.set_gui_data() log.log_data(self.gui, "Load configuration: %s" % filename) else: log.log_data(self.gui, "Load configuration FAILED: %s" % filename)
def _get(self, filename): returndata = {} try: fd = open(filename, 'r') text = fd.read() fd.close() returndata = json.loads(text) status = OK except: log.log_data(self.gui, "COULD NOT LOAD FILE: %s" % filename) status = ERROR return (status, returndata)
def _put(self, data, filename): try: jsondata = json.dumps(data, indent=4, skipkeys=True, sort_keys=True) fd = open(filename, 'w') fd.write(jsondata) fd.close() status = OK except: log.log_data(self.gui, "COULD NOT WRITE TO %s" % filename) status = ERROR return status