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

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

Sundry modifications associated with the deployment on glue, and better
options for viewing and downloading the underlying xml records (not yet
complete)

Line 
1
2
3class ndgObject:
4
5    ''' This class instantiates an ndgObject and methods to obtain
6                (1) a downloadable xml representation from a repository,
7                (2) a printable xml representation '''
8   
9    def __init__(self,uri,config=None):
10        ''' Instantiate an ndg URI, and potentially methods for retrieving documents described by this URI'''
11        bits=uri.split(':')
12        bits2=uri.split('__')
13        ok=1
14        if len(bits)==3:
15            repository,schema,localID=bits
16        elif len(bits2)==3:
17            repository,schema,localID=bits2
18        else:
19            bits=uri.split('/')
20            if len(bits)==2:
21                schema='NDG-B0'
22                repository,localID=bits
23            else: ok=0
24        if not ok:
25            raise ValueError,'URI [%s] is not a valid NDG style URI'%uri
26        else:
27            self.repository,self.schema,self.localID=repository,schema,localID
28        self.setConfig(config)
29       
30    def setConfig(self,config):
31        ''' Set up the configuration for retrieving this document '''
32        if config is None:
33            self.gettable=0
34        else:
35            self.config=config
36            #we can only get this file for sure if it's a discovery record
37            if self.schema in ['DIF','DC']:
38                self.baseURL='%s?repository=%s&uri=%s__%s__%s'%(self.config.get('DISCOVERY','default'),
39                                        self.config.get('DEFAULT','repository'),
40                                        self.repository,self.schema,self.localID)
41                self.xmlURL=self.baseURL+'&type=xml'
42                self.printableURL=self.baseURL+'&type=print'
43                self.gettable=1
44            else:
45                self.gettable=0
46               
47    def get(self,type='xml'):
48        ''' Actually retrieve an xml record corresponding to an ndg URI '''
49        if self.gettable:
50            pass
51        else:
52            pass
53         
54
55import unittest
56
57class TestCase(unittest.TestCase):
58    """ Tests as required """
59   
60    def testURI1(self):
61        ''' Testing the underscore ID type '''
62        uri='badc.nerc.ac.uk__NDG-B0__local1'
63        n=ndgObject(uri)
64        self.__assertions(n)
65       
66    def __assertions(self,uri):
67        self.assertEqual('badc.nerc.ac.uk',uri.repository)
68        self.assertEqual('NDG-B0',uri.schema)
69        self.assertEqual('local1',uri.localID)
70       
71    def testURI2(self):
72        ''' Testing the colon form of ID '''
73        uri='badc.nerc.ac.uk:NDG-B0:local1'
74        n=ndgObject(uri)
75        self.__assertions(n)
76   
77    def testURI3(self):
78        ''' Testing the default MOLES form of id '''
79        uri='badc.nerc.ac.uk/local1'
80        n=ndgObject(uri)
81        self.__assertions(n)
82   
83    def testMakeURL(self):
84        ''' Tests making a url via a configuration file '''
85        uri='badc.nerc.ac.uk__NDG-B0__local1'
86        n=ndgObject(uri)
87        n.schema='DIF'
88        from Utilities import myConfig
89        config=myConfig('ndgDiscovery.config')
90        n.setConfig(config)
91        if n.gettable:
92            self.assertEqual(n.URL[0:5],'http:')
93
94if __name__=="__main__":
95    unittest.main()
Note: See TracBrowser for help on using the repository browser.