source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/ServiceBinding.py @ 1151

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/ServiceBinding.py@1151
Revision 1151, 5.8 KB checked in by lawrence, 13 years ago (diff)

Minor additions to support service binding in browse
interface.

Line 
1#
2# There are (at least) two classes of service map issues which NDG needs to support.
3# Firstly, one can expect that given a specific NDG record, there is a native
4# service for that record ... eg. for an A record, the native service is the
5# data extractor, but there may be others ... at some point we need to be
6# able to find those out (this is a processing affordance issue, and something
7# that we want to use registries for) ... anyway, meanwhile, we have these "native"
8# service types.
9# Secondly, in D  we have related URL's, which are essentially mappings to
10# services which may exist on the dataset described by that D-catalogue record.
11# This is a special case of the generic case described above ...
12#
13# This module provides classes to support these activities.
14
15from Utilities import wrapGetText, myConfig
16from htmlUtilities import hyperlink,image
17
18class ServiceMap(myConfig):
19    ''' This should itself eventually be a service that can be looked up, or maybe
20    even use the dns '''
21    def __init__(self,serviceFile='cgi/serviceMap.config'):
22        myConfig.__init__(self,serviceFile)
23        self.icon,self.iconalt=None,'NoIcon'
24    def do(self,serviceType,dataProvider):
25        '''  Return an actual endpoint for a given service from the data provider, e.g.
26        for ndg_a_service,badc.nerc.ac.uk get http://superglue.badc.rl.ac.uk/cgi-bin/browse.py '''
27        r=self.get(serviceType,dataProvider)
28        if r is None:
29            # we assume this isn't an NDG service and just return the URL
30            if dataProvider[0:4]!='http':
31                return 'http://%s'%dataProvider
32            else: return dataProvider
33        else:
34            # this is a service type known to ndg, so we have constructed an endpoint
35            self.icon=self.get(serviceType,'icon')
36            print 'Icon',self.icon
37            self.iconalt=self.get(serviceType,'icon_alt')
38            return r
39       
40class Service:
41    ''' This simple class simply holds the service information known in a DIF ''' 
42    def __init__(self):
43        self.type='dif_related_url'
44        self.name=''
45        self.url=''
46        self.description=''
47
48class ServiceBinding (Service):
49        ''' Given a specific NDG id, we can go from the scheme identifier to the
50        native service which one expects to be able to operate upon it, for example,
51        an NDG-A type identifier should expect the NDG-A-Service running at the
52        specific repository '''
53        def __init__(self,n='',nativeID=None,serviceFile='cgi/serviceMap.config'):
54            Service.__init__(self)
55            # regrettably we used different things in our ID's than we did in
56            # our difs :
57            self.mapping={'NDG-A0':'NDG_A_Service','NDG-B0':'NDG_B_Service'}
58            self.serviceMap=ServiceMap(serviceFile=serviceFile)
59            self.name=n
60            if nativeID is not None: self.native(nativeID) 
61        def native(self,elem):
62            ''' Given an ndg id, get the native service '''
63            #e.g.
64            #<dataModelID>
65            #    <schemeIdentifier>NDG-A0</schemeIdentifier>
66            #    <repositoryIdentifier>badc.nerc.ac.uk</repositoryIdentifier>
67            #    <localIdentifier>acsoe_badc_eae96_macehead_mcf_1</localIdentifier>
68            #</dataModelID>
69            if elem is None:return 'No valid URL'
70            s=wrapGetText(elem,'schemeIdentifier')
71            r=wrapGetText(elem,'repositoryIdentifier')
72            l=wrapGetText(elem,'localIdentifier')
73            if s in self.mapping: s=self.mapping[s]
74            b=self.serviceMap.do(s,r)
75            self.url='%s?uri=%s'%(b,l)
76            return self
77        def related(self,elem,idelem):
78            ''' Given a DIF related URL, get a service binding '''
79            # e.g:
80            #<Related_URL>
81            #<URL_Content_Type>NDG_B_Service</URL_Content_Type>
82            #<URL>badc.nerc.ac.uk</URL>
83            #</Related_URL>
84            #<Related_URL>
85            #<URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
86            #</Related_URL>
87            self.description=wrapGetText(elem,'Description')
88            ctype=wrapGetText(elem,'URL_Content_Type')
89            if ctype!='':
90                b=self.serviceMap.do(ctype,wrapGetText(elem,'URL'))
91                l=wrapGetText(idelem,'localIdentifier')
92                self.url='%s?uri=%s'%(b,l)
93            else:
94                self.url=wrapGetText(elem,'URL')
95            return self
96        def __str__(self):
97            ''' return string only html link '''
98            return hyperlink(self.name,self.url)
99        def icon(self):
100            ''' return icon html link '''
101            return hyperlink(image(self.serviceMap.icon,self.serviceMap.iconalt),self.url)
102               
103if __name__=="__main__":
104        import ElementTree as ET
105        s='''<dataModelID>
106                <schemeIdentifier>NDG-A0</schemeIdentifier>
107                <repositoryIdentifier>badc.nerc.ac.uk</repositoryIdentifier>
108                <localIdentifier>acsoe_badc_eae96_macehead_mcf_1</localIdentifier>
109            </dataModelID>'''
110        idelem=ET.fromstring(s)
111        n=ServiceBinding('macehead',nativeID=idelem,serviceFile='serviceMap.config')
112        maceheadServices=[n]
113        s='''<dummy><Related_URL>
114        <URL_Content_Type>NDG_B_Service</URL_Content_Type>
115        <URL>badc.nerc.ac.uk</URL>
116        </Related_URL>
117        <Related_URL>
118        <URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
119        </Related_URL>
120        <Related_URL>
121        <URL_Content_Type>NDG_A_Service</URL_Content_Type>
122        <URL>glue.badc.rl.ac.uk/somelongpath</URL>
123        <Description> dummy dx example </Description>
124        </Related_URL></dummy>
125        '''
126        stuff=ET.fromstring(s)
127        for x in stuff.findall('Related_URL'):
128            n=ServiceBinding('macehead',serviceFile='serviceMap.config')
129            maceheadServices.append(n.related(x,idelem))
130        for i in maceheadServices: 
131            print str(i)
132            print i.icon()
Note: See TracBrowser for help on using the repository browser.