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

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

Refactoring browse for consistent service binding
and icons.

RevLine 
[1150]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)
[1152]23        self.icon=self.get('RELATED','icon')
24        self.iconalt=self.get('RELATED','icon_alt')
[1150]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 '''
[1152]28        serviceType=serviceType.upper()
[1150]29        r=self.get(serviceType,dataProvider)
30        if r is None:
[1152]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
[1150]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       
[938]47class Service:
[1150]48    ''' This simple class simply holds the service information known in a DIF ''' 
[938]49    def __init__(self):
[1150]50        self.type='dif_related_url'
[938]51        self.name=''
52        self.url=''
53        self.description=''
54
55class ServiceBinding (Service):
[1150]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 '''
[1152]60        def __init__(self,n='',nativeID=None,serviceFile='cgi/serviceMap.config',uri='local'):
[1150]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'}
[1151]65            self.serviceMap=ServiceMap(serviceFile=serviceFile)
[1150]66            self.name=n
[1152]67            if nativeID is not None: self.native(nativeID,uri=uri) 
68        def native(self,elem,uri='local'):
[1150]69            ''' Given an ndg id, get the native service '''
70            #e.g.
71            #<dataModelID>
72            #    <schemeIdentifier>NDG-A0</schemeIdentifier>
73            #    <repositoryIdentifier>badc.nerc.ac.uk</repositoryIdentifier>
74            #    <localIdentifier>acsoe_badc_eae96_macehead_mcf_1</localIdentifier>
75            #</dataModelID>
76            if elem is None:return 'No valid URL'
77            s=wrapGetText(elem,'schemeIdentifier')
78            r=wrapGetText(elem,'repositoryIdentifier')
79            l=wrapGetText(elem,'localIdentifier')
80            if s in self.mapping: s=self.mapping[s]
81            b=self.serviceMap.do(s,r)
[1152]82            if uri=='local':
83                uri=l
84            elif uri=='stubB':
85                uri='%s/%s'%(r,l)
86            self.url='%s?uri=%s'%(b,uri)
87            self.serviceType=s
[1150]88            return self
89        def related(self,elem,idelem):
90            ''' Given a DIF related URL, get a service binding '''
91            # e.g:
92            #<Related_URL>
93            #<URL_Content_Type>NDG_B_Service</URL_Content_Type>
94            #<URL>badc.nerc.ac.uk</URL>
95            #</Related_URL>
96            #<Related_URL>
97            #<URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
98            #</Related_URL>
99            self.description=wrapGetText(elem,'Description')
[1152]100            self.serviceType=wrapGetText(elem,'URL_Content_Type')
101            if self.serviceType!='':
102                b=self.serviceMap.do(self.serviceType,wrapGetText(elem,'URL'))
[1150]103                l=wrapGetText(idelem,'localIdentifier')
104                self.url='%s?uri=%s'%(b,l)
105            else:
106                self.url=wrapGetText(elem,'URL')
[1152]107            f=file('tmplog.log','a')
108            f.write(self.serviceType+'#'+self.url+'#'+self.icon())
109            f.close()
[1150]110            return self
111        def __str__(self):
112            ''' return string only html link '''
113            return hyperlink(self.name,self.url)
114        def icon(self):
115            ''' return icon html link '''
116            return hyperlink(image(self.serviceMap.icon,self.serviceMap.iconalt),self.url)
[911]117               
118if __name__=="__main__":
119        import ElementTree as ET
120        s='''<dataModelID>
121                <schemeIdentifier>NDG-A0</schemeIdentifier>
122                <repositoryIdentifier>badc.nerc.ac.uk</repositoryIdentifier>
123                <localIdentifier>acsoe_badc_eae96_macehead_mcf_1</localIdentifier>
124            </dataModelID>'''
[1150]125        idelem=ET.fromstring(s)
[1151]126        n=ServiceBinding('macehead',nativeID=idelem,serviceFile='serviceMap.config')
[1150]127        maceheadServices=[n]
128        s='''<dummy><Related_URL>
129        <URL_Content_Type>NDG_B_Service</URL_Content_Type>
130        <URL>badc.nerc.ac.uk</URL>
131        </Related_URL>
132        <Related_URL>
133        <URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
134        </Related_URL>
135        <Related_URL>
136        <URL_Content_Type>NDG_A_Service</URL_Content_Type>
137        <URL>glue.badc.rl.ac.uk/somelongpath</URL>
138        <Description> dummy dx example </Description>
139        </Related_URL></dummy>
140        '''
141        stuff=ET.fromstring(s)
142        for x in stuff.findall('Related_URL'):
[1151]143            n=ServiceBinding('macehead',serviceFile='serviceMap.config')
[1150]144            maceheadServices.append(n.related(x,idelem))
145        for i in maceheadServices: 
146            print str(i)
147            print i.icon()
Note: See TracBrowser for help on using the repository browser.