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

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

Improved handling of deployments in browse.

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