source: ndgCommon/trunk/ndg/common/src/models/ndgObject.py @ 5198

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/models/ndgObject.py@5198
Revision 5198, 7.6 KB checked in by cbyrom, 10 years ago (diff)

Write README.txt for MILK with basic install and config info +
tidy up config files, removing obsolete data + remove unused help
page + fix proper use of global discovery url to simplify use.

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