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

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

Some more browse improvements. Handling dgMetadataDescription.
Better Linking.

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                url=self.discoveryURL.replace('ndg',repository)
69                self.URL=url.replace('format=%s'%fmt,'format=%s'%self.schema)
70                self.xmlURL=self.URL.replace('html','xml')
71                self.printableURL=self.URL.replace('html','print')
72                self.gettable=0
73            else:
74                #currently we don't know how to get this one
75                self.gettable=-1
76                self.URL='unknown'
77                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
78        if self.gettable<>-1:
79            if self.schema<>'NDG-B1':
80                defhost=self.config.get('DEFAULT','server')
81                uri=self.uri.replace(self.schema,'NDG-B1')
82                self.BURL='%s/browse?uri=%s&type=html'%(
83                        self.config.get('NDG_BROWSE_HOSTS',self.repository,defhost),uri)
84            else: self.BURL=self.URL
85        else:
86            self.BURL=None
87           
88    def get(self,type='xml'):
89        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
90        if self.gettable:
91            pass
92        else:
93            pass
94         
95
96import unittest
97
98class TestCase(unittest.TestCase):
99    """ Tests as required """
100   
101    def getConfig(self):
102        from Utilities import myConfig
103        config=myConfig('ndgDiscovery.config')
104        return config
105   
106    def testURI1(self):
107        ''' Testing the underscore ID type '''
108        uri='badc.nerc.ac.uk__NDG-B0__local1'
109        n=ndgObject(uri)
110        self.__assertions(n)
111       
112    def __assertions(self,uri):
113        self.assertEqual('badc.nerc.ac.uk',uri.repository)
114        self.assertEqual('NDG-B0',uri.schema)
115        self.assertEqual('local1',uri.localID)
116       
117    def testURI2(self):
118        ''' Testing the colon form of ID '''
119        uri='badc.nerc.ac.uk:NDG-B0:local1'
120        n=ndgObject(uri)
121        self.__assertions(n)
122   
123    def notestURI3(self):
124        ''' Testing the default MOLES form of id '''
125        uri='badc.nerc.ac.uk/local1'
126        n=ndgObject(uri)
127        self.__assertions(n)
128   
129    def testMakeURL(self):
130        ''' Tests making a url via a configuration file '''
131        uri='badc.nerc.ac.uk__NDG-B0__local1'
132        n=ndgObject(uri)
133        n.schema='DIF'
134        n.setConfig(self.getConfig())
135        if n.gettable:
136            self.assertEqual(n.discoveryURL[0:5],'http:')
137            print n.discoveryURL
138   
139    def testStubB(self):
140        ''' Test the url components of a stub-b '''
141        uri='badc.nerc.ac.uk__NDG-B1__dataent_chablis'
142        n=ndgObject(uri,self.getConfig())
143        print n.discoveryURL
144        print n.URL
145        print n.xmlURL
146        print n.printableURL
147
148
149    def testHorribleURI(self):
150        ''' tests __ in localID '''
151        uri='repository__schema__something__else'
152        n=ndgObject(uri)
153        self.assertEqual(('repository','schema','something__else'),
154            (n.repository,n.schema,n.localID))
155       
156
157if __name__=="__main__":
158    unittest.main()
Note: See TracBrowser for help on using the repository browser.