source: TI07-MOLES/trunk/PythonCode/wsgi/ndgObject.py @ 2422

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

Code to support late binding to Browse service

Line 
1class ndgObject:
2
3    ''' This class instantiates an ndgObject which describes the various ways of obtaining itself, primarily
4                (1) a downloadable xml representation from a repository,
5                (2) a printable xml representation '''
6   
7    def __init__(self,uri,config=None):
8        ''' Instantiate an ndg URI, and potentially methods for retrieving documents described by this URI
9        if a config file is available.  Three default views should be available:
10            (1) a rendered html version
11            (2) a downloadable xml representation, and
12            (3) a printable xml version.
13        Real services should also be available, and at some point of course the services should be looked up
14        at a service registry, but that's not yet available. '''
15       
16        self.gettable=-1  # The specific record is not known to be gettable via any service
17                          # other values of gettable allowed are
18                          # 0: browseable, 1: discoverable
19                         
20        # handle all the known ways of doing an NDG URI ...
21        bits=uri.split(':')
22        bits2=uri.split('__')
23        ok=1
24        if len(bits)==3:
25            repository,schema,localID=bits
26            self.uri=uri.replace(':','__')
27        elif len(bits2)==3:
28            repository,schema,localID=bits2
29            self.uri=uri
30        elif len(bits2)>3:
31            l=bits2[2]
32            for i in bits2[3:]:l+='__%s'%i
33            repository,schema,localID=bits2[0],bits2[1],l
34            self.uri=uri
35        else:
36            bits=uri.split('/')
37            if len(bits)==2:
38                schema='NDG-B0'
39                repository,localID=bits
40                self.uri=None  #
41                ok=0  # I reckon we shouldn't ever see any of these again ...
42                # but if we do, the uri will need fixing too ...
43            else: ok=0
44        if not ok:
45            # after all that, we don't think it's an NDG URI ...
46            raise ValueError,'URI [%s] is not a valid NDG style URI'%uri
47        else:
48            # yes, it is an NDG URI ...
49            self.repository,self.schema,self.localID=repository,schema,localID
50        self.setConfig(config)
51       
52    def setConfig(self,config):
53        ''' Set up the configuration for retrieving this document '''
54        if config is None:
55            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
56        else:
57            self.config=config
58            # This NDG object may itself be a discovery record, which makes life easy, but
59            # it might not be, in which case we have to build up all the possible views upon it.
60            discoveryBASE='%s&uri=%s__%s__%s'%(
61                self.config.get('DISCOVERY','default'),self.repository,self.schema,self.localID)
62            fmt=self.config.get('DISCOVERY','formatDefault','DIF')
63            # We'll build the following even if it can't be used (as would be the case for
64            # a non data entity B record or an A record) because it's a useful template.
65            self.discoveryURL=discoveryBASE+'&format=%s&type=html'%fmt
66            # If this record is itself a discovery record, then we don't have much more to do
67            if self.schema in ['DIF','DC','MDIP','ISO19139']:
68                self.xmlURL=self.discoveryURL.replace('html','xml')
69                self.printableURL=self.discoveryURL.replace('html','print')
70                self.gettable=1
71                self.URL=self.discoveryURL
72            elif self.schema in ['NDG-B0','NDG-B1']:
73                # One day we'll use a service binding to get this
74                # This is a mapping from the ndg repository id to an actual repository id
75                # understood by the ndg exist interface
76                repository=self.config.get('NDG_B_SERVICE',self.repository,'Unknown-B-Repository')
77                url=self.discoveryURL.replace('ndg',repository)
78                self.URL=url.replace('format=%s'%fmt,'format=%s'%self.schema)
79                self.xmlURL=self.URL.replace('html','xml')
80                self.printableURL=self.URL.replace('html','print')
81                if repository!='Unknown-B-Repository': self.gettable=0
82            else:
83                #currently we don't know how to get this one
84                self.URL='unknown'
85                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
86               
87        #now, we'll build a stub-B url as well, in cae that comes in handy
88        if self.gettable<>-1:
89            if self.schema<>'NDG-B1':
90                defhost=self.config.get('DEFAULT','server')
91                uri=self.uri.replace(self.schema,'NDG-B1')
92                self.BURL='%s/browse?uri=%s&type=html'%(
93                        self.config.get('NDG_BROWSE_HOSTS',self.repository,defhost),uri)
94            else: self.BURL=self.URL
95        else:
96            self.BURL=None
97           
98    def get(self,type='xml'):
99        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
100        if self.gettable:
101            pass
102        else:
103            pass
104         
105
106import unittest
107
108class TestCase(unittest.TestCase):
109    """ Tests as required """
110   
111    def getConfig(self):
112        from Utilities import myConfig
113        config=myConfig('ndgDiscovery.config')
114        return config
115   
116    def testURI1(self):
117        ''' Testing the underscore ID type '''
118        uri='badc.nerc.ac.uk__NDG-B0__local1'
119        n=ndgObject(uri)
120        self.__assertions(n)
121       
122    def __assertions(self,uri):
123        self.assertEqual('badc.nerc.ac.uk',uri.repository)
124        self.assertEqual('NDG-B0',uri.schema)
125        self.assertEqual('local1',uri.localID)
126       
127    def testURI2(self):
128        ''' Testing the colon form of ID '''
129        uri='badc.nerc.ac.uk:NDG-B0:local1'
130        n=ndgObject(uri)
131        self.__assertions(n)
132   
133    def notestURI3(self):
134        ''' Testing the default MOLES form of id '''
135        uri='badc.nerc.ac.uk/local1'
136        n=ndgObject(uri)
137        self.__assertions(n)
138   
139    def testMakeURL(self):
140        ''' Tests making a url via a configuration file '''
141        uri='badc.nerc.ac.uk__NDG-B0__local1'
142        n=ndgObject(uri)
143        n.schema='DIF'
144        n.setConfig(self.getConfig())
145        if n.gettable:
146            self.assertEqual(n.discoveryURL[0:5],'http:')
147            print n.discoveryURL
148   
149    def testStubB(self):
150        ''' Test the url components of a stub-b '''
151        uri='badc.nerc.ac.uk__NDG-B1__dataent_chablis'
152        n=ndgObject(uri,self.getConfig())
153        print n.discoveryURL
154        print n.URL
155        print n.xmlURL
156        print n.printableURL
157
158
159    def testHorribleURI(self):
160        ''' tests __ in localID '''
161        uri='repository__schema__something__else'
162        n=ndgObject(uri)
163        self.assertEqual(('repository','schema','something__else'),
164            (n.repository,n.schema,n.localID))
165       
166
167if __name__=="__main__":
168    unittest.main()
Note: See TracBrowser for help on using the repository browser.