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

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

extended for MDIP and MOLES listing

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',fast=0):
7        ''' Initialise a targetCollection directory of a particular docType.
8        Valid docTypes are:
9            dgObservationStation, dgDataEntity, dgActivity, dgDataProductionTool
10            DIF, MDIP
11            (Not all supported ...yet )
12            Note that if fast is set true, the members attribute is not populated
13            at instantiation.
14        '''
15       
16        self.members=[]
17        self.entries=[]
18        self.lastUpdated=None
19        self.targetCollection=targetCollection
20       
21        #get the right namespace
22        self.ns={'DIF':'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/','MOLES':'http://ndg.nerc.ac.uk/moles','MDIP':'http://www.oceannet.org/mdip/xml'}[docType]
23        #select the query
24        queries=ndgXqueries.ndgXqueries()
25        xqName={'DIF':'DIFList','MOLES':'molesList', 'MDIP':'MDIPList', 'moles':'molesList'}[docType]
26        self.docType=docType
27        xq=queries[xqName]
28        self.xq=xq.replace('TargetCollection',self.targetCollection)
29       
30        #make sure we have a db available
31        self.xmldb=ndg_eXist(db=existHost)
32     
33        if not fast: m=self.getMembers()
34       
35    def getMembers(self):
36        ''' Build the membership of the directory by URI (based on the FILENAME) '''
37        id,s=self.xmldb.executeQuery(self.xq)
38        if s['hits']==0: return []
39        indices=range(s['hits'])
40        self.members=[self.__loadxml(id,i) for i in indices]
41        self.xmldb.release(id)
42        self.files=[i['fileName'] for i in self.members]
43        self.entries=[i['EntryID'] for i in self.members]
44        return self.members
45       
46    def __loadxml(self,id,i):
47        ''' Returns the i'th xml document name in result set id '''
48        r=self.xmldb.retrieve(id,i)
49        e=ET.fromstring(r)
50        fn=e.findtext('{%s}fileName'%self.ns)
51        eid=e.findtext('{%s}repositoryID'%self.ns)
52        c=e.findtext('{%s}created'%self.ns)
53        return {'fileName':fn,'EntryID':eid,'created':c}
54   
55    def get(self,fileName):
56        ''' Return the actual document corresponding to uri in this directory '''
57        #Use the fact that we know its document name ...
58        return self.xmldb.getDoc(self.targetCollection,fileName)
59   
60    def created(self,fileName=None):
61        ''' Return the creation date of the collection (if uri is None), or
62        of the specific member within the collection '''
63        q="'%s'"%self.targetCollection
64        if fileName is not None: q+=",'%s'"%fileName
65        xq="<created>{xmldb:created(%s)}</created>"%q
66        r=self.xmldb.executeChunkedQuery(xq,1,1)
67        e=ET.fromstring(str(r))
68        return e.findtext('created')
69   
70
71       
72       
73       
Note: See TracBrowser for help on using the repository browser.