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

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

Slight code refactoring ... (only committed to enable other sandbox
operations).

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