source: exist/trunk/python/ndgUtils/ndgDirectory.py @ 3158

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgDirectory.py@3158
Revision 3158, 2.2 KB checked in by lawrence, 12 years ago (diff)

... and support for creation dates of objects as well ...

Line 
1import ndgXqueries
2from eXistInterface import ndg_eXist
3from elementtree import ElementTree as ET
4class ndgDirectory:
5    ''' Treats an NDG directory as something that can be counted,listed, and dated '''
6    def __init__(self,targetCollection,existHost,docType='DIF'):
7        ''' Initialise a targetCollection directory of a particular docType.
8        Valid docTypes are:
9            dgObservationStation, dgDataEntity, dgActivity, dgDataProductionTool
10            DIF, MDIP
11        '''
12       
13        self.members=[]
14        self.lastUpdated=None
15        self.targetCollection=targetCollection
16       
17        #get the right namespace
18        self.ns={'DIF':'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/'}[docType]
19       
20        #select the query
21        queries=ndgXqueries.ndgXqueries()
22        xqName={'DIF':'DIFList'}[docType]
23        xq=queries[xqName]
24        xq=xq.replace('TargetCollection',self.targetCollection)
25       
26        #issue the query
27        self.xmldb=ndg_eXist(db=existHost)
28        id,s=self.xmldb.executeQuery(xq)
29        if s['hits']==0: return
30       
31        #ok loop over the members and load a list of them
32        indices=range(s['hits'])
33        self.members=[self.__loadxml(id,i) for i in indices]
34        self.xmldb.release(id)
35       
36    def __loadxml(self,id,i):
37        ''' Returns the i'th xml document name in result set id '''
38        r=self.xmldb.retrieve(id,i)
39        e=ET.fromstring(r)
40        fn=e.findtext('{%s}fileName'%self.ns)
41        return fn.split('.xml')[0]
42   
43    def get(self,uri):
44        ''' Return the actual document corresponding to uri in this directory '''
45        #Use the fact that we know its document name ...
46        fn='%s.xml'%uri
47        return self.xmldb.getDoc(self.targetCollection,fn)
48   
49    def created(self,uri=None):
50        ''' Return the creation date of the collection (if uri is None), or
51        of the specific member within the collection '''
52        q="'%s'"%self.targetCollection
53        if uri is not None: q+=",'%s.xml'"%uri
54        xq="<created>{xmldb:created(%s)}</created>"%q
55        r=self.xmldb.executeChunkedQuery(xq,1,1)
56        e=ET.fromstring(str(r))
57        return e.findtext('created')
58       
59       
60       
61       
62       
Note: See TracBrowser for help on using the repository browser.