source: TI04-geosplat/trunk/pygsc/WSCaller.py @ 1210

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI04-geosplat/trunk/pygsc/WSCaller.py@1210
Revision 1210, 3.0 KB checked in by astephen, 14 years ago (diff)

Updated lots of stuff to get zsi working with version 2.0 or lower.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6WSCaller.py
7===========
8
9Holds the WSCaller class used to make calls to the Web Service
10across the network to a remote server.
11
12"""
13
14# Import standard library modules
15import os, sys, re
16
17# Import SOAP library
18from ZSI.client import Binding
19
20# Import package modules
21from clientConfig import *
22from common import *
23from DisplayManager import DisplayManager
24
25
26class WSCaller:
27    """
28    Used to call Web Service on a remote machine.
29    """         
30   
31    def __init__(self):
32        """
33        Sets up the instance.
34        """
35        # Set up SOAP bindings
36        trace=open('/tmp/tracefile.txt','w')
37        urlPattern=re.compile(r"(\w+)://([\w.]+):(\d+)/(.*)$")
38        (transport, soap_server_host, soap_server_port, cmd)=urlPattern.match(SOAP_SERVER_URL).groups()
39        from ZSI.version import Version as zsiv
40        zsiv=float("%s.%s" % zsiv[:2])
41        if zsiv<2.0:
42            self.server=Binding(url="", host=soap_server_host, port=soap_server_port)
43        else:
44            self.server = Binding(url=SOAP_SERVER_URL) 
45#       self.server=Binding(url='', host=SOAP_SERVER_NAME, port=SOAP_SERVER_PORT, tracefile=trace)             
46
47
48    def callServerMethod(self, methodName, args, packArgs=None):
49        """
50
51        Calls method "methodName(args)" and returns whatever is returned.
52        """
53        if packArgs:
54            args=self._packArgsAsList(args)
55       
56        # Make that call
57        try:
58            response=apply(getattr(self.server, methodName), args) 
59            response=deUnicodeObject(response)     
60        except Exception, error:
61            if str(error)=="(111, 'Connection refused')":
62                error="GeoSPlAT server is currently unavailable. Please try again later."
63            self._displayErrorPage(error)
64        response=self._webServiceCallWrapper(response)
65        return response
66       
67       
68    def _packArgsAsList(self, args):
69        """
70        In order to work with ZSI SOAP library need to pack up arguments as a list
71        of [keyword, value] pairs rather than a dictionary.
72        """
73        newList=[]
74        for key, value in args.items():
75            newList.append([key, value])
76        return newList
77       
78
79    def _deUnicodeObject(self, obj):
80        """
81        Returns an identical object with all unicode strings returned as normal strings.
82        """
83        return deUnicodeObject(obj)     
84       
85
86    def _webServiceCallWrapper(self, response):
87        """
88        Analyses the response from a Web Service call to check if an error has
89        occurred. If so it parses and reports the error. Otherwise it returns 
90        the object returned from the Web Service call.
91        """
92        if type(response) in (type(""), type(u'w')):
93            # String returns are errors
94            self._displayErrorPage(response)
95        else:
96            return response
97
98
99    def _displayErrorPage(self, error):
100        """
101        Displays the error if one is found.
102        """
103        self.displayer=DisplayManager()
104        self.displayer._displayErrorPage(error)
105        if DEBUG==1: raise Exception, error
106        sys.exit()
107       
Note: See TracBrowser for help on using the repository browser.