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

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

This is actually a bit broken, but committed because it will do what Sue needs
for now ...

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,created=0):
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        c=e.findtext('created')
54        #if created:
55        #    return {'fileName':fn,'EntryID':eid,'created':self.created(fn)}
56        #else: return {'fileName':fn,'EntryID':eid}
57        return {'fileName':fn,'EntryID':eid,'created':c}
58   
59    def get(self,fileName):
60        ''' Return the actual document corresponding to uri in this directory '''
61        #Use the fact that we know its document name ...
62        return self.xmldb.getDoc(self.targetCollection,fileName)
63   
64    def created(self,fileName=None):
65        ''' Return the creation date of the collection (if uri is None), or
66        of the specific member within the collection '''
67        q="'%s'"%self.targetCollection
68        if fileName is not None: q+=",'%s'"%fileName
69        xq="<created>{xmldb:created(%s)}</created>"%q
70        r=self.xmldb.executeChunkedQuery(xq,1,1)
71        e=ET.fromstring(str(r))
72        return e.findtext('created')
73   
74
75       
76       
77       
Note: See TracBrowser for help on using the repository browser.