<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2013, GEM Foundation.
#
# OpenQuake 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.
#
# OpenQuake 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 General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.

import os
# Import the PyQt and QGIS libraries
from PyQt4 import QtCore, QtGui
from qgis.core import (QgsCoordinateTransform, QgsCoordinateReferenceSystem,
                       QgsVectorLayer, QgsMapLayerRegistry)
# Initialize Qt resources from file resources.py, used for side-effects
import resources_rc
# Import the code for the dialog
from download_exposure import ExposureDownloader, ExposureDownloadError
from platform_settings import PlatformSettingsDialog
from extentSelector import ExtentSelector
from qgis.core import QgsMessageLog


class Dock(QtGui.QDockWidget):
    def __init__(self, iface, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.extentSelector = ExtentSelector(self.canvas)
        self.extentSelector.tool.rectangleCreated.connect(self.polygonCreated)
        self.setupUi()

    def setupUi(self):
        self.resize(200, 200)
        self.setWindowTitle('Exposure')
        self.drawBtn = QtGui.QPushButton(self)
        self.drawBtn.setObjectName('drawBtn')
        self.drawBtn.setText('Draw')

        self.downloadBtn = QtGui.QPushButton(self)
        self.downloadBtn.setObjectName('downloadBtn')
        self.downloadBtn.setText('Download')
        self.downloadBtn.setEnabled(False)

        self.clearBtn = QtGui.QPushButton(self)
        self.clearBtn.setObjectName('clearBtn')
        self.clearBtn.setText('Clear')

        self.contents = QtGui.QGroupBox(self)
        self.layout = QtGui.QVBoxLayout(self.contents)
        self.layout.addWidget(self.drawBtn)
        self.layout.addWidget(self.downloadBtn)
        self.layout.addWidget(self.clearBtn)
        self.setWidget(self.contents)

        QtCore.QMetaObject.connectSlotsByName(self)

    def enableBusyCursor(self):
        """Set the hourglass enabled and stop listening for layer changes."""
        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

    def disableBusyCursor(self):
        """Disable the hourglass cursor and listen for layer changes."""
        QtGui.qApp.restoreOverrideCursor()

    @QtCore.pyqtSlot()
    def on_downloadBtn_clicked(self):
        qs = QtCore.QSettings()
        hostname = qs.value('download_exposure/hostname', '')
        username = qs.value('download_exposure/username', '')
        password = qs.value('download_exposure/password', '')
        if not (hostname and username and password):
            dialog = PlatformSettingsDialog(self.iface)
            if dialog.exec_():
                self.show_exposure(hostname, username, password)
        else:
            self.show_exposure(hostname, username, password)
        self.extentSelector.stop()

    @QtCore.pyqtSlot()
    def on_drawBtn_clicked(self):
        self.extentSelector.start()
        self.extentSelector.getExtent()

    @QtCore.pyqtSlot()
    def on_clearBtn_clicked(self):
        self.extentSelector.stop()
        self.downloadBtn.setEnabled(False)

    def polygonCreated(self):
        if self.selectedExtent() is not None:
            self.downloadBtn.setEnabled(True)

    def selectedExtent(self):
        return self.extentSelector.getExtent()

    def show_exposure(self, hostname, username, password):
        self.enableBusyCursor()
        try:
            crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
            xform = QgsCoordinateTransform(
                crs, QgsCoordinateReferenceSystem(4326))
            extent = xform.transform(self.selectedExtent())
            lon_min, lon_max = extent.xMinimum(), extent.xMaximum()
            lat_min, lat_max = extent.yMinimum(), extent.yMaximum()

            # download data
            ed = ExposureDownloader(hostname)
            ed.login(username, password)<commit_msg>Ask for credentials when the Draw button is clicked, if necessary<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2013, GEM Foundation.
#
# OpenQuake 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.
#
# OpenQuake 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 General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.

import os
# Import the PyQt and QGIS libraries
from PyQt4 import QtCore, QtGui
from qgis.core import (QgsCoordinateTransform, QgsCoordinateReferenceSystem,
                       QgsVectorLayer, QgsMapLayerRegistry)
# Initialize Qt resources from file resources.py, used for side-effects
import resources_rc
# Import the code for the dialog
from download_exposure import ExposureDownloader, ExposureDownloadError
from platform_settings import PlatformSettingsDialog
from extentSelector import ExtentSelector
from qgis.core import QgsMessageLog


class Dock(QtGui.QDockWidget):
    def __init__(self, iface, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.extentSelector = ExtentSelector(self.canvas)
        self.extentSelector.tool.rectangleCreated.connect(self.polygonCreated)
        self.setupUi()

    def get_credentials(self):
        qs = QtCore.QSettings()
        hostname = qs.value('download_exposure/hostname', '')
        username = qs.value('download_exposure/username', '')
        password = qs.value('download_exposure/password', '')
        if not (hostname and username and password):
            PlatformSettingsDialog(self.iface).exec_()
            hostname = qs.value('download_exposure/hostname', '')
            username = qs.value('download_exposure/username', '')
            password = qs.value('download_exposure/password', '')
        return hostname, username, password

    def setupUi(self):
        self.resize(200, 200)
        self.setWindowTitle('Exposure')
        self.drawBtn = QtGui.QPushButton(self)
        self.drawBtn.setObjectName('drawBtn')
        self.drawBtn.setText('Draw')

        self.downloadBtn = QtGui.QPushButton(self)
        self.downloadBtn.setObjectName('downloadBtn')
        self.downloadBtn.setText('Download')
        self.downloadBtn.setEnabled(False)

        self.clearBtn = QtGui.QPushButton(self)
        self.clearBtn.setObjectName('clearBtn')
        self.clearBtn.setText('Clear')

        self.contents = QtGui.QGroupBox(self)
        self.layout = QtGui.QVBoxLayout(self.contents)
        self.layout.addWidget(self.drawBtn)
        self.layout.addWidget(self.downloadBtn)
        self.layout.addWidget(self.clearBtn)
        self.setWidget(self.contents)

        QtCore.QMetaObject.connectSlotsByName(self)

    def enableBusyCursor(self):
        """Set the hourglass enabled and stop listening for layer changes."""
        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

    def disableBusyCursor(self):
        """Disable the hourglass cursor and listen for layer changes."""
        QtGui.qApp.restoreOverrideCursor()

    @QtCore.pyqtSlot()
    def on_downloadBtn_clicked(self):
        hostname, username, password = self.get_credentials()
        self.show_exposure(hostname, username, password)
        self.extentSelector.stop()
        self.downloadBtn.setEnabled(False)

    @QtCore.pyqtSlot()
    def on_drawBtn_clicked(self):
        self.get_credentials()  # opens PlatformSettingsDialog if no password
        self.extentSelector.start()
        self.extentSelector.getExtent()

    @QtCore.pyqtSlot()
    def on_clearBtn_clicked(self):
        self.extentSelector.stop()
        self.downloadBtn.setEnabled(False)

    def polygonCreated(self):
        if self.selectedExtent() is not None:
            self.downloadBtn.setEnabled(True)

    def selectedExtent(self):
        return self.extentSelector.getExtent()

    def show_exposure(self, hostname, username, password):
        selected_extent = self.selectedExtent()
        if selected_extent is None:
            QtGui.QMessageBox.warning(
                self, 'Exposure Download Error', 'No region selected')
            return
        self.enableBusyCursor()
        try:
            crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
            xform = QgsCoordinateTransform(
                crs, QgsCoordinateReferenceSystem(4326))
            extent = xform.transform(selected_extent)
            lon_min, lon_max = extent.xMinimum(), extent.xMaximum()
            lat_min, lat_max = extent.yMinimum(), extent.yMaximum()

            # download data
            ed = ExposureDownloader(hostname)
            ed.login(username, password)
{'fraction_non_alphanumeric': 0.0920353982300885, 'fraction_numerical': 0.0035398230088495575, 'mean_word_length': 3.0963752665245203, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 41, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2335718', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2632, 'n_words': 658}