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

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

Fixes potential underscore in ndg URI problem.

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