source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndgObject.py @ 2680

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndgObject.py@2680
Revision 2680, 6.8 KB checked in by lawrence, 12 years ago (diff)

ows stack now has the equivalent support for DIF to
the older wsgi stack

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/view/%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
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+'?format=raw'
69                self.printableURL=self.discoveryURL+'?format=xml'
70                self.gettable=1
71                self.URL=self.discoveryURL
72            elif self.schema in ['NDG-B0','NDG-B1','NumSim']:
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                url=blank.replace('SERVICEHOST',servicehost)
79                url=url.replace('URI',self.uri)
80                self.URL=url
81                self.xmlURL=url+'?format=raw'
82                self.printableURL=url+'?format=xml'
83                if servicehost!='unknown': self.gettable=0
84            else:
85                #currently we don't know how to get this one
86                self.URL='unknown'
87                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
88               
89        #now, we'll build a stub-B url as well, in case that comes in handy
90        if self.schema!='NumSim' and self.gettable<>-1:
91            self.BURL=self.URL.replace(self.schema,'NDG-B1')
92        else:
93            self.BURL=None
94           
95    def get(self,type='xml'):
96        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
97        if self.gettable:
98            pass
99        else:
100            pass
101       
102       
103    def __str__(self):
104        return self.uri
105
106import unittest
107
108class TestCase(unittest.TestCase):
109    """ Tests as required """
110   
111    def getConfig(self):
112        from Utilities import myConfig
113        config=myConfig('ndgDiscovery.config')
114        return config
115   
116    def testURI1(self):
117        ''' Testing the underscore ID type '''
118        uri='badc.nerc.ac.uk__NDG-B0__local1'
119        n=ndgObject(uri)
120        self.__assertions(n)
121       
122    def __assertions(self,uri):
123        self.assertEqual('badc.nerc.ac.uk',uri.repository)
124        self.assertEqual('NDG-B0',uri.schema)
125        self.assertEqual('local1',uri.localID)
126       
127    def testURI2(self):
128        ''' Testing the colon form of ID '''
129        uri='badc.nerc.ac.uk:NDG-B0:local1'
130        n=ndgObject(uri)
131        self.__assertions(n)
132   
133    def notestURI3(self):
134        ''' Testing the default MOLES form of id '''
135        uri='badc.nerc.ac.uk/local1'
136        n=ndgObject(uri)
137        self.__assertions(n)
138   
139    def testMakeURL(self):
140        ''' Tests making a url via a configuration file '''
141        uri='badc.nerc.ac.uk__NDG-B0__local1'
142        n=ndgObject(uri)
143        n.schema='DIF'
144        n.setConfig(self.getConfig())
145        if n.gettable:
146            self.assertEqual(n.discoveryURL[0:5],'http:')
147            print n.discoveryURL
148   
149    def testStubB(self):
150        ''' Test the url components of a stub-b '''
151        uri='badc.nerc.ac.uk__NDG-B1__dataent_chablis'
152        n=ndgObject(uri,self.getConfig())
153        print n.discoveryURL
154        print n.URL
155        print n.xmlURL
156        print n.printableURL
157
158    def testPMLuri(self):
159        '''Make sure the pml uris work in ndg object '''
160        uri='npm.ac.uk__NDG-B1__dataent.692'
161        n=ndgObject(uri,self.getConfig())
162        print n.URL
163
164    def testHorribleURI(self):
165        ''' tests __ in localID '''
166        uri='repository__schema__something__else'
167        n=ndgObject(uri)
168        self.assertEqual(('repository','schema','something__else'),
169            (n.repository,n.schema,n.localID))
170       
171
172if __name__=="__main__":
173    unittest.main()
Note: See TracBrowser for help on using the repository browser.