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 |
|
|
class WMSCapabilities: |
23 |
|
|
__init__ (resource xor filename xor nothing) |
24 |
|
|
|
25 |
|
|
getErrorMsg() |
26 |
|
|
|
27 |
joey |
2147 |
fetchCapabilities(resource) |
28 |
joey |
2137 |
saveCapabilities(filename) |
29 |
|
|
loadCapabilities(filename) |
30 |
|
|
printCapabilities() |
31 |
joey |
2110 |
|
32 |
|
|
Requirements: |
33 |
|
|
- PyOGCLib <http://www.sourceforge.net/projects/pyogclib> |
34 |
|
|
|
35 |
|
|
Requires the ogclib installed regularily on the system or checked out |
36 |
|
|
next to the Thuban checkout. |
37 |
|
|
|
38 |
|
|
If this module is executed solitarily, it will fetch the capabilities |
39 |
|
|
of the frida service and store them in the local file |
40 |
|
|
frida_capabilities.xml for later processing. |
41 |
|
|
|
42 |
|
|
""" |
43 |
|
|
|
44 |
|
|
__version__ = "$Revision$" |
45 |
|
|
# $Source$ |
46 |
|
|
# $Id$ |
47 |
|
|
|
48 |
|
|
import os |
49 |
|
|
|
50 |
|
|
# ---------------------------------------------------------------------- |
51 |
|
|
# FIXME: Temporary code until PyOGCLib is a standard requirement |
52 |
|
|
|
53 |
|
|
from sys import path |
54 |
|
|
|
55 |
|
|
# Assume the PyOGCLib to be checked out next to the thuban main directory |
56 |
|
|
pyogclib = "../../../PyOGCLib" |
57 |
joey |
2137 |
if os.path.isdir(pyogclib) and os.path.isdir(pyogclib + "/ogclib"): |
58 |
|
|
path.insert(0, pyogclib) |
59 |
joey |
2110 |
|
60 |
|
|
# We use gettext, so we need to import it and hence need to adjust the |
61 |
|
|
# path again |
62 |
|
|
if __name__ == "__main__": |
63 |
joey |
2137 |
path.insert(0, "../../") |
64 |
joey |
2110 |
|
65 |
|
|
# ---------------------------------------------------------------------- |
66 |
|
|
|
67 |
|
|
from Thuban import _ |
68 |
|
|
|
69 |
|
|
from ogclib.WMSClient import WMSClient |
70 |
|
|
|
71 |
joey |
2137 |
class WMSCapabilities(WMSClient): |
72 |
joey |
2110 |
""" |
73 |
|
|
Thuban class to maintain capabilities. This class provides |
74 |
|
|
methods to fetch, save and load capabilities as well as methods to |
75 |
|
|
retrieve particular information from the fetched or loaded |
76 |
|
|
capabilities XML. |
77 |
|
|
|
78 |
|
|
If an error occured during processing an error text is assigned to |
79 |
|
|
self.errorMsg. If everything went fine, this variable is set to |
80 |
|
|
None. The current value can be retrieved by the getErrorMsg() |
81 |
|
|
method. |
82 |
|
|
""" |
83 |
|
|
|
84 |
|
|
capabilities = None |
85 |
|
|
errorMsg = None |
86 |
|
|
wmsVersion = None |
87 |
|
|
|
88 |
joey |
2137 |
def __init__(self, *parm): |
89 |
joey |
2110 |
""" |
90 |
|
|
Initialises Capabilities with one optional parameter |
91 |
|
|
|
92 |
joey |
2147 |
param can be either a URL or a filename: |
93 |
|
|
|
94 |
joey |
2110 |
filename -- load capabilities from file |
95 |
|
|
url -- fetch capabilities from network |
96 |
|
|
""" |
97 |
|
|
|
98 |
|
|
if parm and parm[0]: |
99 |
joey |
2137 |
if os.path.isfile(parm[0]): |
100 |
|
|
self.loadCapabilities(parm[0]) |
101 |
joey |
2110 |
else: |
102 |
joey |
2137 |
if parm[0].find("http://", 0) == 0: |
103 |
|
|
self.fetchCapabilities(parm[0]) |
104 |
joey |
2110 |
else: |
105 |
joey |
2136 |
self.errorMsg \ |
106 |
joey |
2147 |
= _("Resource '%s' is neither local file nor URL") \ |
107 |
|
|
% parm[0] |
108 |
joey |
2110 |
|
109 |
|
|
|
110 |
joey |
2137 |
def getErrorMsg(self): |
111 |
joey |
2110 |
return self.errorMsg |
112 |
|
|
|
113 |
|
|
|
114 |
joey |
2137 |
def fetchCapabilities(self, resource): |
115 |
joey |
2110 |
"""Fetches the WMS capabilities from an Internet resource""" |
116 |
|
|
|
117 |
joey |
2147 |
xml = None |
118 |
joey |
2110 |
self.wmsVersion = "1.1" |
119 |
joey |
2147 |
xml = self.getCapabilities(resource, self.wmsVersion) |
120 |
joey |
2110 |
if not self.capabilities: |
121 |
|
|
self.wmsVersion = "1.0" |
122 |
joey |
2147 |
xml = self.getCapabilities(resource, self.wmsVersion) |
123 |
joey |
2110 |
|
124 |
|
|
|
125 |
joey |
2137 |
def saveCapabilities(self, fname): |
126 |
joey |
2110 |
"""Save capabilities to local file""" |
127 |
|
|
|
128 |
|
|
if self.capabilities is None: |
129 |
|
|
self.errorMsg = _("No capabilities available") |
130 |
|
|
else: |
131 |
|
|
try: |
132 |
joey |
2137 |
out = open(fname, "w") |
133 |
|
|
out.write(self.capabilities) |
134 |
joey |
2110 |
out.close() |
135 |
joey |
2147 |
except IOError: |
136 |
joey |
2110 |
self.errorMsg = _("Can't open file '%s' for writing") % fname |
137 |
|
|
|
138 |
|
|
|
139 |
joey |
2137 |
def loadCapabilities(self, fname): |
140 |
joey |
2110 |
"""Load capabilities from a local file""" |
141 |
|
|
|
142 |
|
|
try: |
143 |
joey |
2137 |
input = open(fname, "r") |
144 |
joey |
2147 |
xml = input.read() |
145 |
joey |
2110 |
input.close() |
146 |
joey |
2147 |
except IOError: |
147 |
joey |
2110 |
self.errorMsg = _("Can't open file '%s' for reading") % fname |
148 |
|
|
|
149 |
|
|
|
150 |
joey |
2137 |
def printCapabilities(self): |
151 |
joey |
2110 |
"""Prints capabilities to stdout""" |
152 |
|
|
|
153 |
|
|
print self.capabilities |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
if __name__ == "__main__": |
158 |
joey |
2136 |
capabilities \ |
159 |
|
|
= WMSCapabilities("http://frida.intevation.org/cgi-bin/frida_wms?") |
160 |
joey |
2147 |
if capabilities.getErrorMsg() is None: |
161 |
|
|
capabilities.saveCapabilities("frida_capabilities.xml") |
162 |
joey |
2110 |
else: |
163 |
joey |
2147 |
print "Error: " + capabilities.getErrorMsg() |