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

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

Sundry browse bug fixes from PML experience, plus look-n-feel
in browse and discovery.

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        ''' Instantiate an ndg URI, and potentially methods for retrieving documents described by this URI
9        if a config file is available.  Three default views should be available:
10            (1) a rendered html version
11            (2) a downloadable xml representation, and
12            (3) a printable xml version.
13        Real services should also be available, and at some point of course the services should be looked up
14        at a service registry, but that's not yet available. '''
15       
16        self.gettable=-1  # The specific record is not known to be gettable via any service
17                          # other values of gettable allowed are
18                          # 0: browseable, 1: discoverable
19                         
20        # handle all the known ways of doing an NDG URI ...
21        bits=uri.split(':')
22        bits2=uri.split('__')
23        ok=1
24        if len(bits)==3:
25            repository,schema,localID=bits
26            self.uri=uri.replace(':','__')
27        elif len(bits2)==3:
28            repository,schema,localID=bits2
29            self.uri=uri
30        elif len(bits2)>3:
31            l=bits2[2]
32            for i in bits2[3:]:l+='__%s'%i
33            repository,schema,localID=bits2[0],bits2[1],l
34            self.uri=uri
35        else:
36            bits=uri.split('/')
37            if len(bits)==2:
38                schema='NDG-B0'
39                repository,localID=bits
40                self.uri=None  #
41                ok=0  # I reckon we shouldn't ever see any of these again ...
42                # but if we do, the uri will need fixing too ...
43            else: ok=0
44        if not ok:
45            # after all that, we don't think it's an NDG URI ...
46            raise ValueError,'URI [%s] is not a valid NDG style URI'%uri
47        else:
48            # yes, it is an NDG URI ...
49            self.repository,self.schema,self.localID=repository,schema,localID
50        self.setConfig(config)
51       
52    def setConfig(self,config):
53        ''' Set up the configuration for retrieving this document '''
54        if config is None:
55            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
56        else:
57            self.config=config
58            # This NDG object may itself be a discovery record, which makes life easy, but
59            # it might not be, in which case we have to build up all the possible views upon it.
60            discoveryBASE='%s&uri=%s__%s__%s'%(
61                self.config.get('DISCOVERY','default'),self.repository,self.schema,self.localID)
62            fmt=self.config.get('DISCOVERY','formatDefault','DIF')
63            # We'll build the following even if it can't be used (as would be the case for
64            # a non data entity B record or an A record) because it's a useful template.
65            self.discoveryURL=discoveryBASE+'&format=%s&type=html'%fmt
66            # If this record is itself a discovery record, then we don't have much more to do
67            if self.schema in ['DIF','DC','MDIP','ISO19139']:
68                self.xmlURL=self.discoveryURL.replace('html','xml')
69                self.printableURL=self.discoveryURL.replace('html','print')
70                self.gettable=1
71                self.URL=self.discoveryURL
72            elif self.schema in ['NDG-B0','NDG-B1']:
73                # One day we'll use a service binding to get this
74                # This is a mapping from the ndg repository id to an actual repository id
75                # understood by the ndg exist interface
76                blank=self.config.get('NDG_B_SERVICE','instance')
77                servicehost=self.config.get('NDG_B_SERVICE',self.repository,'unknown')
78                existhost=self.config.get('NDG_EXIST',self.repository,self.repository)
79                url=blank.replace('SERVICEHOST',servicehost)
80                url=url.replace('EXISTHOST',existhost)
81                self.URL=url.replace('SCHEMA',self.schema)+self.uri
82                self.xmlURL=self.URL.replace('html','xml')
83                self.printableURL=self.URL.replace('html','print')
84                if servicehost!='unknown': self.gettable=0
85            else:
86                #currently we don't know how to get this one
87                self.URL='unknown'
88                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
89               
90        #now, we'll build a stub-B url as well, in cae that comes in handy
91        if self.gettable<>-1:
92            if self.schema<>'NDG-B1':
93                defhost=self.config.get('DEFAULT','server')
94                uri=self.uri.replace(self.schema,'NDG-B1')
95                self.BURL='%s/browse?uri=%s&type=html'%(
96                        self.config.get('NDG_BROWSE_HOSTS',self.repository,defhost),uri)
97            else: self.BURL=self.URL
98        else:
99            self.BURL=None
100           
101    def get(self,type='xml'):
102        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
103        if self.gettable:
104            pass
105        else:
106            pass
107         
108
109import unittest
110
111class TestCase(unittest.TestCase):
112    """ Tests as required """
113   
114    def getConfig(self):
115        from Utilities import myConfig
116        config=myConfig('ndgDiscovery.config')
117        return config
118   
119    def testURI1(self):
120        ''' Testing the underscore ID type '''
121        uri='badc.nerc.ac.uk__NDG-B0__local1'
122        n=ndgObject(uri)
123        self.__assertions(n)
124       
125    def __assertions(self,uri):
126        self.assertEqual('badc.nerc.ac.uk',uri.repository)
127        self.assertEqual('NDG-B0',uri.schema)
128        self.assertEqual('local1',uri.localID)
129       
130    def testURI2(self):
131        ''' Testing the colon form of ID '''
132        uri='badc.nerc.ac.uk:NDG-B0:local1'
133        n=ndgObject(uri)
134        self.__assertions(n)
135   
136    def notestURI3(self):
137        ''' Testing the default MOLES form of id '''
138        uri='badc.nerc.ac.uk/local1'
139        n=ndgObject(uri)
140        self.__assertions(n)
141   
142    def testMakeURL(self):
143        ''' Tests making a url via a configuration file '''
144        uri='badc.nerc.ac.uk__NDG-B0__local1'
145        n=ndgObject(uri)
146        n.schema='DIF'
147        n.setConfig(self.getConfig())
148        if n.gettable:
149            self.assertEqual(n.discoveryURL[0:5],'http:')
150            print n.discoveryURL
151   
152    def testStubB(self):
153        ''' Test the url components of a stub-b '''
154        uri='badc.nerc.ac.uk__NDG-B1__dataent_chablis'
155        n=ndgObject(uri,self.getConfig())
156        print n.discoveryURL
157        print n.URL
158        print n.xmlURL
159        print n.printableURL
160
161    def testPMLuri(self):
162        '''Make sure the pml uris work in ndg object '''
163        uri='npm.ac.uk__NDG-B1__dataent.692'
164        n=ndgObject(uri,self.getConfig())
165        print n.URL
166
167    def testHorribleURI(self):
168        ''' tests __ in localID '''
169        uri='repository__schema__something__else'
170        n=ndgObject(uri)
171        self.assertEqual(('repository','schema','something__else'),
172            (n.repository,n.schema,n.localID))
173       
174
175if __name__=="__main__":
176    unittest.main()
Note: See TracBrowser for help on using the repository browser.