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

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

Probably a version of ndgObject that'll never get used ...

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,discoveryOriginal=0):
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.ori=discoveryOriginal
40        self.setConfig(config)
41       
42    def setConfig(self,config):
43        ''' Set up the configuration for retrieving this document '''
44        if config is None:
45            self.gettable=-1
46            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
47        else:
48            self.config=config
49            fundamental='%s?repository=%s'%(self.config.get('DISCOVERY','default'),
50                                        self.config.get('DEFAULT','repository'))
51            self.baseURL='%s&uri=%s__%s__%s'%(fundamental,self.repository,self.schema,self.localID)
52            fmt=self.config.get('DISCOVERY','formatDefault','DIF')
53           
54            if self.ori:
55                #(we'll allow self.uri to differ from the ndg uri "just-in-case")
56                self.original='%s&uri=%s&original=1&type=xml'%(fundamental,self.uri)
57                self.printableOriginal=self.original.replace('type=xml','type=print')
58           
59            # Now we indicate the main ways of finding this record
60            if self.schema in ['DIF','DC','MDIP','ISO19139','MOLES-B0','MOLES-B1']:
61                # The record is itself a discovery record, but we'll assume by default
62                # we want to show a specific rendered version
63                self.discoveryURL=self.baseURL+'&format=%s&type=html'%fmt
64                self.xmlURL=self.discoveryURL.replace('html','xml')
65                self.printableURL=self.discoveryURL.replace('html','print')
66                self.gettable=1
67            else:
68                #Actually we don't know what this is, so we make the discovery URL the same as the
69                #original one.
70                if self.ori:
71                    self.discoveryURL=self.printableOriginal
72                    self.printableURL=self.printableOriginal
73                    self.xmlURL=self.original
74                self.gettable=0
75
76           
77    def get(self,type='xml'):
78        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
79        if self.gettable:
80            pass
81        else:
82            pass
83         
84
85import unittest
86
87class TestCase(unittest.TestCase):
88    """ Tests as required """
89   
90    def testURI1(self):
91        ''' Testing the underscore ID type '''
92        uri='badc.nerc.ac.uk__NDG-B0__local1'
93        n=ndgObject(uri)
94        self.__assertions(n)
95       
96    def __assertions(self,uri):
97        self.assertEqual('badc.nerc.ac.uk',uri.repository)
98        self.assertEqual('NDG-B0',uri.schema)
99        self.assertEqual('local1',uri.localID)
100       
101    def testURI2(self):
102        ''' Testing the colon form of ID '''
103        uri='badc.nerc.ac.uk:NDG-B0:local1'
104        n=ndgObject(uri)
105        self.__assertions(n)
106   
107    def notestURI3(self):
108        ''' Testing the default MOLES form of id '''
109        uri='badc.nerc.ac.uk/local1'
110        n=ndgObject(uri)
111        self.__assertions(n)
112   
113    def testMakeURL(self):
114        ''' Tests making a url via a configuration file '''
115        uri='badc.nerc.ac.uk__NDG-B0__local1'
116        n=ndgObject(uri)
117        n.schema='DIF'
118        from Utilities import myConfig
119        config=myConfig('ndgDiscovery.config')
120        n.setConfig(config)
121        if n.gettable:
122            self.assertEqual(n.baseURL[0:5],'http:')
123
124if __name__=="__main__":
125    unittest.main()
Note: See TracBrowser for help on using the repository browser.