source: exist/trunk/python/ndgUtils/DocumentRetrieve.py @ 5371

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/DocumentRetrieve.py@5371
Revision 5371, 3.8 KB checked in by lawrence, 11 years ago (diff)

Fixes to the underlying utils for handling the vocab server interface.
Note that by moving this stuff to it's own module, there might be
consequential changes in calling code.

Line 
1from eXistInterface import ndg_eXist
2from ndgXqueries import ndgXqueries
3
4
5import urllib2, logging, socket
6try:
7    from xml.etree import ElementTree as ET
8except ImportError:
9    try:
10        import ElementTree as ET
11    except ImportError:
12        # For some reason when I install ElementTree with easyinstall it
13        # is called "elementree".
14        import elementtree.ElementTree as ET
15
16debug=0
17 
18class DocumentRetrieve(ndg_eXist):
19    '''
20    This class provides a document retrieval service via the NDG exist interfaces
21    '''
22
23    ATOM_TYPE = 'ATOM-TYPE'
24    ATOM_BACKUP_TYPE = 'ATOM-BACKUP'
25    ATOM = 'ATOM'
26    def __init__(self, repository, pwfile='passwords.txt'):
27        logging.info("Using repository, '%s'" %repository)
28
29        ndg_eXist.__init__(self, db=repository, passwordFile=pwfile)
30        logging.info("DB connection initialised")
31        self.repository=repository
32        self.xq=ndgXqueries()
33        self.knownQueries={'DIF':'moles2dif','DC':'moles2DC','ISO19139':'moles2iso19139', \
34                           'NDG-B0':'moles','NDG-B1':'molesObjectType','MDIP':'moles2mdip', \
35                           'NDG-A0':'csml','NumSim':'numsim', self.ATOM:'atom', \
36                           self.ATOM_BACKUP_TYPE:'atom', self.ATOM_TYPE:'atomTypeList'}
37
38    def _retrieveDoc(self, schema, xqtype, targetCollection, repository, localID):
39        '''
40        Retrieve doc using specified XQuery type
41        @return: docName, docContents
42        '''
43        logging.debug("Retrieving doc - type, '%s', coll, '%s', rep:'%s', localID:'%s'" \
44                      %(xqtype,targetCollection,repository,localID))
45        xquery=self.xq.actual(xqtype,targetCollection,repository,localID)
46
47        id,summary=self.executeQuery(xquery)
48        # NB, backups will inevitably return lots of docs - only retrieve the top one
49        # for the moment - since this is not really needed atm
50        if summary['hits'] != 1 and schema != self.ATOM_BACKUP_TYPE:
51            raise ValueError('Unable to obtain single %s document [%s] (hits=%s)'\
52                             %(schema,localID,summary['hits']))
53
54        docName = summary['documents'][0][0]
55        r=self.retrieve(id,0,{})
56        self.sessionRelease(id)
57        return docName, r
58
59
60    def get(self,repository,schema,localID,targetCollection='/db/discovery/moles', \
61            includeDocNameData=False):
62        '''
63        @keyword includeDocNameData: if True, a dictionary is returned, instead of the dataset, with the key
64        being the name of the document and the entry being the dataset
65        '''
66        logging.debug("Get called with rep:'%s', schema:'%s', localID:'%s', collection:'%s'" \
67                      %(repository,schema,localID,targetCollection))
68        docName = ""
69        if schema not in self.knownQueries:
70            raise TypeError('Unknown Schema "%s" in URI'%schema)
71
72        xqtype=self.knownQueries[schema]
73
74        if schema == 'NDG-B1':
75            # this is a general moles object - so need to further establish the type of moles doc
76            # it is to get the correct XQUery to use
77            name, xml = self._retrieveDoc(schema, xqtype, targetCollection, \
78                                          repository, localID)
79            xml=ET.fromstring(xml)
80            otype=int(xml.text or 0)
81           
82            xqtype={4:'stubB_dataEntity',
83                    3:'stubB_observationStation',
84                    2:'stubB_DPT',
85                    1:'stubB_activity'}[otype]
86
87        docName, r = self._retrieveDoc(schema, xqtype, targetCollection, 
88                                       repository, localID)
89        if includeDocNameData:
90            return {docName: r}
91        return r
92   
93   
94    def error(self,string,t,r,s,l):
95        raise ValueError,string+' for %s:%s:%s in %s'%(r,s,l,t)
96
97           
Note: See TracBrowser for help on using the repository browser.