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