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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgDirectory.py@3840
Revision 3840, 3.0 KB checked in by cbyrom, 11 years ago (diff)

Adjust ndgutils setup to allow the inclusion of the xquery lib files
+ adjust the ndgXqueries script to allow the easy retrieval of
these files.

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        #load all queries
24        queries=ndgXqueries.ndgXqueries()
25        #select the right query according to the docType
26        xqName={'DIF':'DIFList','MOLES':'molesList', 'MDIP':'MDIPList', 'moles':'molesList'}[docType]
27        self.docType=docType
28        xq=queries[xqName]
29        # set the query up with the correct target collection
30        self.xq=xq.replace('TargetCollection',self.targetCollection)
31       
32        #make sure we have a db available
33        self.xmldb=ndg_eXist(db=existHost)
34     
35        if not fast: m=self.getMembers()
36       
37    def getMembers(self):
38        ''' Build the membership of the directory by URI (based on the FILENAME) '''
39        id,s=self.xmldb.executeQuery(self.xq)
40        if s['hits']==0: return []
41        indices=range(s['hits'])
42        self.members=[self.__loadxml(id,i) for i in indices]
43        self.xmldb.release(id)
44        self.files=[i['fileName'] for i in self.members]
45        self.entries=[i['EntryID'] for i in self.members]
46        return self.members
47       
48    def __loadxml(self,id,i):
49        ''' Returns the i'th xml document name in result set id '''
50        r=self.xmldb.retrieve(id,i)
51        e=ET.fromstring(r)
52        fn=e.findtext('{%s}fileName'%self.ns)
53        eid=e.findtext('{%s}repositoryID'%self.ns)
54        c=e.findtext('{%s}created'%self.ns)
55        return {'fileName':fn,'EntryID':eid,'created':c}
56   
57    def get(self,fileName):
58        ''' Return the actual document corresponding to uri in this directory '''
59        #Use the fact that we know its document name ...
60        return self.xmldb.getDoc(self.targetCollection,fileName)
61   
62    def created(self,fileName=None):
63        ''' Return the creation date of the collection (if uri is None), or
64        of the specific member within the collection '''
65        q="'%s'"%self.targetCollection
66        if fileName is not None: q+=",'%s'"%fileName
67        xq="<created>{xmldb:created(%s)}</created>"%q
68        r=self.xmldb.executeChunkedQuery(xq,1,1)
69        e=ET.fromstring(str(r))
70        return e.findtext('created')
71   
72
73       
74       
75       
Note: See TracBrowser for help on using the repository browser.