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

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

Support for viewing xhtml description sections within stub-B

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        uri=str(uri) #Dom had problem with unicode coming in here ... dunno why @@@@
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,'The identifier [%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            self.BURL=None
57        else:
58            self.config=config
59            qs=None
60            # This NDG object may itself be a discovery record, which makes life easy, but
61            # it might not be, in which case we have to build up all the possible views upon it.
62            # But remember only data entity b records have discovery records ...
63            server=self.config.get('DISCOVERY','default')
64            self.viewService='%s/view/'%server
65            discoveryBASE='%s%s__%s__%s'%(self.viewService,self.repository,self.schema,self.localID)
66            fmt=self.config.get('DISCOVERY','formatDefault','DIF')
67            self.BURL=None
68            # We'll build the following even if it can't be used (as would be the case for
69            # a non data entity B record or an A record) because it's a useful template.
70            if self.schema!=fmt: qs=('outputSchema',fmt)
71            self.discoveryURL=self.__buildURL(discoveryBASE,[qs])
72            servicehost=self.config.get('NDG_B_SERVICE',self.repository,'unknown')
73            # If this record is itself a discovery record, then we don't have much more to do
74            if self.schema in ['DIF','DC','MDIP','ISO19139']:
75                self.xmlURL=self.__buildURL(
76                    discoveryBASE.replace('/view/','/retrieve/'),[qs,('format','raw')])
77                self.printableURL=self.__buildURL(discoveryBASE,[qs,('format','xml')])
78                self.URL=self.discoveryURL
79                if servicehost!='unknown':self.gettable=1
80            elif self.schema in ['NDG-B0','NDG-B1','NumSim']:
81                # One day we'll use a service binding to get this
82                # This is a mapping from the ndg repository id to an actual repository id
83                # understood by the ndg exist interface
84                blank=self.config.get('NDG_B_SERVICE','instance')
85                url=blank.replace('SERVICEHOST',servicehost)
86                url=url.replace('URI',self.uri)
87                self.URL=url
88                self.xmlURL=url.replace('/view/','/retrieve/')+'?format=raw'
89                self.printableURL=url+'?format=xml'
90                if servicehost!='unknown': self.gettable=0
91            else:
92                #currently we don't know how to get this one
93                self.URL='unknown'
94                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
95               
96            #now, we'll build a stub-B url as well, in case that comes in handy
97            if self.schema!='NumSim' and self.gettable<>-1:
98                self.BURL=discoveryBASE.replace(self.schema,'NDG-B1')
99                self.BURL=self.BURL.replace(server,servicehost)
100
101    def __buildURL(self,base,queryStuff):
102        ss=''
103        for i in queryStuff: 
104            if i!=None:ss+='&%s=%s'%(i[0],i[1])
105        if ss!='':ss='?'+ss[1:]
106        return base+ss
107           
108    def get(self,type='xml'):
109        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
110        if self.gettable:
111            pass
112        else:
113            pass
114       
115       
116    def __str__(self):
117        return self.uri
118
119import unittest
120
121class TestCase(unittest.TestCase):
122    """ Tests as required """
123   
124    def getConfig(self):
125        from Utilities import myConfig
126        config=myConfig('ndgDiscovery.config')
127        return config
128   
129    def testURI1(self):
130        ''' Testing the underscore ID type '''
131        uri='badc.nerc.ac.uk__NDG-B0__local1'
132        n=ndgObject(uri)
133        self.__assertions(n)
134       
135    def __assertions(self,uri):
136        self.assertEqual('badc.nerc.ac.uk',uri.repository)
137        self.assertEqual('NDG-B0',uri.schema)
138        self.assertEqual('local1',uri.localID)
139       
140    def testURI2(self):
141        ''' Testing the colon form of ID '''
142        uri='badc.nerc.ac.uk:NDG-B0:local1'
143        n=ndgObject(uri)
144        self.__assertions(n)
145   
146    def notestURI3(self):
147        ''' Testing the default MOLES form of id '''
148        uri='badc.nerc.ac.uk/local1'
149        n=ndgObject(uri)
150        self.__assertions(n)
151   
152    def testMakeURL(self):
153        ''' Tests making a url via a configuration file '''
154        uri='badc.nerc.ac.uk__NDG-B0__local1'
155        n=ndgObject(uri)
156        n.schema='DIF'
157        n.setConfig(self.getConfig())
158        if n.gettable:
159            self.assertEqual(n.discoveryURL[0:5],'http:')
160            print n.discoveryURL
161   
162    def testStubB(self):
163        ''' Test the url components of a stub-b '''
164        uri='badc.nerc.ac.uk__NDG-B1__dataent_chablis'
165        n=ndgObject(uri,self.getConfig())
166        print n.discoveryURL
167        print n.URL
168        print n.xmlURL
169        print n.printableURL
170
171    def testPMLuri(self):
172        '''Make sure the pml uris work in ndg object '''
173        uri='npm.ac.uk__NDG-B1__dataent.692'
174        n=ndgObject(uri,self.getConfig())
175        print n.URL
176
177    def testHorribleURI(self):
178        ''' tests __ in localID '''
179        uri='repository__schema__something__else'
180        n=ndgObject(uri)
181        self.assertEqual(('repository','schema','something__else'),
182            (n.repository,n.schema,n.localID))
183       
184
185if __name__=="__main__":
186    unittest.main()
Note: See TracBrowser for help on using the repository browser.