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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2136 by joey, Wed Mar 24 20:05:33 2004 UTC revision 2152 by joey, Thu Apr 1 15:34:25 2004 UTC
# Line 19  Line 19 
19  """  """
20  Maintain WMS Capabilities  Maintain WMS Capabilities
21    
22    Inherits methods from WMSCapabilitiesParser
23    
24  class WMSCapabilities:  class WMSCapabilities:
25      __init__ (resource xor filename xor nothing)      __init__ (resource xor filename xor nothing)
26    
27      getErrorMsg()      getErrorMsg()
28    
29      fetchCapabilities (*resource)      fetchCapabilities(resource)
30      saveCapabilities (filename)      saveCapabilities(filename)
31      loadCapabilities (filename)      loadCapabilities(filename)
32      printCapabilities ()      printCapabilities()
33    
34        getVersion()
35    
36  Requirements:  Requirements:
37      - PyOGCLib <http://www.sourceforge.net/projects/pyogclib>      - PyOGCLib <http://www.sourceforge.net/projects/pyogclib>
# Line 54  from sys import path Line 58  from sys import path
58    
59  # Assume the PyOGCLib to be checked out next to the thuban main directory  # Assume the PyOGCLib to be checked out next to the thuban main directory
60  pyogclib = "../../../PyOGCLib"  pyogclib = "../../../PyOGCLib"
61  if os.path.isdir (pyogclib) and os.path.isdir (pyogclib + "/ogclib"):  if os.path.isdir(pyogclib) and os.path.isdir(pyogclib + "/ogclib"):
62      path.insert (0, pyogclib)      path.insert(0, pyogclib)
63    
64  # We use gettext, so we need to import it and hence need to adjust the  # We use gettext, so we need to import it and hence need to adjust the
65  # path again  # path again
66  if __name__ == "__main__":  if __name__ == "__main__":
67      path.insert (0, "../../")      path.insert(0, "../../")
68    
69  # ----------------------------------------------------------------------  # ----------------------------------------------------------------------
70    
71  from Thuban import _  from Thuban import _
72    
73  from ogclib.WMSClient import WMSClient  from ogclib.WMSClient import WMSClient
74    from parser import WMSCapabilitiesParser
75    
76  class WMSCapabilities (WMSClient):  class WMSCapabilities(WMSClient, WMSCapabilitiesParser):
77      """      """
78      Thuban class to maintain capabilities.  This class provides      Thuban class to maintain capabilities.  This class provides
79      methods to fetch, save and load capabilities as well as methods to      methods to fetch, save and load capabilities as well as methods to
# Line 85  class WMSCapabilities (WMSClient): Line 90  class WMSCapabilities (WMSClient):
90      errorMsg = None      errorMsg = None
91      wmsVersion = None      wmsVersion = None
92    
93      def __init__ (self, *parm):      def __init__(self, *parm):
94          """          """
95          Initialises Capabilities with one optional parameter          Initialises Capabilities with one optional parameter
96    
97            param can be either a URL or a filename:
98    
99          filename -- load capabilities from file          filename -- load capabilities from file
100          url -- fetch capabilities from network          url -- fetch capabilities from network
101          """          """
102    
103          if parm and parm[0]:          if parm and parm[0]:
104              if os.path.isfile (parm[0]):              if os.path.isfile(parm[0]):
105                  self.loadCapabilities (parm[0])                  self.loadCapabilities(parm[0])
106              else:              else:
107                  if parm[0].find ("http://",0) == 0:                  if parm[0].find("http://", 0) == 0:
108                      self.fetchCapabilities (parm[0])                      self.fetchCapabilities(parm[0])
109                  else:                  else:
110                      self.errorMsg \                      self.errorMsg \
111                          = _("Resource '%s' is neither local file nor URL")                          = _("Resource '%s' is neither local file nor URL") \
112                              % parm[0]                          % parm[0]
113    
114    
115      def getErrorMsg (self):      def getErrorMsg(self):
116          return self.errorMsg          return self.errorMsg
117    
118    
119      def fetchCapabilities (self, resource):      def fetchCapabilities(self, resource):
120          """Fetches the WMS capabilities from an Internet resource"""          """
121            Fetches the WMS capabilities from an Internet resource
122    
123            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          self.wmsVersion = "1.1"          self.wmsVersion = "1.1"
130          self.capabilities = self.getCapabilities(resource, self.wmsVersion)          self.capabilities = self.getCapabilities(resource, self.wmsVersion)
131          if not self.capabilities:          if not self.capabilities:
132              self.wmsVersion = "1.0"              self.wmsVersion = "1.0"
133              self.capabilities = self.getCapabilities(resource, self.wmsVersion)              self.capabilities = self.getCapabilities(resource, self.wmsVersion)
134                if not self.capabilities:
135                    self.wmsVersion = None
136                    self.errorMsg \
137                        = _("Resource '%s' "
138                            "does support neither WMS version 1.1 nor 1.0") \
139                            % resource
140    
141            if self.capabilities:
142                self.grok(self.capabilities)
143    
144      def saveCapabilities (self, fname):  
145        def saveCapabilities(self, fname):
146          """Save capabilities to local file"""          """Save capabilities to local file"""
147    
148          if self.capabilities is None:          if self.capabilities is None:
149              self.errorMsg = _("No capabilities available")              self.errorMsg = _("No capabilities available")
150          else:          else:
151              try:              try:
152                  out = open (fname, "w")                  out = open(fname, "w")
153                  out.write (self.capabilities)                  out.write(self.capabilities)
154                  out.close()                  out.close()
155              except:              except IOError:
156                  self.errorMsg = _("Can't open file '%s' for writing") % fname                  self.errorMsg = _("Can't open file '%s' for writing") % fname
157    
158    
159      def loadCapabilities (self, fname):      def loadCapabilities(self, fname):
160          """Load capabilities from a local file"""          """Load capabilities from a local file"""
161    
162          try:          try:
163              input = open (fname, "r")              input = open(fname, "r")
164              self.capabilities = input.read ()              self.capabilities = input.read()
165              input.close()              input.close()
166          except:              self.grok(self.capabilities)
167            except IOError:
168              self.errorMsg = _("Can't open file '%s' for reading") % fname              self.errorMsg = _("Can't open file '%s' for reading") % fname
169    
170    
171      def printCapabilities (self):      def printCapabilities(self):
172          """Prints capabilities to stdout"""          """Prints capabilities to stdout"""
173    
174          print self.capabilities          print self.capabilities
175    
176    
177        def getVersion(self):
178            """
179            Returns the WMS protocol version
180    
181            If no capabilities could be fetched, None is returned.
182            """
183            return self.wmsVersion
184    
185    
186  if __name__ == "__main__":  if __name__ == "__main__":
187      capabilities \      capabilities \
188          = WMSCapabilities("http://frida.intevation.org/cgi-bin/frida_wms?")          = WMSCapabilities("http://frida.intevation.org/cgi-bin/frida_wms?")
189      if capa.getErrorMsg() is None:      if capabilities.getErrorMsg() is None:
190          capa.saveCapabilities("frida_capabilities.xml")          capabilities.saveCapabilities("frida_capabilities.xml")
191      else:      else:
192          print "Error: " + capa.getErrorMsg()          print "Error: " + capabilities.getErrorMsg()

Legend:
Removed from v.2136  
changed lines
  Added in v.2152

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26