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

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

Move module level constants to become class level constants; NB, the former
seem to only be visible to other modules in the same package.

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