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

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

Fix a few bugs in DocumentRetrieve? + add support for Atom based docs
+ extend test suite for the DocumentRetrieve? class + improve error
handling and logging.

Line 
1import ndgRetrieve,xmlHandler2, logging
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        logging.debug("Initialising ndgObject with uri: '%s'" %uri)
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
44        # yes, it is an NDG URI ...
45        self.repository,self.schema,self.localID=repository,schema,localID
46        logging.debug("Extracted valid NDG values from URI: repository: '%s', schema: '%s', localID: '%s'" \
47                      %(repository, schema, localID))
48        self.setConfig(config)
49       
50    def setConfig(self,config):
51        '''
52        Set up the configuration for retrieving this document
53        '''
54        logging.debug("Setting up configuration for retrieving document")
55        self.config=config
56        if config is None:
57            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
58            self.BURL=None
59            return
60
61        self.server=self.config.get('DISCOVERY','default')
62        server=self.server
63        qs=None
64        # This NDG object may itself be a discovery record, which makes life easy, but
65        # it might not be, in which case we have to build up all the possible views upon it.
66        # But remember only data entity b records have discovery records ...
67        self.viewService='%s/view/'%server
68        discoveryBASE='%s%s__%s__%s'%(self.viewService,self.repository,self.schema,self.localID)
69        fmt=self.config.get('DISCOVERY','formatDefault','DIF')
70        self.BURL=None
71        # We'll build the following even if it can't be used (as would be the case for
72        # a non data entity B record or an A record) because it's a useful template.
73        if self.schema!=fmt: qs=('outputSchema',fmt)
74        self.discoveryURL=self.__buildURL(discoveryBASE,[qs])
75        servicehost=self.config.get('NDG_B_SERVICE',self.repository,'unknown')
76        # If this record is itself a discovery record, then we don't have much more to do
77        if self.schema in ['DIF','DC','MDIP','ISO19139']:
78            self.xmlURL=self.__buildURL(
79                discoveryBASE.replace('/view/','/retrieve/'),[qs,('format','raw')])
80            self.printableURL=self.__buildURL(discoveryBASE,[qs,('format','xml')])
81            self.URL=self.discoveryURL
82            if servicehost!='unknown':self.gettable=1
83        elif self.schema in ['NDG-B0','NDG-B1','NumSim']:
84            # One day we'll use a service binding to get this
85            # This is a mapping from the ndg repository id to an actual repository id
86            # understood by the ndg exist interface
87            blank=self.config.get('NDG_B_SERVICE','instance')
88            url=blank.replace('SERVICEHOST',servicehost)
89            url=url.replace('URI',self.uri)
90            self.URL=url
91            self.xmlURL=url.replace('/view/','/retrieve/')+'?format=raw'
92            self.printableURL=url+'?format=xml'
93            if servicehost!='unknown': self.gettable=0
94        else:
95            #currently we don't know how to get this one
96            self.URL='unknown'
97            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
98
99        logging.debug("Set up URL: '%s'\ndiscoveryURL: '%s'" %(self.URL, self.discoveryURL))
100           
101        #now, we'll build a stub-B url as well, in case that comes in handy
102        if self.schema!='NumSim' and self.gettable<>-1:
103            self.BURL=discoveryBASE.replace(self.schema,'NDG-B1')
104            self.BURL=self.BURL.replace(server,servicehost)
105            logging.debug("Set up stub-B url: '%s'" %self.BURL)
106
107
108    def __buildURL(self,base,queryStuff):
109        ss=''
110        for i in queryStuff: 
111            if i!=None:ss+='&%s=%s'%(i[0],i[1])
112        if ss!='':ss='?'+ss[1:]
113        return base+ss
114           
115    def get(self,securityCredentials=None):
116        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
117        if self.config is None: return None
118        status,xmlh=ndgRetrieve(self,self.config)
119        if status:
120            x=xmlHandler2.xmlHandler(xmlh,string=1)
121            return x.xmls
122        else:
123            return status
124       
125    def __str__(self):
126        return self.uri
127
Note: See TracBrowser for help on using the repository browser.