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

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

Putting browse code into a module to make it easier
to coexist with other cgi scripts etc ...

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.get('RELATED','icon')
24        self.iconalt=self.get('RELATED','icon_alt')
25    def do(self,serviceType,dataProvider):
26        '''  Return an actual endpoint for a given service from the data provider, e.g.
27        for ndg_a_service,badc.nerc.ac.uk get http://superglue.badc.rl.ac.uk/cgi-bin/browse.py '''
28        serviceType=serviceType.upper() # ought to be redundant ...
29        r=self.get(serviceType,dataProvider)
30        if r is None:
31            #see if we have an icon anyway ...
32            try:
33                i,a=self.get(serviceType,'icon'),self.get(serviceType,'icon_alt')
34                if (i,a)!=(None,None):self.icon,self.iconalt=i,a
35            except: pass
36               
37            # just return the URL
38            if dataProvider[0:4]!='http':
39                return 'http://%s'%dataProvider
40            else: return dataProvider
41        else:
42            # this is a service type known to ndg, so we have constructed an endpoint
43            self.icon=self.get(serviceType,'icon')
44            self.iconalt=self.get(serviceType,'icon_alt')
45            return r
46       
47class Service:
48    ''' This simple class simply holds the service information known in a DIF ''' 
49    def __init__(self):
50        self.type='dif_related_url'
51        self.name=''
52        self.url=''
53        self.description=''
54
55class ServiceBinding (Service):
56        ''' Given a specific NDG id, we can go from the scheme identifier to the
57        native service which one expects to be able to operate upon it, for example,
58        an NDG-A type identifier should expect the NDG-A-Service running at the
59        specific repository '''
60        def __init__(self,n='',nativeID=None,serviceFile='cgi/serviceMap.config',uri='local'):
61            Service.__init__(self)
62            # regrettably we used different things in our ID's than we did in
63            # our difs :
64            self.mapping={'NDG-A0':'NDG_A_SERVICE','NDG-B0':'NDG_B_SERVICE',
65            'NDG-B1':'NDG_B_SERVICE'}
66            self.serviceMap=ServiceMap(serviceFile=serviceFile)
67            self.name=n
68            if nativeID is not None: self.native(nativeID,uri=uri) 
69        def native(self,elem,uri='local'):
70            ''' Given an ndg id, get the native service '''
71            #e.g.
72            #<dataModelID>
73            #    <schemeIdentifier>NDG-A0</schemeIdentifier>
74            #    <repositoryIdentifier>badc.nerc.ac.uk</repositoryIdentifier>
75            #    <localIdentifier>acsoe_badc_eae96_macehead_mcf_1</localIdentifier>
76            #</dataModelID>
77            if elem is None:return 'No valid URL'
78            s=wrapGetText(elem,'schemeIdentifier')
79            r=wrapGetText(elem,'repositoryIdentifier')
80            l=wrapGetText(elem,'localIdentifier')
81            if s in self.mapping: s=self.mapping[s].upper()
82            b=self.serviceMap.do(s,r)
83            uri=self.__makeuri(s,r,l)
84            self.url='%s?uri=%s'%(b,uri)
85            self.serviceType=s
86            return self
87        def related(self,elem,idelem):
88            ''' Given a DIF related URL, get a service binding '''
89            # e.g:
90            #<Related_URL>
91            #<URL_Content_Type>NDG_B_Service</URL_Content_Type>
92            #<URL>badc.nerc.ac.uk</URL>
93            #</Related_URL>
94            #<Related_URL>
95            #<URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
96            #</Related_URL>
97            self.description=wrapGetText(elem,'Description')
98            s=wrapGetText(elem,'URL_Content_Type').upper()
99            self.serviceType=s
100            if s!='':
101                r=wrapGetText(elem,'URL')
102                b=self.serviceMap.do(s,r)
103                l=wrapGetText(idelem,'localIdentifier')
104                uri=self.__makeuri(s,r,l)
105                self.url='%s?uri=%s'%(b,uri)
106            else:
107                self.url=wrapGetText(elem,'URL')
108            return self
109        def __str__(self):
110            ''' return string only html link '''
111            return hyperlink(self.name,self.url)
112        def icon(self):
113            ''' return html icon linking to specific service '''
114            return hyperlink(image(self.serviceMap.icon,self.serviceMap.iconalt),self.url)
115        def __makeuri(self,s,b,l):
116            ''' Actually create the URL '''
117            if s=='NDG_B_SERVICE':
118                uri='%s/%s'%(b,l)
119            else:
120                uri=l
121            return uri
122           
123if __name__=="__main__":
124        import ElementTree as ET
125        s='''<dataModelID>
126                <schemeIdentifier>NDG-A0</schemeIdentifier>
127                <repositoryIdentifier>badc.nerc.ac.uk</repositoryIdentifier>
128                <localIdentifier>acsoe_badc_eae96_macehead_mcf_1</localIdentifier>
129            </dataModelID>'''
130        idelem=ET.fromstring(s)
131        n=ServiceBinding('macehead',nativeID=idelem,serviceFile='serviceMap.config')
132        maceheadServices=[n]
133        s='''<dummy><Related_URL>
134        <URL_Content_Type>NDG_B_Service</URL_Content_Type>
135        <URL>badc.nerc.ac.uk</URL>
136        </Related_URL>
137        <Related_URL>
138        <URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
139        </Related_URL>
140        <Related_URL>
141        <URL_Content_Type>NDG_A_Service</URL_Content_Type>
142        <URL>glue.badc.rl.ac.uk/somelongpath</URL>
143        <Description> dummy dx example </Description>
144        </Related_URL></dummy>
145        '''
146        stuff=ET.fromstring(s)
147        for x in stuff.findall('Related_URL'):
148            n=ServiceBinding('macehead',serviceFile='serviceMap.config')
149            maceheadServices.append(n.related(x,idelem))
150        for i in maceheadServices: 
151            print str(i)
152            print i.icon()
Note: See TracBrowser for help on using the repository browser.