Skip to content

Commit

Permalink
fix #30
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik committed Apr 4, 2017
1 parent 21acaae commit 3838f24
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ OSTranslatorII/.idea/
*.pyc
.idea/
*.zip
*.qml
OSTranslatorII/downloads/*
!OSTranslatorII/downloads/.gitkeep
*.qml
Empty file removed OSTranslatorII/downloads/.gitkeep
Empty file.
4 changes: 2 additions & 2 deletions OSTranslatorII/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ qgisMinimumVersion=2.0
qgismaximumversion=2.99
description=A plugin for loading Ordnance Survey Products.
about=OS Translator II makes loading GML-based datasets into PostGIS easy. It also performs a number of post-processing tasks on the data such as adding additional fields for styling and downloading 3rd-party stylesheets.
version=1.4.0
version=1.4.1
author=Peter Wells for Lutra Consulting
[email protected]

Expand All @@ -15,7 +15,7 @@ [email protected]
# Optional items:

# Uncomment the following line and add your changelog:
changelog=1.4.0 - New features and bug fixes:
changelog=1.4.1 - New features and bug fixes:
- Support for schema 9 (issue #25)
- Support for downloads behing proxy (issue #16)
<p>1.3.0 - New features:
Expand Down
6 changes: 5 additions & 1 deletion OSTranslatorII/styler.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def addFields(self, table):
self.cur.execute(sqlQuery, {})

self.cleanUp(table)
utils.delete(sqlPath)

def applyDefaultStyle(self, table):
"""
Expand Down Expand Up @@ -126,6 +127,8 @@ def applyDefaultStyle(self, table):
raise Exception('Failed to load layer %s for applying default style.' % table)

success, message = pgLayer.importNamedStyle(domDoc)
utils.delete(qmlPath)

if not success:
raise Exception('Failed to load layer style: %s\n\nThis can happen when using free wifi connections requiring registration.' % message)
try:
Expand Down Expand Up @@ -163,7 +166,8 @@ def applyDefaultStyle(self, table):
raise Exception('Error: %s' % failedDbStyleSaveError)
except psycopg2.ProgrammingError:
raise Exception('Error: %s' % failedDbStyleSaveError)



return True

def getLayerUri(self, table, schemaType='destination'):
Expand Down
38 changes: 15 additions & 23 deletions OSTranslatorII/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,32 @@
from PyQt4.QtCore import QSettings
from PyQt4.QtNetwork import QNetworkRequest
from PyQt4.QtCore import QUrl, QEventLoop

import tempfile

def OSII_icon_path():
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "images", "icon.png")


def download(packageUrl, destinationFileName):
destFolder = os.path.join(os.path.dirname(__file__), "downloads")
dest = os.path.join(destFolder, destinationFileName)
handle = tempfile.NamedTemporaryFile(delete=False, suffix=destinationFileName)
name = handle.name

try:
from qgis.core import QgsNetworkAccessManager
_download_qgis(packageUrl, dest)
_download_qgis(packageUrl, handle)

except ImportError:
# in case we are using cli and qgis is not installed
_download_urllib2(packageUrl, dest)
_download_urllib2(packageUrl, handle)

return dest
handle.close()
return name

def delete(fileName):
if os.path.isfile(fileName):
os.unlink(fileName)

def _download_qgis(packageUrl, dest):
def _download_qgis(packageUrl, handle):
from qgis.core import QgsNetworkAccessManager

request = QNetworkRequest(QUrl(packageUrl))
Expand All @@ -46,20 +51,12 @@ def _download_qgis(packageUrl, dest):
evloop.exec_(QEventLoop.ExcludeUserInputEvents)
content_type = reply.rawHeader('Content-Type')
if bytearray(content_type) == bytearray('text/plain; charset=utf-8'):
# remove the old file
if os.path.isfile(dest):
os.unlink(dest)

destinationFile = open(dest, 'wb')
destinationFile.write(bytearray(reply.readAll()))
destinationFile.close()
handle.write(bytearray(reply.readAll()))
else:
ret_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
raise Exception('Failed to download %s\n\nThe HTTP status code was %d.\n\nPlease check your network settings' % (packageUrl, ret_code))

return dest

def _download_urllib2(url, destinationFileName):
def _download_urllib2(url, handle):
s = QSettings()
try:
useProxy = s.value("proxy/proxyEnabled", False).toBool()
Expand Down Expand Up @@ -91,12 +88,7 @@ def _download_urllib2(url, destinationFileName):
raise Exception('Failed to download %s\n\nThe reason was %s.' % (
url, e.reason) + generalDownloadFailureMessage)

if os.path.isfile(destinationFileName):
os.unlink(destinationFileName)
destinationFile = open(destinationFileName, 'wb')
destinationFile.write(conn.read())
destinationFile.close()
return destinationFileName
handle.write(conn.read())


def get_OSMM_schema_ver(s):
Expand Down

0 comments on commit 3838f24

Please sign in to comment.