Example #1
0
    def credentials_uploader(self):
        """Get file selected by the user and loads API credentials into plugin.
        If the selected is compliant, credentials are loaded from then it's
        moved inside plugin/_auth subfolder.
        """
        # test file structure
        try:
            selected_file = path.normpath(self.ui_auth_form.btn_browse_credentials.filePath())
            api_credentials = plg_tools.credentials_loader(selected_file)
        except Exception as e:
            logger.error(e)
            QMessageBox.critical(self.ui_auth_form,
                                 self.tr("Alert", "IsogeoPlgApiMngr"),
                                 self.tr("The selected credentials file is not correct.",
                                         "IsogeoPlgApiMngr"))
        # move credentials file into the plugin file structure
        if path.isfile(path.join(self.auth_folder, "client_secrets.json")):
            rename(path.join(self.auth_folder, "client_secrets.json"),
                   path.join(self.auth_folder, "old_client_secrets_{}.json"
                                               .format(int(time.time())))
                   )
            logger.debug("client_secrets.json already existed. "
                         "Previous file has been renamed.")
        else:
            pass
        rename(selected_file,
               path.join(self.auth_folder, "client_secrets.json"))
        logger.debug("Selected credentials file has been moved into plugin"
                     "_auth subfolder")

        # set form
        self.ui_auth_form.ent_app_id.setText(api_credentials.get("client_id"))
        self.ui_auth_form.ent_app_secret.setText(api_credentials.get("client_secret"))
        self.ui_auth_form.lbl_api_url_value.setText(api_credentials.get("uri_auth"))

        # update class attributes from file
        self.credentials_update(credentials_source="oAuth2_file")

        # store into QSettings if existing
        self.credentials_storer(store_location="QSettings")
Example #2
0
 def exceptionOccurred(self, message):
     QMessageBox.critical(self, self.tr("OQ-Consolidate: Error"), message)
     self.stopProcessing()
     self.restoreGui()
     return
Example #3
0
 def processError(self, message):
     QMessageBox.critical(self, self.tr("OQ-Consolidate: Error"), message)
     self.stopProcessing()
     self.restoreGui()
     return
