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

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

Sundry problems fixed with respect to namespaces 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 myConfig
16from htmlUtilities import hyperlink,image
17from ETxmlView import nsdumb,et2text
18
19def idconvert(e,helper=None):
20    ''' Converts an XML ndg identifier into a uri version '''
21    if helper is None:
22        helper=nsdumb()
23    s='%s:%s:%s'%(helper.getText(e,'repositoryIdentifier'),
24                    helper.getText(e,'schemeIdentifier'),
25                    helper.getText(e,'localIdentifier'))
26    return s
27
28class ServiceMap(myConfig):
29    ''' This should itself eventually be a service that can be looked up, or maybe
30    even use the dns '''
31    def __init__(self,serviceFile='cgi/serviceMap.config'):
32        myConfig.__init__(self,serviceFile)
33        self.icon=self.get('RELATED','icon')
34        self.iconalt=self.get('RELATED','icon_alt')
35    def do(self,serviceType,dataProvider):
36        '''  Return an actual endpoint for a given service from the data provider, e.g.
37        for ndg_a_service,badc.nerc.ac.uk get http://superglue.badc.rl.ac.uk/cgi-bin/browse.py '''
38        serviceType=serviceType.upper() # ought to be redundant ...
39        r=self.get(serviceType,dataProvider)
40        if r is None:
41            #see if we have an icon anyway ...
42            try:
43                i,a=self.get(serviceType,'icon'),self.get(serviceType,'icon_alt')
44                if (i,a)!=(None,None):self.icon,self.iconalt=i,a
45            except: pass
46               
47            # just return the URL
48            if dataProvider[0:4]!='http':
49                return 'http://%s'%dataProvider
50            else: return dataProvider
51        else:
52            # this is a service type known to ndg, so we have constructed an endpoint
53            self.icon=self.get(serviceType,'icon')
54            self.iconalt=self.get(serviceType,'icon_alt')
55            return r
56       
57class Service:
58    ''' This simple class simply holds the service information known in a DIF ''' 
59    def __init__(self):
60        self.type='dif_related_url'
61        self.name=''
62        self.url=''
63        self.description=''
64
65class ServiceBinding (Service):
66        ''' Given a specific entryID, we can go from the scheme identifier to the
67        native service which one expects to be able to operate upon it, for example,
68        an NDG-A type identifier should expect the NDG-A-Service running at the
69        specific repository. Note that we support the NDG id in the abbreviated format
70        entryID is a repository:scheme:localidentifier string '''
71        def __init__(self,n='',entryID=None,serviceFile='cgi/serviceMap.config'):
72            Service.__init__(self)
73            # regrettably we used different things in our ID's than we did in
74            # our difs :
75            self.mapping={'NDG-A0':'NDG_A_SERVICE','NDG-B0':'NDG_B_SERVICE',
76            'NDG-B1':'NDG_B_SERVICE','DIF':'DISCOVERY'}
77            self.serviceMap=ServiceMap(serviceFile=serviceFile)
78            self.name=n
79            if entryID is not None:
80                self.fromEntry(entryID)
81        def fromNative(self,nativeID,helper=None,seqno=1):
82            ''' Support native XML ID form of entryID, see also fromEntry '''
83            entryID=idconvert(nativeID,helper)
84            return self.fromEntry(entryID,seqno)
85        def fromEntry(self,entryID,seqno=1):
86            ''' Given an entryID, get the native service '''
87            #e.g. badc.nerc.ac.uk:NDG-A0:someLocalIdentifier
88            s,r,l,b=self.__getsrlb(entryID)
89            uri=self.__makeuri(s,r,l)
90            self.url=self.__makeurl(s,b,uri,seqno)
91            self.serviceType=s
92            return self
93        def fromRelated(self,elem,entryID,helper=None,seqno=1):
94            ''' Given a DIF related URL, get a service binding,
95            using an instantiated ET helper that's available
96            to avoid problems with namespaces '''
97            # e.g:
98            #<Related_URL>
99            #<URL_Content_Type>NDG_B_Service</URL_Content_Type>
100            #<URL>badc.nerc.ac.uk</URL>
101            #</Related_URL>
102            #<Related_URL>
103            #<URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
104            #</Related_URL>
105            s,r,l,b=self.__getsrlb(entryID)
106            #now ovewrite the self description above with
107            #detail from this particular rleated url
108            if helper is None:helper=nsdumb()
109            self.description=helper.getText(elem,'Description')
110            s=helper.getText(elem,'URL_Content_Type').upper()
111            self.serviceType=s
112            if s!='':
113                r=helper.getText(elem,'URL')
114                b=self.serviceMap.do(s,r)
115                uri=self.__makeuri(s,r,l)
116                self.url=self.__makeurl(s,b,uri,seqno)
117            else:
118                self.url=helper.getText(elem,'URL')
119            return self
120        def __str__(self):
121            ''' return string only html link '''
122            return hyperlink(self.name,self.url)
123        def icon(self):
124            ''' return html icon linking to specific service '''
125            return hyperlink(image(self.serviceMap.icon,self.serviceMap.iconalt),self.url)
126        def __makeuri(self,s,r,l):
127            ''' Actually create the URI '''
128            if r=='': return l
129            if s in ('NDG_B_SERVICE','DISCOVERY'):
130                uri='%s/%s'%(r,l)
131            else:
132                uri=l
133            return uri
134        def __makeurl(self,s,b,uri,seqno):
135            ''' Actually create the URL, which may be sequentially
136            numbered if it's going to the data extractor (and so
137            iType read from config file will have a %s in it ) '''
138            iType=self.serviceMap.get(s,'instance','uri')
139            if '%s' in iType: iType=iType%seqno
140            return '%s?%s=%s'%(b,iType,uri)
141        def __getsrlb(self,entryID):
142            '''Take an entry ID, and parse it for NDG identifiers otherwise
143            and if not, do something sensible '''
144            try:
145                r,s,l=entryID.split(':')
146                if s in self.mapping: s=self.mapping[s].upper()
147                b=self.serviceMap.do(s,r)
148            except:
149                #not from NDG ... provide only discovery binding
150                s,r,l='DISCOVERY','',entryID
151                b=self.serviceMap.get(s,'default')
152            return s,r,l,b
153           
154if __name__=="__main__":
155        import ElementTree as ET
156        s='''<dataModelID>
157                <schemeIdentifier>NDG-A0</schemeIdentifier>
158                <repositoryIdentifier>badc.nerc.ac.uk</repositoryIdentifier>
159                <localIdentifier>acsoe_badc_eae96_macehead_mcf_1</localIdentifier>
160            </dataModelID>'''
161        idelem=ET.fromstring(s)
162        entryID=idconvert(idelem)
163        n=ServiceBinding('macehead',entryID=entryID,serviceFile='serviceMap.config')
164        maceheadServices=[n]
165        s='''<dummy><Related_URL>
166        <URL_Content_Type>NDG_B_Service</URL_Content_Type>
167        <URL>badc.nerc.ac.uk</URL>
168        </Related_URL>
169        <Related_URL>
170        <URL>http://badc.nerc.ac.uk/browse/badc/CDs/spade9293</URL>
171        </Related_URL>
172        <Related_URL>
173        <URL_Content_Type>NDG_A_Service</URL_Content_Type>
174        <URL>glue.badc.rl.ac.uk/somelongpath</URL>
175        <Description> dummy dx example </Description>
176        </Related_URL></dummy>
177        '''
178        stuff=ET.fromstring(s)
179        for x in stuff.findall('Related_URL'):
180            n=ServiceBinding('macehead',serviceFile='serviceMap.config')
181            maceheadServices.append(n.fromRelated(x,idelem))
182        for i in maceheadServices: 
183            print str(i)
184            print i.icon()
Note: See TracBrowser for help on using the repository browser.