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

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

Interim check in browse update ... (browse still not working properly)

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 ['NDG-B0','NDG-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,'Unknown-B-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 getConfig(self):
90        from Utilities import myConfig
91        config=myConfig('ndgDiscovery.config')
92        return config
93   
94    def testURI1(self):
95        ''' Testing the underscore ID type '''
96        uri='badc.nerc.ac.uk__NDG-B0__local1'
97        n=ndgObject(uri)
98        self.__assertions(n)
99       
100    def __assertions(self,uri):
101        self.assertEqual('badc.nerc.ac.uk',uri.repository)
102        self.assertEqual('NDG-B0',uri.schema)
103        self.assertEqual('local1',uri.localID)
104       
105    def testURI2(self):
106        ''' Testing the colon form of ID '''
107        uri='badc.nerc.ac.uk:NDG-B0:local1'
108        n=ndgObject(uri)
109        self.__assertions(n)
110   
111    def notestURI3(self):
112        ''' Testing the default MOLES form of id '''
113        uri='badc.nerc.ac.uk/local1'
114        n=ndgObject(uri)
115        self.__assertions(n)
116   
117    def testMakeURL(self):
118        ''' Tests making a url via a configuration file '''
119        uri='badc.nerc.ac.uk__NDG-B0__local1'
120        n=ndgObject(uri)
121        n.schema='DIF'
122        n.setConfig(self.getConfig())
123        if n.gettable:
124            self.assertEqual(n.discoveryURL[0:5],'http:')
125            print n.discoveryURL
126   
127    def testStubB(self):
128        uri='grid.bodc.nerc.ac.uk__MOLES-B1__PCDA47973RS2302'
129        n=ndgObject(uri)
130        n.setConfig(self.getConfig())
131       
132       
133
134if __name__=="__main__":
135    unittest.main()
Note: See TracBrowser for help on using the repository browser.