Example #4
0
    def accept(self):
        project_name = self.project_name_le.text()
        if project_name.endswith('.qgs'):
            project_name = project_name[:-4]
        if not project_name:
            QMessageBox.critical(self, self.tr("OQ-Consolidate: Error"),
                                 self.tr("Please specify the project name"))
            return

        outputDir = self.leOutputDir.text()
        if not outputDir:
            QMessageBox.critical(
                self, self.tr("OQ-Consolidate: Error"),
                self.tr("Please specify the output directory."))
            return
        outputDir = os.path.join(outputDir, get_valid_filename(project_name))

        # create main directory if not exists
        d = QDir(outputDir)
        if not d.exists():
            if not d.mkpath("."):
                QMessageBox.critical(
                    self, self.tr("OQ-Consolidate: Error"),
                    self.tr("Can't create directory to store the project."))
                return

        # create directory for layers if not exists
        if d.exists("layers"):
            res = QMessageBox.question(
                self, self.tr("Directory exists"),
                self.tr("Output directory already contains 'layers'"
                        " subdirectory. Maybe this directory was used to"
                        " consolidate another project. Continue?"),
                QMessageBox.Yes | QMessageBox.No)
            if res == QMessageBox.No:
                return
        else:
            if not d.mkdir("layers"):
                QMessageBox.critical(
                    self, self.tr("OQ-Consolidate: Error"),
                    self.tr("Can't create directory for layers."))
                return

        # copy project file
        projectFile = QgsProject.instance().fileName()
        if projectFile:
            f = QFile(projectFile)
            newProjectFile = os.path.join(outputDir, '%s.qgs' % project_name)
            f.copy(newProjectFile)
        else:
            newProjectFile = os.path.join(outputDir, '%s.qgs' % project_name)
            f = QFileInfo(newProjectFile)
            p = QgsProject.instance()
            p.write(f)

        # start consolidate thread that does all real work
        self.workThread = consolidatethread.ConsolidateThread(
            self.iface, outputDir, newProjectFile,
            self.checkBoxZip.isChecked())
        self.workThread.rangeChanged.connect(self.setProgressRange)
        self.workThread.updateProgress.connect(self.updateProgress)
        self.workThread.processFinished.connect(self.processFinished)
        self.workThread.processInterrupted.connect(self.processInterrupted)
        self.workThread.processError.connect(self.processError)
        self.workThread.exceptionOccurred.connect(self.exceptionOccurred)

        self.btnClose.setText(self.tr("Cancel"))
        self.btnOk.setEnabled(False)
        self.buttonBox.rejected.disconnect(self.reject)
        self.btnClose.clicked.connect(self.stopProcessing)

        self.workThread.start()
    def accept(self):

        # Check the user entered a folder path
        inputFolder = self.inputLineEdit.text()
        if len(inputFolder) == 0:
            QMessageBox.critical(None, 'No Input Folder Selected',
                                 'Please select an input folder.')
            return
        if not os.path.isdir(inputFolder):
            QMessageBox.critical(
                None, 'Invalid Input Folder',
                '%s is not a valid folder path.' % inputFolder)
            return

        # Check a connection is selected
        if self.postgisConnectionComboBox.count() == 0:
            QMessageBox.critical(
                None, 'No PostGIS Connection Selected',
                'No PostGIS connection was selected. Please configure a connection through Layer > Add PostGIS Layers...'
            )
            return
        self.extractPgConnectionDetails()

        # Ensure destination schema exists - prompt to create it
        self.schema_name = str(self.destSchema.text())
        if len(self.schema_name) == 0:
            QMessageBox.critical(None, 'No Schema Specified',
                                 'Please specify a destination schema.')
            return
        if self.schema_name[0] in string.digits:
            QMessageBox.critical(None, 'Unsupported Schema Name',
                                 'Schema names must not start with a number.')
            return
        for ch in self.schema_name:
            if not ch in string.ascii_lowercase and not ch in string.digits and not ch == '_':
                QMessageBox.critical(
                    None, 'Unsupported Schema Name',
                    'Schema names must currently consist of lower case characters, numbers and underscores.'
                )
                return
        if len(self.schema_name) == 0:
            errMsg = 'No destination schema was specified. Do you wish to import into the public schema?'
            reply = QMessageBox.question(self.parent, 'No Schema Specified',
                                         errMsg,
                                         QMessageBox.Yes | QMessageBox.No,
                                         QMessageBox.No)
            if reply == QMessageBox.No:
                return
            self.schema_name = 'public'

        try:
            cur = get_db_cur(self.dbDetails,
                             self.postgisConnectionComboBox.currentText())
        except:
            QMessageBox.critical(
                None, 'Failed to Connect to Database',
                'Failed to make a connection to the database, detailed error was:\n\n%s'
                % traceback.format_exc())
            return

        for schemaName in [self.schema_name, self.schema_name + '_tmp']:
            try:
                qDic = {'schema_name': schemaName}
                cur.execute(
                    """SELECT schema_name FROM information_schema.schemata WHERE schema_name = %(schema_name)s;""",
                    qDic)
            except:
                QMessageBox.critical(
                    None, 'Failed to Query Schemas',
                    'Failed to determine whether destination already exists, detailed error was:\n\n%s'
                    % traceback.format_exc())
                return
            if cur.rowcount < 1:
                # The schema does not already exist - create it
                try:
                    if not create_schema(cur, schemaName):
                        raise Exception()
                except:
                    QMessageBox.critical(
                        None, 'Failed to Create Schema',
                        'Failed to create schema, detailed error was:\n\n%s' %
                        traceback.format_exc())
                    return

        gfsFilePath = self.buildGfs()

        # If mode is create or replace, issue a warning
        if self.modeComboBox.currentText() == 'Create or Replace':
            qDic['schema_name'] = self.schema_name
            # See if the table exists
            existingTables = []
            for dTable in self.destTables:
                try:
                    qDic['table_name'] = dTable
                    cur.execute(
                        """ SELECT table_name FROM information_schema.tables
                                    WHERE table_schema = %(schema_name)s AND 
                                    table_name = %(table_name)s;""", qDic)
                except:
                    QMessageBox.critical(
                        None, 'Failed to Query Tables',
                        'Failed to determine whether destination table already exists, detailed error was:\n\n%s'
                        % traceback.format_exc())
                    return
                if cur.rowcount > 0:
                    existingTables.append(dTable)
            if len(existingTables) > 0:
                errMsg = "The following tables will be permanently overwritten:\n\n"
                for exTab in existingTables:
                    errMsg += '%s.%s' % (self.schema_name, exTab) + '\n'
                errMsg += "\nDo you wish to proceed?"
                reply = QMessageBox.question(self.parent, 'Overwriting Tables',
                                             errMsg,
                                             QMessageBox.Yes | QMessageBox.No,
                                             QMessageBox.No)
                if reply == QMessageBox.No:
                    return

        inputFiles = get_input_files(str(self.inputLineEdit.text()))
        # Insert a 'pioneer' file which contains a feature of each table type
        inputFiles.insert(
            0, get_pioneer_file(str(self.datasetComboBox.currentText())))

        # Ensure the user has selected some files
        if len(inputFiles) == 0:
            QMessageBox.critical(
                None, 'No Input Files Selected',
                'Failed to find any GML files under the selected folder.')
            return
        """ Add the jobs to the import manager """

        if len(self.dbDetails['user']) == 0:
            user, password = credentials_user_pass(
                self.postgisConnectionComboBox.currentText())
        else:
            user, password = self.dbDetails['user'], self.dbDetails['password']
        pgSource = 'PG:dbname=\'%s\' host=\'%s\' port=\'%d\' active_schema=%s user=\'%s\' password=\'%s\'' % \
            (self.dbDetails['database'], self.dbDetails['host'], self.dbDetails['port'], self.schema_name + '_tmp', user, password)
        # Note we are loading into a temporary schema

        self.im.reset()

        for arg in build_args(inputFiles, gfsFilePath, pgSource,
                              self.ignoreFidCheckBox.checkState()):
            self.im.add(arg)

        try:
            self.im.start(self.simultaneousJobsSpinBox.value())
        except:
            QMessageBox.critical(
                None, 'Failed to Start Process',
                'Failed to start the import process - please ensure you have ogr2ogr installed.'
            )
            return
        self.freezeUi()
        self.progressBar.setEnabled(True)
        self.progressBar.setValue(0)
        self.statusLabel.setText('Loading - grab a snack..')