source: TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/Wps/capabilities.py @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/Wps/capabilities.py@2194
Revision 2194, 11.6 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

Line 
1#!/usr/bin/python
2"""
3This module generates XML file with GetCapabilities response of WPS
4"""
5# Author:       Jachym Cepicky
6#               http://les-ejk.cz
7# Lince:
8#
9# Web Processing Service implementation
10# Copyright (C) 2006 Jachym Cepicky
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25
26# TODO: Wps/ows content -> attributes/elements
27# TODO: Wps/ows mandatory -> oblig 'mandatory' x 'optional' x 'conditional'
28
29from ogc import getcapabilities
30from xml.dom.minidom import Document
31import append
32
33class Capabilities:
34    def __init__(self,serverSettings,processes):
35        """
36        Inicialize and create 4 sections of GetCapabilities XML output:
37
38            * ServiceIdentification
39            * ServiceProvider
40            * OperationsMetadata
41            * ProcessOfferings
42       
43        TODO: ServiceProvider contact is somehow very complicated :-/
44
45        Inputs:
46            serverSettings  - file with settings of server (etc/settings.py)
47            processes      - module with operations provided by this server
48        """
49
50        self.settings = serverSettings  # server settings
51        self.WPS = getcapabilities.WPS()
52        self.gc = self.WPS.gc            # Structure with all parameters defined in WPS
53        self.processes = processes    # Package with operations
54        self.document = Document()
55        self.Append = append.Append()
56
57        # creating of the root element Capabilities
58        self.Capabilities = self.document.createElementNS(self.WPS.namespaces['wps'],"Capabilities")
59        self.Capabilities.setAttribute("version", self.settings.WPS['version'])
60        self.Capabilities.setAttribute("xmlns",self.WPS.namespaces['wps'])
61        self.Capabilities.setAttribute("xmlns:xlink",self.WPS.namespaces['xlink'])
62        self.Capabilities.setAttribute("xmlns:ows",self.WPS.namespaces['ows'])
63        self.Capabilities.setAttribute("xmlns:xsi",self.WPS.namespaces['xsi'])
64        self.Capabilities.setAttribute("xsi:schemaLocation",self.WPS.namespaces['wps']+' '+self.WPS.schemalocation['wps'])
65        self.document.appendChild(self.Capabilities)
66
67        # GetCapabilities has 4 section. Every Section will be added
68        self.serviceIdentification(self.Capabilities)
69        self.serviceProvider(self.Capabilities)
70        self.operationsMetadata(self.Capabilities)
71        self.processOfferings(self.Capabilities)
72        return
73   
74    def serviceIdentification(self,parent):
75        """
76        Creates ServiceIdentification XML Node
77
78        Inputs:
79            parent  - node to which the ServiceIdentification node should be appended
80        """
81
82        serviceIdentOWS = self.gc['elements']['ServiceIdentification']['elements']
83        ServiceIdentificationNode = self.document.createElement("ows:ServiceIdentification")
84        parent.appendChild(ServiceIdentificationNode)
85        userSettings = self.settings.WPS['ServiceIdentification']
86
87        # for each value in responce structure
88        for opt in self.gc['elements']['ServiceIdentification']['order']:
89            # keywords
90            if opt == "Keywords":
91                try:
92                    if len(self.settings.WPS['Keywords']) > 0:
93                        namespace = self.gc['elements']['ServiceIdentification']['elements']['Keywords']['ns']
94                        node = self.document.createElement("%s%s" % (namespace, "Keywords"))
95                        for word in self.settings.WPS['Keywords']:
96                            keynode = self.document.createElement("%s%s" % (namespace,"Keyword"))
97                            keytext = self.document.createTextNode(word)
98                            keynode.appendChild(keytext)
99                            node.appendChild(keynode)
100                        ServiceIdentificationNode.appendChild(node)
101                except KeyError:
102                    comment = self.document.createComment("Keywords not defined")
103                    ServiceIdentificationNode.appendChild(comment)
104            else:
105                self.Append.Node(
106                            document=self.document, 
107                            childNode=opt, 
108                            parentNode=ServiceIdentificationNode,
109                            Elements=serviceIdentOWS, 
110                            Values=self.settings.WPS['ServiceIdentification']
111                            )
112        return
113
114    def serviceProvider(self,parent):
115        """
116        Creates ServiceIdentification XML Node
117
118        Inputs:
119            parent  - node to which the ServiceIdentification node should be appended
120        """
121
122        provider = self.document.createElement("ows:ServiceProvider")
123        parent.appendChild(provider)
124
125        # FIXME: This should be solved with help of some seperate "contact" function
126        for opt in self.gc['elements']['ServiceProvider']['order']:
127            if opt == "ServiceContact":
128                ns = self.gc['elements']['ServiceProvider']['elements']['ServiceContact']['ns']
129                servicecontact =  self.document.createElement("%sServiceContact"% (ns))
130                provider.appendChild(servicecontact)
131               
132                for optS in self.gc['elements']['ServiceProvider']['elements']['ServiceContact']['order']:
133                    if optS == "ContactInfo":
134                        ns = self.gc['elements']['ServiceProvider']['elements']['ServiceContact']['elements']['ContactInfo']['ns']
135                        contactinfo = self.document.createElement("%sContactInfo"% (ns))
136                        servicecontact.appendChild(contactinfo)
137
138                        ns = self.gc['elements']['ServiceProvider']['elements']['ServiceContact']['elements']['ContactInfo']['elements']['Address']['ns']
139                        address = self.document.createElement("%sAddress"% (ns))
140                        contactinfo.appendChild(address)
141
142                        for optA in self.gc['elements']['ServiceProvider']['elements']['ServiceContact']['elements']['ContactInfo']['elements']['Address']['order']:
143                            self.Append.Node(
144                                document=self.document, 
145                                childNode=optA, 
146                                parentNode=address,
147                                Elements=self.gc['elements']['ServiceProvider']['elements']['ServiceContact']['elements']['ContactInfo']['elements']['Address']['elements'],
148                                Values=self.settings.WPS['ServiceProvider']
149                                )
150
151                    else:
152                        self.Append.Node(
153                            document=self.document, 
154                            childNode=optS, 
155                            parentNode=servicecontact,
156                            Elements=self.gc['elements']['ServiceProvider']['elements']['ServiceContact']['elements'],
157                            Values=self.settings.WPS['ServiceProvider']
158                            )
159            else:
160                self.Append.Node (
161                    document=self.document, 
162                    childNode=opt, 
163                    parentNode=provider,
164                    Elements=self.gc['elements']['ServiceProvider']['elements'],
165                    Values=self.settings.WPS['ServiceProvider']
166                    )
167        return
168
169    def operationsMetadata(self,parent):
170        """
171        Creates OperationsMetadata XML Node
172
173        Inputs:
174            parent  - node to which the ServiceIdentification node should be appended
175        """
176
177        operations = self.document.createElement("ows:OperationsMetadata")
178        opMetDat = self.gc['elements']['OperationsMetadata']
179        parent.appendChild(operations)
180
181
182        # Operation part
183        for request in opMetDat['elements']['Operation']['values']:
184            # Create one <Operation> node
185            namespace = opMetDat['elements']['Operation']['ns']
186            operation = self.document.createElement("%s%s" % 
187                    (namespace,"Operation"))
188            operation.setAttribute("name",request)
189            operations.appendChild(operation)
190
191            # and in the <Operation> node is only one <DCP> node
192            namespace = opMetDat['elements']['Operation']['elements']['DCP']['ns']
193            dcp = self.document.createElement("%s%s"%(namespace,"DCP"))
194            operation.appendChild(dcp)
195           
196            # and in the <DCP> node is only one <HTTP> node
197            namespace = \
198                opMetDat['elements']['Operation']['elements']['DCP']['HTTP']['ns']
199            http = self.document.createElement("%s%s"%(namespace,"HTTP"))
200            dcp.appendChild(http)
201
202            # for each protocol (Get and Post) create the appropriate node
203            protocols =\
204                opMetDat['elements']['Operation']['elements']['DCP']['HTTP']['elements']
205            for proto in protocols:
206                node = self.document.createElement("%s%s"%(namespace,proto))
207                http.appendChild(node)
208                try:
209                    address = self.settings.WPS['OperationsMetadata']['ServerAddress']
210                   
211                    # appending '?' or '&' to the end of address, if needed
212                    if proto.lower() == "get":
213                        if address[-1] != "?" and address[-1] != "&": 
214                            address = address + "?"
215                    else:
216                        if address[-1] == "?" or address[-1] == "&": 
217                            address = address[:-1]
218
219                    node.setAttribute("%shref" % (protocols[proto]['attributes']['href']['ns']),address)
220                except KeyError: 
221                   node.appendChild(self.document.createComment("===== Server Address not set =====")) 
222
223           
224        return
225
226    def processOfferings(self,parent):
227        """
228        Creates ProcessOfferings XML Node
229
230        Inputs:
231            parent  - node to which the ServiceIdentification node should be appended
232        """
233
234        processesOfferings = self.document.createElement("ProcessOfferings")
235        parent.appendChild(processesOfferings)
236        POff = self.gc['elements']['ProcessOfferings']
237
238        # for each process
239        for process in self.processes.__all__:
240            thisProcess = eval("self.processes."+process+".Process()")
241            processNode = self.document.createElement("%s%s" % (POff['Process']['ns'],"Process"))
242            try:
243                processNode.setAttribute("%s%s" % (POff['Process']['attributes']['processVersion']['ns'],\
244                        "processVersion"),thisProcess.processVersion)
245            except KeyError:
246                pass
247
248               
249            # append defined nodes to process
250            for opt in POff['Process']['order']:
251                self.Append.Node(
252                    document=self.document, 
253                    childNode=opt, 
254                    parentNode=processNode,
255                    Elements=POff['Process']['elements'], 
256                    Values=thisProcess,
257                    )
258            processesOfferings.appendChild(processNode)
259        return
260
261if __name__ == "__main__":
262    print """
263    This file can be used only as module
264    """
265
Note: See TracBrowser for help on using the repository browser.