[GRASS-SVN] r48190 - grass-addons/grass7/gui/wxpython/wx.wms
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Sep 7 13:30:51 EDT 2011
Author: marisn
Date: 2011-09-07 10:30:51 -0700 (Wed, 07 Sep 2011)
New Revision: 48190
Modified:
grass-addons/grass7/gui/wxpython/wx.wms/addserver.py
Log:
Strip provided WMS URL query parameters from service URL
Modified: grass-addons/grass7/gui/wxpython/wx.wms/addserver.py
===================================================================
--- grass-addons/grass7/gui/wxpython/wx.wms/addserver.py 2011-09-07 17:27:22 UTC (rev 48189)
+++ grass-addons/grass7/gui/wxpython/wx.wms/addserver.py 2011-09-07 17:30:51 UTC (rev 48190)
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
"""!
@package addserver.py
@@ -17,15 +18,15 @@
@author: Maris Nartiss (maris.nartiss gmail.com)
@author: Sudeep Singh Walia (Indian Institute of Technology, Kharagpur , sudeep495 at gmail.com)
"""
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-# generated by wxGlade 0.6.3 on Thu Jul 14 06:22:35 2011
import wx
import os
import popen2
import uuid
from urllib2 import Request, urlopen, URLError, HTTPError
+import urlparse
+import urllib
+
from grass.script import core as grass
from wx.lib.pubsub import Publisher
from BeautifulSoup import BeautifulSoup, Tag, NavigableString, BeautifulStoneSoup
@@ -82,12 +83,12 @@
#sudeep code starts
- if( not loadConfigFile(self)):
- grass.fatal_error('Config File Error, Unable to start application...')
+ if not loadConfigFile(self):
+ grass.fatal_error(_('Configuration file error. Unable to start application.'))
self.Close()
self.soup, open = initServerInfoBase('ServersList.xml')
- if(not open):
+ if not open:
return
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.__populate_URL_List(self.ServerList)
@@ -154,55 +155,111 @@
self.SetSizer(sizer_1)
self.Layout()
# end wxGlade
+
def valueExists(self,dict, newServerName):
try:
for key, value in dict.items():
- if(value.servername == newServerName):
+ if value.servername == newServerName:
return True
return False
except:
return False
+ def parse_WMS_URL(self, full_url):
+ '''!Strips any WMS parts from url for easy reuse.
+
+ Strips all WMS 1.1.1 and 1.3.0 parts from URL.
+
+ This could be moved to some separate WxS support procedure file.
+
+ @param full_url user provided WMS URL.
+
+ @return URL as dict suitable for storage or None if provided URL isn't a valid WMS URL.
+ '''
+ WMS_13_parameters = (
+ 'VERSION',
+ 'SERVICE',
+ 'REQUEST',
+ 'FORMAT',
+ 'UPDATESEQUENCE',
+ 'LAYERS',
+ 'STYLES',
+ 'CRS',
+ 'BBOX',
+ 'WIDTH',
+ 'HEIGHT',
+ 'TRANSPARENT',
+ 'BGCOLOR',
+ 'EXCEPTIONS',
+ 'TIME',
+ 'ELEVATION',
+ 'QUERY_LAYERS',
+ 'INFO_FORMAT',
+ 'FEATURE_COUNT',
+ 'I',
+ 'J')
+ url = urlparse.urlparse(full_url)
+ final_url = {'scheme': url.scheme, 'hostname': url.hostname, 'port': url.port, 'path': url.path, 'params': url.params, 'username': url.username, 'password': url.password, 'netloc': url.netloc}
+
+ # WMS supports only http or https
+ if final_url['scheme'] not in ('http','https'):
+ # URL without scheme is not a valid one. http is a good guess.
+ if url.scheme == '':
+ return self.parse_WMS_URL('http://'+full_url)
+ return None
+
+ # If URL lacks host, it's not valid
+ if not final_url['hostname']:
+ return None
+
+ # Leave any non-WMS query parts
+ query = urlparse.parse_qs(url.query)
+ for k in query.keys():
+ if k.upper() in WMS_13_parameters:
+ del query[k]
+ final_url['query'] = urllib.urlencode(query)
+ return urlparse.urlunparse((final_url['scheme'], final_url['netloc'], final_url['path'], final_url['params'], final_url['query'], None))
+
def OnSave(self, event): # wxGlade: ServerAdd.<event_handler>
newServerName = unicode(self.ServerNameText.GetValue())
newUrl = self.URLText.GetValue()
newUserName = self.UsernameText.GetValue()
newPassword = self.PasswordText.GetValue()
- if(not self.allFieldsValid(newServerName, newUrl, newUserName, newPassword)):
+ newUrl = self.parse_WMS_URL(newUrl.strip())
+ if not newUrl:
+ message = _("Provided WMS URL is not valid.")
+ self.ShowMessage(message, _('Error'))
+ grass.warning(message)
return
-
- if(self.selectedUid == None):
+
+ if self.selectedUid == None:
update = False
else:
update = True
-
+
serverData = ServerData()
- if(len(newUrl) != 0 and len(newServerName) != 0):
- if(self.selectedServer is not None):
- if(not self.selectedServer.servername == newServerName):
- if(self.valueExists(self.servers, newServerName)):
- message = "ServerName already exists. Please enter a different ServerName"
- self.ShowMessage(message, 'Warning')
+ if len(newServerName) != 0 :
+ if self.selectedServer is not None:
+ if not self.selectedServer.servername == newServerName:
+ if self.valueExists(self.servers, newServerName):
+ message = _("Server name already exists. Please enter a different server name")
+ self.ShowMessage(message, _('Warning'))
grass.warning(message)
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
return
- if(not newUrl.startswith('http://')):
- newUrl = 'http://'+newUrl
-
validUrl, message = self.validateUrl(newUrl)
- if(not validUrl):
- message = "Unable to validate URL ("+message+")"
- self.ShowMessage(message, 'Warning')
+ if not validUrl:
+ message = _("Unable to validate URL (%s)") % message
+ self.ShowMessage(message, _('Warning'))
grass.warning(message)
else:
- message = "Url validated ("+message+")"
+ message = _("Url validated (%s)") % message
-
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
serverData.servername = newServerName
@@ -210,16 +267,16 @@
serverData.username = newUserName
serverData.password = newPassword
- if(update):
- if(updateServerInfo(self.soup, self.soup.serverinfo, self.selectedUid, newServerName, newUrl, newUserName, newPassword)):
- if(not self.saveXMLData()):
+ if update:
+ if updateServerInfo(self.soup, self.soup.serverinfo, self.selectedUid, newServerName, newUrl, newUserName, newPassword):
+ if not self.saveXMLData():
oldInfo = self.servers[self.selectedUid]
- if(updateServerInfo(self.soup, self.soup.serverinfo, self.selectedUid, oldInfo.servername, oldInfo.url, oldInfo.username, oldInfo.password)):
- message = 'Unable to SaveXML, changes reverted back'
+ if updateServerInfo(self.soup, self.soup.serverinfo, self.selectedUid, oldInfo.servername, oldInfo.url, oldInfo.username, oldInfo.password):
+ message = _('Unable to save XML, changes reverted back')
grass.warning(message)
return
else:
- message = 'Unable to SaveXML, changes will be reverted back on restart'
+ message = _('Unable to save XML, changes will be reverted back on restart')
grass.warning(message)
return
message = 'update save successful'
@@ -234,27 +291,27 @@
msg = self.map_servernameTouid
Publisher().sendMessage(("update.map_servernameTouid"), msg)
- message = "Server Info Updated"
+ message = _("Server Info Updated")
grass.message(message)
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
self.setModified(False)
else:
- message = "Update not Successful"
+ message = _("Update not Successful")
self.ShowMessage(message, 'Warning')
grass.warning(message)
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
else:
uid = str(uuid.uuid4())
- if(addServerInfo(self.soup, self.soup.serverinfo, uid, newServerName, newUrl, newUserName, newPassword)):
- if(not self.saveXMLData()):
- if(removeServerInfo(self.soup, uid)):
- message = 'Unable to SaveXML, Info not saved'
+ if addServerInfo(self.soup, self.soup.serverinfo, uid, newServerName, newUrl, newUserName, newPassword):
+ if not self.saveXMLData():
+ if removeServerInfo(self.soup, uid):
+ message = _('Unable to save XML. Info not saved')
grass.warning(message)
return
else:
- message = 'Unable to SaveXML, changes will be reverted back on restart'
+ message = _('Unable to save XML. Changes will be reverted back on restart')
grass.warning(message)
return
message = 'soup save successfully'
@@ -282,21 +339,20 @@
self.selectedURL = newUrl
self.__update_URL_List()
else:
- message = "Please fill servername and url fields"
- self.ShowMessage(message, 'Warning')
+ message = _("Please fill in server name")
+ self.ShowMessage(message, _('Warning'))
grass.warning(message)
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
-
self.editOn = False
if(event):
event.Skip()
def OnRemove(self, event): # wxGlade: ServerAdd.<event_handler>
- if(self.selectedUid == None):
- message = "No Server selected....Remove Unsuccessful"
- self.ShowMessage(message, 'Warning')
+ if self.selectedUid == None:
+ message = _("No server selected. Remove unsuccessful")
+ self.ShowMessage(message, _('Warning'))
grass.warning(message)
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
@@ -446,18 +502,18 @@
try:
TMP = grass.tempfile()
if TMP is None:
- grass.fatal_error("Unable to create temporary files")
+ grass.fatal_error(_("Unable to create temporary files"))
f = open(TMP,'w')
f.write(xml)
f.close()
except:
- message = 'Unable to write in '+TMP+' file, Save not successful'
+ message = _('Unable to write in %s file. Save not successful') % TMP
grass.warning(message)
return False
- try:
- copyCommand = "cp "+TMP+" ServersList.xml"
+ try:
+ copyCommand = "cp "+TMP+" ServersList.xml" # FIXME
r, w, e = popen2.popen3(copyCommand)
- if(len(e.readlines())!=0):
+ if len(e.readlines()) != 0:
message = 'Copy failed, raising excpetion in savexml()'
grass.warning(message)
r.close()
@@ -473,7 +529,9 @@
return True
def allFieldsValid(self, newServerName, newUrl, newUserName, newPassword):
- if(newServerName.count(self.name_url_delimiter)>0):
+ # FIXME All texts should be encoded and accept all characters!
+ # Šitajam vajadzētu pazust, ja izmanto XML, kas automātiski kodē tekstus.
+ if newServerName.count(self.name_url_delimiter) > 0:
message = "Warning: UserName cannot consist of "+self.name_url_delimiter
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
@@ -481,7 +539,7 @@
grass.warning(message)
return False
- if(newUrl.count(self.name_url_delimiter)>0):
+ if newUrl.count(self.name_url_delimiter) > 0:
message = "Warning: URL cannot consist of "+self.name_url_delimiter
StatusBar_fields = ["Warning: URL Delimiter conflict. Edit config file"]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
@@ -518,8 +576,8 @@
return True
- def validateUrl(self,url):
- message = 'Validating Url...'
+ def validateUrl(self, url):
+ message = _('Validating URL...')
StatusBar_fields = [message]
self.StatusBar.SetStatusText(StatusBar_fields[0], 0)
req = Request(url)
@@ -541,7 +599,7 @@
message = 'urlopen exception, unable to fetch data for getcapabilities'
message = str(e)
- if(not message=='Successful'):
+ if not message == 'Successful':
return False, message
else:
return True, message
More information about the grass-commit
mailing list