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

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

Upgrade the various 'list' xqueries - to nest all results in a single
root element - so that only one document need be retrieved to get all
results + adjust ndgDirectory to cope with processing the new results +
fix the various namespaces mentioned in the codebase to map to the
current atom/moles ones.

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