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

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

Modifications to ndgDirectory to expose filenames, entries and both more easily

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/'}[docType]
23       
24        #select the query
25        queries=ndgXqueries.ndgXqueries()
26        xqName={'DIF':'DIFList'}[docType]
27        self.docType=docType
28        xq=queries[xqName]
29        self.xq=xq.replace('TargetCollection',self.targetCollection)
30       
31        #make sure we have a db available
32        self.xmldb=ndg_eXist(db=existHost)
33     
34        if not fast: m=self.getMembers()
35       
36    def getMembers(self):
37        ''' Build the membership of the directory by URI (based on the FILENAME) '''
38        id,s=self.xmldb.executeQuery(self.xq)
39        if s['hits']==0: return []
40        indices=range(s['hits'])
41        self.members=[self.__loadxml(id,i) for i in indices]
42        self.xmldb.release(id)
43        self.files=[i['fileName'] for i in self.members]
44        self.entries=[i['EntryID'] for i in self.members]
45        return self.members
46       
47    def __loadxml(self,id,i):
48        ''' Returns the i'th xml document name in result set id '''
49        r=self.xmldb.retrieve(id,i)
50        e=ET.fromstring(r)
51        fn=e.findtext('{%s}fileName'%self.ns)
52        eid=e.findtext('{%s}repositoryID'%self.ns)
53        return {'fileName':fn,'EntryID':eid}
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.