source: TI07-MOLES/trunk/PythonCode/wsgi/ServiceBinding.py @ 1905

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/ServiceBinding.py@1905
Revision 1905, 8.3 KB checked in by lawrence, 14 years ago (diff)

Working discovery within paste-deploy context ...

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