source: exist/trunk/python/ndgUtils/ndgObject.py @ 3500

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgObject.py@4025
Revision 3500, 5.2 KB checked in by cbyrom, 12 years ago (diff)

Change default value of discovery flag to 1 to get DIF records working via ndgSearch as standard.
Adjust ndgObject to convert the ndgRetrieve doc to a proper XML tree, with xmlHandler2.

Line 
1import ndgRetrieve,xmlHandler2
2
3class ndgObject:
4    ''' This class instantiates an ndgObject which describes the various ways of obtaining itself, primarily
5                (1) a downloadable xml representation from a repository,
6                (2) a printable xml representation '''
7   
8    def __init__(self,uri,config=None):
9        ''' Parse the uri and prepare for obtaining the actual content'''
10       
11        #Dom had problem with unicode coming in here ... dunno why @@@@
12        uri=str(uri) 
13       
14        # a priori, assume we can't get content for this object
15        self.gettable=-1 
16       
17        # handle all the known ways of doing an NDG URI ...
18        bits=uri.split(':')
19        bits2=uri.split('__')
20        ok=1
21        if len(bits)==3:
22            repository,schema,localID=bits
23            self.uri=uri.replace(':','__')
24        elif len(bits2)==3:
25            repository,schema,localID=bits2
26            self.uri=uri
27        elif len(bits2)>3:
28            repository,schema,localID=bits2[0],bits2[1],'__'.join(bits2[2:])
29            self.uri=uri
30        else:
31            bits=uri.split('/')
32            if len(bits)==2:
33                schema='NDG-B0'
34                repository,localID=bits
35                self.uri=None  #
36                ok=0  # I reckon we shouldn't ever see any of these again ...
37                # but if we do, the uri will need fixing too ...
38            else: ok=0
39       
40        if not ok:
41            # after all that, we don't think it's an NDG URI ...
42            raise ValueError,'The identifier [%s] is not a valid NDG style URI'%uri
43        else:
44            # yes, it is an NDG URI ...
45            self.repository,self.schema,self.localID=repository,schema,localID
46       
47        self.setConfig(config)
48       
49    def setConfig(self,config):
50        ''' Set up the configuration for retrieving this document '''
51        self.config=config
52        if config is None:
53            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
54            self.BURL=None
55        else:
56            self.server=self.config.get('DISCOVERY','default')
57            server=self.server
58            qs=None
59            # This NDG object may itself be a discovery record, which makes life easy, but
60            # it might not be, in which case we have to build up all the possible views upon it.
61            # But remember only data entity b records have discovery records ...
62            self.viewService='%s/view/'%server
63            discoveryBASE='%s%s__%s__%s'%(self.viewService,self.repository,self.schema,self.localID)
64            fmt=self.config.get('DISCOVERY','formatDefault','DIF')
65            self.BURL=None
66            # We'll build the following even if it can't be used (as would be the case for
67            # a non data entity B record or an A record) because it's a useful template.
68            if self.schema!=fmt: qs=('outputSchema',fmt)
69            self.discoveryURL=self.__buildURL(discoveryBASE,[qs])
70            servicehost=self.config.get('NDG_B_SERVICE',self.repository,'unknown')
71            # If this record is itself a discovery record, then we don't have much more to do
72            if self.schema in ['DIF','DC','MDIP','ISO19139']:
73                self.xmlURL=self.__buildURL(
74                    discoveryBASE.replace('/view/','/retrieve/'),[qs,('format','raw')])
75                self.printableURL=self.__buildURL(discoveryBASE,[qs,('format','xml')])
76                self.URL=self.discoveryURL
77                if servicehost!='unknown':self.gettable=1
78            elif self.schema in ['NDG-B0','NDG-B1','NumSim']:
79                # One day we'll use a service binding to get this
80                # This is a mapping from the ndg repository id to an actual repository id
81                # understood by the ndg exist interface
82                blank=self.config.get('NDG_B_SERVICE','instance')
83                url=blank.replace('SERVICEHOST',servicehost)
84                url=url.replace('URI',self.uri)
85                self.URL=url
86                self.xmlURL=url.replace('/view/','/retrieve/')+'?format=raw'
87                self.printableURL=url+'?format=xml'
88                if servicehost!='unknown': self.gettable=0
89            else:
90                #currently we don't know how to get this one
91                self.URL='unknown'
92                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
93               
94            #now, we'll build a stub-B url as well, in case that comes in handy
95            if self.schema!='NumSim' and self.gettable<>-1:
96                self.BURL=discoveryBASE.replace(self.schema,'NDG-B1')
97                self.BURL=self.BURL.replace(server,servicehost)
98
99    def __buildURL(self,base,queryStuff):
100        ss=''
101        for i in queryStuff: 
102            if i!=None:ss+='&%s=%s'%(i[0],i[1])
103        if ss!='':ss='?'+ss[1:]
104        return base+ss
105           
106    def get(self,securityCredentials=None):
107        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
108        if self.config is None: return None
109        status,xmlh=ndgRetrieve(self,self.config)
110        if status:
111            x=xmlHandler2.xmlHandler(xmlh,string=1)
112            return x.xmls
113        else:
114            return status
115       
116    def __str__(self):
117        return self.uri
118
Note: See TracBrowser for help on using the repository browser.