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

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

Add CDML and CSML namespace/dtd info to use when checking file type.

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