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

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

support for mdip and a stub ndgDirectory routine (sue to complete)

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        print fn
48        return self.xmldb.getDoc(self.targetCollection,fn)
49       
50       
51       
Note: See TracBrowser for help on using the repository browser.