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

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

Modifications to show original records

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