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

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

Upgrade the various 'list' xqueries - to nest all results in a single
root element - so that only one document need be retrieved to get all
results + adjust ndgDirectory to cope with processing the new results +
fix the various namespaces mentioned in the codebase to map to the
current atom/moles ones.

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            DIF, MDIP, ATOM, MOLES
10            (Not all supported ...yet )
11            Note that if fast is set true, the members attribute is not populated
12            at instantiation.
13        '''
14       
15        self.members=[]
16        self.entries=[]
17        self.lastUpdated=None
18        self.targetCollection=targetCollection
19        self.docType = docType
20       
21        #get the right namespace
22        self.ns={'DIF':'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/',\
23                 'MOLES':'http://ndg.nerc.ac.uk/moles',\
24                 'MDIP':'http://www.oceannet.org/mdip/xml',\
25                 'ATOM': 'http://www.w3.org/2005/Atom'}[docType]
26        #load all queries
27        queries=ndgXqueries.ndgXqueries()
28        #select the right query according to the docType
29        xqName={'DIF':'DIFList','MOLES':'molesList', \
30                'MDIP':'MDIPList', 'moles':'molesList', \
31                'ATOM': 'atomList'}[docType]
32        self.docType=docType
33        xq=queries[xqName]
34        # set the query up with the correct target collection
35        self.xq=xq.replace('TargetCollection',self.targetCollection)
36       
37        #make sure we have a db available
38        self.xmldb=ndg_eXist(db=existHost)
39     
40        if not fast: m=self.getMembers()
41
42       
43    def getMembers(self):
44        ''' Build the membership of the directory by URI (based on the FILENAME) '''
45        id,s=self.xmldb.executeQuery(self.xq)
46        if s['hits']==0: return []
47        indices=range(s['hits'])
48       
49        doc = self.xmldb.retrieve(id, 0)
50        et = ET.fromstring(doc)
51        for member in et:
52            fn = member.findtext('{%s}fileName'%self.ns)
53            eid = member.findtext('{%s}repositoryID'%self.ns)
54            c = member.findtext('{%s}created'%self.ns)
55            self.members.append({'fileName':fn,'EntryID':eid,'created':c})
56       
57        self.xmldb.release(id)
58        self.files=[i['fileName'] for i in self.members]
59        self.entries=[i['EntryID'] for i in self.members]
60        return self.members
61
62   
63    def get(self,fileName):
64        ''' Return the actual document corresponding to uri in this directory '''
65        #Use the fact that we know its document name ...
66        return self.xmldb.getDoc(self.targetCollection,fileName)
67
68   
69    def created(self,fileName=None):
70        ''' Return the creation date of the collection (if uri is None), or
71        of the specific member within the collection '''
72        q="'%s'"%self.targetCollection
73        if fileName:
74            # if we're dealing with atom docs, these include the full collection
75            # path already - so strip this off
76            if fileName.startswith(self.targetCollection):
77                fileBits = fileName.split('/')
78                fileName = fileBits[-1]
79                tc = '/' + '/'.join(fileBits[0:-1])
80                q = "'%s', '%s'" %(tc, fileName)
81            else:
82                q+=",'%s'"%fileName
83        xq="<created>{xmldb:created(%s)}</created>"%q
84        r=self.xmldb.executeChunkedQuery(xq,1,1)
85        e=ET.fromstring(str(r))
86        return e.findtext('created')
87   
88
89       
90       
91       
Note: See TracBrowser for help on using the repository browser.