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

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

Now using the SOAP call for all discovery D calls, rather than XMLRPC
for discovery portability ...

Line 
1#
2#
3
4class ndgObject:
5
6    ''' This class instantiates an ndgObject which describes the various ways of obtaining itself, primarily
7                (1) a downloadable xml representation from a repository,
8                (2) a printable xml representation '''
9   
10    def __init__(self,uri,config=None):
11           
12        ''' Instantiate an ndg URI, and potentially methods for retrieving documents described by this URI.
13       
14        If discoveryOriginal is set, then it indicates that the URI provided corresponds to an original
15        URI which can be obtained both from the original database, and via transformation from the
16        intermediate schema (aka mini-moles) database '''
17       
18        bits=uri.split(':')
19        bits2=uri.split('__')
20        ok=1
21        if len(bits)==3:
22            repository,schema,localID=bits
23            self.uri=uri.replace(':','__')
24        elif len(bits2)==3:
25            repository,schema,localID=bits2
26            self.uri=uri
27        else:
28            bits=uri.split('/')
29            if len(bits)==2:
30                schema='NDG-B0'
31                repository,localID=bits
32                self.uri=None  #
33                ok=0  # I reckon we shouldn't ever see any of these again ...
34                # but if we do, the uri will need fixing too ...
35            else: ok=0
36        if not ok:
37            raise ValueError,'URI [%s] is not a valid NDG style URI'%uri
38        else:
39            self.repository,self.schema,self.localID=repository,schema,localID
40        self.setConfig(config)
41       
42    def setConfig(self,config):
43        ''' Set up the configuration for retrieving this document '''
44        if config is None:
45            self.gettable=-1
46            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
47        else:
48            self.config=config
49            # this NDG object may itself be a discovery record, which makes life easy, but
50            # it might not be, in which case we have to build up all the possible views upon it.
51            discoveryBASE='%s&uri=%s__%s__%s'%(
52                self.config.get('DISCOVERY','default'),self.repository,self.schema,self.localID)
53            fmt=self.config.get('DISCOVERY','formatDefault','DIF')
54            self.discoveryURL=discoveryBASE+'&format=%s&type=html'%fmt
55            # If this record is itself a discovery record, then we don't have much more to do
56            if self.schema in ['DIF','DC','MDIP','ISO19139']:
57                self.xmlURL=self.discoveryURL.replace('html','xml')
58                self.printableURL=self.discoveryURL.replace('html','print')
59                self.gettable=1
60                self.URL=self.discoveryURL
61            elif self.schema in ['MOLES-B0','MOLES-B1']:
62                #one day we'll use a service binding to get this
63                #this is a mapping from the ndg repository id to an actual repository id
64                #understood by the ndg exist interface
65                repository=self.config.get('NDG_B_SERVICE',self.repository)
66                self.URL=self.discoveryURL.replace('ndg',repository)
67                self.xmlURL=self.URL.replace('html','xml')
68                self.printableURL=self.URL.replace('html','print')
69                self.gettable=0
70            else:
71                #currently we don't know how to get this one
72                self.gettable=-1
73                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
74
75           
76    def get(self,type='xml'):
77        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
78        if self.gettable:
79            pass
80        else:
81            pass
82         
83
84import unittest
85
86class TestCase(unittest.TestCase):
87    """ Tests as required """
88   
89    def testURI1(self):
90        ''' Testing the underscore ID type '''
91        uri='badc.nerc.ac.uk__NDG-B0__local1'
92        n=ndgObject(uri)
93        self.__assertions(n)
94       
95    def __assertions(self,uri):
96        self.assertEqual('badc.nerc.ac.uk',uri.repository)
97        self.assertEqual('NDG-B0',uri.schema)
98        self.assertEqual('local1',uri.localID)
99       
100    def testURI2(self):
101        ''' Testing the colon form of ID '''
102        uri='badc.nerc.ac.uk:NDG-B0:local1'
103        n=ndgObject(uri)
104        self.__assertions(n)
105   
106    def notestURI3(self):
107        ''' Testing the default MOLES form of id '''
108        uri='badc.nerc.ac.uk/local1'
109        n=ndgObject(uri)
110        self.__assertions(n)
111   
112    def testMakeURL(self):
113        ''' Tests making a url via a configuration file '''
114        uri='badc.nerc.ac.uk__NDG-B0__local1'
115        n=ndgObject(uri)
116        n.schema='DIF'
117        from Utilities import myConfig
118        config=myConfig('ndgDiscovery.config')
119        n.setConfig(config)
120        if n.gettable:
121            self.assertEqual(n.discoveryURL[0:5],'http:')
122            print n.discoveryURL
123           
124
125if __name__=="__main__":
126    unittest.main()
Note: See TracBrowser for help on using the repository browser.