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

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

Fine tune handling of properties object handled by ndgObject + add extra
logging.

Line 
1import ndgRetrieve, xmlHandler2, logging
2
3# The various different document types:
4MOLES_DOC_TYPE = 'NDG-B0'
5DIF_DOC_TYPE = 'DIF'
6DC_DOC_TYPE = 'DC'
7ISO_DOC_TYPE = 'ISO19139'
8NDGB1_DOC_TYPE = 'NDG-B1'
9NDGB0_DOC_TYPE = 'NDG-B0'
10MDIP_DOC_TYPE = 'MDIP'
11NDGA0_DOC_TYPE = 'NDG-A0'
12NUMSIM_DOC_TYPE = 'NumSim'
13ATOM_DOC_TYPE = 'ATOM'
14ATOM_BACKUP_DOC_TYPE = 'ATOM-BACKUP'
15   
16# Group the doc types according to the source they should be retrieved from
17DISCOVERY_SCHEMES = ['DIF','DC','MDIP','ISO19139', 'ISO']
18BROWSE_SCHEMES = ['NDG-B0','NDG-B1','NumSim', 'ATOM', 'ATOM-BACKUP']
19
20class ndgObject:
21    ''' This class instantiates an ndgObject which describes the various ways
22        of obtaining itself, primarily
23                (1) a downloadable xml representation from a repository,
24                (2) a printable xml representation '''
25   
26    def __init__(self,uri,config=None):
27        ''' Parse the uri and prepare for obtaining the actual content'''
28        logging.debug("Initialising ndgObject with uri: '%s'" %uri)
29        #Dom had problem with unicode coming in here ... dunno why @@@@
30        uri=str(uri) 
31       
32        # a priori, assume we can't get content for this object
33        self.gettable=-1 
34       
35        # handle all the known ways of doing an NDG URI ...
36        bits=uri.split(':')
37        bits2=uri.split('__')
38        ok=1
39        if len(bits)==3:
40            repository,schema,localID=bits
41            self.uri=uri.replace(':','__')
42        elif len(bits2)==3:
43            repository,schema,localID=bits2
44            self.uri=uri
45        elif len(bits2)>3:
46            repository,schema,localID=bits2[0],bits2[1],'__'.join(bits2[2:])
47            self.uri=uri
48        else:
49            bits=uri.split('/')
50            if len(bits)==2:
51                schema='NDG-B0'
52                repository,localID=bits
53                self.uri=None  #
54                ok=0  # I reckon we shouldn't ever see any of these again ...
55                # but if we do, the uri will need fixing too ...
56            else: ok=0
57       
58        if not ok:
59            # after all that, we don't think it's an NDG URI ...
60            raise ValueError,'The identifier [%s] is not a valid NDG style URI'%uri
61
62        # yes, it is an NDG URI ...
63        self.repository,self.schema,self.localID=repository,schema,localID
64        logging.debug("Extracted valid NDG values from URI: repository: '%s', schema: '%s', localID: '%s'" \
65                      %(repository, schema, localID))
66        self.setConfig(config)
67
68    def setDefaultProperties(self):
69        '''
70        Set default properties for object
71        '''
72        self.discoveryURL,self.baseURL,self.xmlURL,\
73            self.printableURL =None,None,None,None
74        self.useDiscoveryService = 1
75        self.BURL=None
76
77       
78    def setConfig(self, config):
79        '''
80        Set up the configuration for retrieving this document
81        '''
82        logging.debug("Setting up configuration for retrieving document")
83        self.config=config
84        if config is None:
85            self.setDefaultProperties()
86            return
87
88        self.server=self.config.get('DISCOVERY','default')
89        server=self.server
90        qs=None
91        # This NDG object may itself be a discovery record, which makes life easy, but
92        # it might not be, in which case we have to build up all the possible views upon it.
93        # But remember only data entity b records have discovery records ...
94        self.viewService='%s/view/'%server
95        discoveryBASE='%s%s__%s__%s'%(self.viewService,self.repository,self.schema,self.localID)
96
97        # set default return format - if not set, just return in original format
98        fmt=''
99        if self.config.has_option('DISCOVERY','formatDefault'):
100            fmt=self.config.get('DISCOVERY','formatDefault')
101            logging.info("Default discovery format set to: %s" %fmt)
102           
103        self.BURL=None
104        # We'll build the following even if it can't be used (as would be the case for
105        # a non data entity B record or an A record) because it's a useful template.
106        if self.schema!=fmt: 
107            qs=('outputSchema',fmt)
108        self.discoveryURL=self.__buildURL(discoveryBASE,[qs])
109
110        # config file should have details on the service to use for the repository
111        # - if not, default to 'unknown'
112        servicehost = 'unknown'
113        if self.config.has_option('NDG_B_SERVICE',self.repository):
114            servicehost=self.config.get('NDG_B_SERVICE',self.repository)
115           
116        # If this record is itself a discovery record, then we don't have much more to do
117        if self.schema in DISCOVERY_SCHEMES:
118            self.xmlURL=self.__buildURL(
119                discoveryBASE.replace('/view/','/retrieve/'),[qs,('format','raw')])
120            self.printableURL=self.__buildURL(discoveryBASE,[qs,('format','xml')])
121            self.URL=self.discoveryURL
122            if servicehost!='unknown':self.gettable=1
123        elif self.schema in BROWSE_SCHEMES:
124            # One day we'll use a service binding to get this
125            # This is a mapping from the ndg repository id to an actual repository id
126            # understood by the ndg exist interface
127            blank=self.config.get('NDG_B_SERVICE','instance')
128            url=blank.replace('SERVICEHOST',servicehost)
129            url=url.replace('URI',self.uri)
130            self.URL=url
131            self.xmlURL=url.replace('/view/','/retrieve/')+'?format=raw'
132            self.printableURL=url+'?format=xml'
133            if servicehost!='unknown': self.gettable=0
134            self.useDiscoveryService = 0
135        else:
136            #currently we don't know how to get this one
137            self.URL='unknown'
138            self.setDefaultProperties()
139
140        logging.debug("Set up URL: '%s'\ndiscoveryURL: '%s'" %(self.URL, self.discoveryURL))
141           
142        #now, we'll build a stub-B url as well, in case that comes in handy
143        if self.schema!='NumSim' and self.gettable<>-1:
144            self.BURL=discoveryBASE.replace(self.schema,'NDG-B1')
145            self.BURL=self.BURL.replace(server,servicehost)
146            logging.debug("Set up stub-B url: '%s'" %self.BURL)
147
148
149    def __buildURL(self,base,queryStuff):
150        ss=''
151        for i in queryStuff: 
152            if i!=None:ss+='&%s=%s'%(i[0],i[1])
153        if ss!='':ss='?'+ss[1:]
154        return base+ss
155           
156    def get(self,securityCredentials=None):
157        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI
158        NB, not sure if this method is actually used anywhere - since get is
159        usually done via ndgRetrieve
160        '''
161        if self.config is None: return None
162        status,xmlh=ndgRetrieve.ndgRetrieve(self,self.config, \
163                                            discovery = self.useDiscoveryService)
164        if status:
165            x=xmlHandler2.xmlHandler(xmlh,string=1)
166            return x.xmls
167        else:
168            return status
169       
170    def __str__(self):
171        return self.uri
172
Note: See TracBrowser for help on using the repository browser.