/[thuban]/branches/WIP-pyshapelib-bramz/Extensions/wms/capabilities.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Extensions/wms/capabilities.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2152 - (hide annotations)
Thu Apr 1 15:34:25 2004 UTC (20 years, 11 months ago) by joey
Original Path: trunk/thuban/Extensions/wms/capabilities.py
File MIME type: text/x-python
File size: 5862 byte(s)
No need to use the concatenation operator here, and this version will
also be understood by gettext which is important for translations
later.

1 joey 2110 # Copyright (c) 2004 by Intevation GmbH
2     # Authors:
3     # Martin Schulze <[email protected]>
4     #
5     # This program is free software; you can redistribute it and/or modify
6     # it under the terms of the GNU General Public License as published by
7     # the Free Software Foundation; either version 2 of the License, or
8     # (at your option) any later version.
9     #
10     # This program is distributed in the hope that it will be useful,
11     # but WITHOUT ANY WARRANTY; without even the implied warranty of
12     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     # GNU General Public License for more details.
14     #
15     # You should have received a copy of the GNU General Public License
16     # along with this program; if not, write to the Free Software
17     # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18    
19     """
20     Maintain WMS Capabilities
21    
22 joey 2149 Inherits methods from WMSCapabilitiesParser
23    
24 joey 2110 class WMSCapabilities:
25     __init__ (resource xor filename xor nothing)
26    
27     getErrorMsg()
28    
29 joey 2147 fetchCapabilities(resource)
30 joey 2137 saveCapabilities(filename)
31     loadCapabilities(filename)
32     printCapabilities()
33 joey 2110
34 joey 2150 getVersion()
35    
36 joey 2110 Requirements:
37     - PyOGCLib <http://www.sourceforge.net/projects/pyogclib>
38    
39     Requires the ogclib installed regularily on the system or checked out
40     next to the Thuban checkout.
41    
42     If this module is executed solitarily, it will fetch the capabilities
43     of the frida service and store them in the local file
44     frida_capabilities.xml for later processing.
45    
46     """
47    
48     __version__ = "$Revision$"
49     # $Source$
50     # $Id$
51    
52     import os
53    
54     # ----------------------------------------------------------------------
55     # FIXME: Temporary code until PyOGCLib is a standard requirement
56    
57     from sys import path
58    
59     # Assume the PyOGCLib to be checked out next to the thuban main directory
60     pyogclib = "../../../PyOGCLib"
61 joey 2137 if os.path.isdir(pyogclib) and os.path.isdir(pyogclib + "/ogclib"):
62     path.insert(0, pyogclib)
63 joey 2110
64     # We use gettext, so we need to import it and hence need to adjust the
65     # path again
66     if __name__ == "__main__":
67 joey 2137 path.insert(0, "../../")
68 joey 2110
69     # ----------------------------------------------------------------------
70    
71     from Thuban import _
72    
73     from ogclib.WMSClient import WMSClient
74 joey 2149 from parser import WMSCapabilitiesParser
75 joey 2110
76 joey 2149 class WMSCapabilities(WMSClient, WMSCapabilitiesParser):
77 joey 2110 """
78     Thuban class to maintain capabilities. This class provides
79     methods to fetch, save and load capabilities as well as methods to
80     retrieve particular information from the fetched or loaded
81     capabilities XML.
82    
83     If an error occured during processing an error text is assigned to
84     self.errorMsg. If everything went fine, this variable is set to
85     None. The current value can be retrieved by the getErrorMsg()
86     method.
87     """
88    
89     capabilities = None
90     errorMsg = None
91     wmsVersion = None
92    
93 joey 2137 def __init__(self, *parm):
94 joey 2110 """
95     Initialises Capabilities with one optional parameter
96    
97 joey 2147 param can be either a URL or a filename:
98    
99 joey 2110 filename -- load capabilities from file
100     url -- fetch capabilities from network
101     """
102    
103     if parm and parm[0]:
104 joey 2137 if os.path.isfile(parm[0]):
105     self.loadCapabilities(parm[0])
106 joey 2110 else:
107 joey 2137 if parm[0].find("http://", 0) == 0:
108     self.fetchCapabilities(parm[0])
109 joey 2110 else:
110 joey 2136 self.errorMsg \
111 joey 2147 = _("Resource '%s' is neither local file nor URL") \
112     % parm[0]
113 joey 2110
114    
115 joey 2137 def getErrorMsg(self):
116 joey 2110 return self.errorMsg
117    
118    
119 joey 2137 def fetchCapabilities(self, resource):
120 joey 2151 """
121     Fetches the WMS capabilities from an Internet resource
122 joey 2110
123 joey 2151 WMS Protocol version 1.1 is tried first, then 1.0. The
124     protocol version used can be queried by the getVersion()
125     method for later use. If both tries fail, errorMsg will be
126     set accordingly, which can be fetched with getErrorMsg().
127     """
128    
129 joey 2110 self.wmsVersion = "1.1"
130 joey 2148 self.capabilities = self.getCapabilities(resource, self.wmsVersion)
131 joey 2110 if not self.capabilities:
132     self.wmsVersion = "1.0"
133 joey 2148 self.capabilities = self.getCapabilities(resource, self.wmsVersion)
134 joey 2150 if not self.capabilities:
135     self.wmsVersion = None
136 joey 2151 self.errorMsg \
137     = _("Resource '%s' "
138 joey 2152 "does support neither WMS version 1.1 nor 1.0") \
139 joey 2151 % resource
140 joey 2110
141 joey 2149 if self.capabilities:
142     self.grok(self.capabilities)
143 joey 2110
144 joey 2149
145 joey 2137 def saveCapabilities(self, fname):
146 joey 2110 """Save capabilities to local file"""
147    
148     if self.capabilities is None:
149     self.errorMsg = _("No capabilities available")
150     else:
151     try:
152 joey 2137 out = open(fname, "w")
153     out.write(self.capabilities)
154 joey 2110 out.close()
155 joey 2147 except IOError:
156 joey 2110 self.errorMsg = _("Can't open file '%s' for writing") % fname
157    
158    
159 joey 2137 def loadCapabilities(self, fname):
160 joey 2110 """Load capabilities from a local file"""
161    
162     try:
163 joey 2137 input = open(fname, "r")
164 joey 2148 self.capabilities = input.read()
165 joey 2110 input.close()
166 joey 2149 self.grok(self.capabilities)
167 joey 2147 except IOError:
168 joey 2110 self.errorMsg = _("Can't open file '%s' for reading") % fname
169    
170    
171 joey 2137 def printCapabilities(self):
172 joey 2110 """Prints capabilities to stdout"""
173    
174     print self.capabilities
175    
176    
177 joey 2150 def getVersion(self):
178     """
179     Returns the WMS protocol version
180 joey 2110
181 joey 2150 If no capabilities could be fetched, None is returned.
182     """
183     return self.wmsVersion
184    
185    
186 joey 2110 if __name__ == "__main__":
187 joey 2136 capabilities \
188     = WMSCapabilities("http://frida.intevation.org/cgi-bin/frida_wms?")
189 joey 2147 if capabilities.getErrorMsg() is None:
190     capabilities.saveCapabilities("frida_capabilities.xml")
191 joey 2110 else:
192 joey 2147 print "Error: " + capabilities.getErrorMsg()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26