def __sendToApi(data): """ Classify an image using the API. data needs to contain the bytes of a png formatted image. """ key = config.get('API', 'key') url = config.get('API', 'url') success = False resJson = None printedRateLimitingMessage = False with __lock: while not success: try: res = requests.post( url, data={'key': key}, files={'image': data} ) resJson = res.json() success = True except JSONDecodeError: if not printedRateLimitingMessage: logging.log('API: Rate limiting detected, retrying...') printedRateLimitingMessage = True time.sleep(1) except ValueError: if not printedRateLimitingMessage: logging.log('API: Rate limiting detected, retrying...') printedRateLimitingMessage = True time.sleep(1) except Exception as e: logging.log('API: Unexpected Error: ' + str(e) + ', retrying...') time.sleep(1) return resJson
def __initLayout(self): self.layout = QFormLayout(self) self.lineEditUrl = QLineEdit(config.get('API', 'url')) self.layout.addRow('URL', self.lineEditUrl) self.lineEditKey = QLineEdit(config.get('API', 'key')) self.layout.addRow('Key', self.lineEditKey) saveButton = QPushButton('Apply') saveButton.clicked.connect(self.save) self.layout.addRow(saveButton) self.setLayout(self.layout)
def initOptionsDialog(self): self.optionsWidget = QWidget() self.ui = Ui_Options() self.ui.setupUi(self.optionsWidget) self.ui.newPresetButton.clicked.connect(self.__openSavePresetDialog) self.ui.removePresetButton.clicked.connect(self.__removePreset) self.ui.setOptionsButton.clicked.connect(self.__setOptions) self.ui.presetComboBox.currentIndexChanged.connect(self.__selectPreset) presets = [ name.strip() for name in config.get("EAGenerator", "presets").split(",") ] selectedPreset = config.get("EAGenerator", "preset") for preset in zip(presets, range(len(presets))): self.ui.presetComboBox.addItem(preset[0]) if preset[0] == selectedPreset: self.ui.presetComboBox.setCurrentIndex(preset[1])
def initOptionsDialog(self): """ Instantiate (don't show yet) the options window. The values from the window are loaded from the configuration and used to set the values of the generator function. """ self.optionsWidget = QWidget() self.ui = Ui_Options() self.ui.setupUi(self.optionsWidget) self.ui.newPresetButton.clicked.connect(self.__openSavePresetDialog) self.ui.removePresetButton.clicked.connect(self.__removePreset) self.ui.setOptionsButton.clicked.connect(self.__setOptions) self.ui.presetComboBox.currentIndexChanged.connect(self.__selectPreset) presets = [ name.strip() for name in config.get("EAGenerator", "presets").split(",") ] selectedPreset = config.get("EAGenerator", "preset") for preset in zip(presets, range(len(presets))): self.ui.presetComboBox.addItem(preset[0]) if preset[0] == selectedPreset: self.ui.presetComboBox.setCurrentIndex(preset[1])
def __savePreset(self, alert, nameField): preset = nameField.text() presetsList = config.get("EAGenerator", "presets") presets = [name.strip() for name in presetsList.split(",")] if preset not in presets: # append preset's name to the comma separated list presetsList += "," + preset config.set("EAGenerator", "presets", presetsList) config.flush() # reflect changes in ui self.ui.presetComboBox.addItem(preset) self.ui.presetComboBox.setCurrentIndex( self.ui.presetComboBox.count() - 1) self.__savePresetFromUi(preset) alert.close()
def __removePreset(self): preset = self.ui.presetComboBox.currentText() section = "EAGenerator_Preset:" + preset presets = [ name.strip() for name in config.get("EAGenerator", "presets").split(",") ] if preset in presets and len(presets) > 1: delIndex = presets.index(preset) del presets[delIndex] config.set("EAGenerator", "presets", ",".join(presets)) # either select previous preset or the first one newIndex = max(0, delIndex - 1) config.set("EAGenerator", "preset", presets[newIndex]) config.removeSection(section) config.flush() # reflect changes in ui self.ui.presetComboBox.removeItem( self.ui.presetComboBox.currentIndex()) self.ui.presetComboBox.setCurrentIndex(newIndex)
def __loadPresetIntoUi(self, preset): section = "EAGenerator_Preset:" + preset self.ui.colorMutationRateSpinBox.setValue( config.get(section, "colorMutationRate", int)) self.ui.colorsFromRSpinBox.setValue( config.get(section, "colorsRangeFromR", int)) self.ui.colorsToRSpinBox.setValue( config.get(section, "colorsRangeToR", int)) self.ui.colorsFromGSpinBox.setValue( config.get(section, "colorsRangeFromG", int)) self.ui.colorsToGSpinBox.setValue( config.get(section, "colorsRangeToG", int)) self.ui.colorsFromBSpinBox.setValue( config.get(section, "colorsRangeFromB", int)) self.ui.colorsToBSpinBox.setValue( config.get(section, "colorsRangeToB", int)) self.ui.contrastFromSpinBox.setValue( config.get(section, "contrastRangeFrom", int)) self.ui.contrastToSpinBox.setValue( config.get(section, "contrastRangeTo", int)) self.ui.shapeMutationRateSpinBox.setValue( config.get(section, "shapeMutationRate", int)) self.ui.shapePolygonPointCountSpinBox.setValue( config.get(section, "shapePolygonPointCount", int)) self.ui.initialPopulationSizeSpinBox.setValue( config.get(section, "initialPopulationSize", int)) self.ui.targetConfidenceSpinBox.setValue( config.get(section, "targetConfidence", int)) self.ui.targetPopulationSizeSpinBox.setValue( config.get(section, "targetPopulationSize", int))