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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgXqueries.py@3840
Revision 3840, 2.3 KB checked in by cbyrom, 13 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 pkg_resources
2import re
3class ndgXqueries(object):
4    ''' Holds a set of ndg xqueries after having read them from disk (which is
5    done in order to share the xqueries amongst different applications and
6    programming languages). '''
7    def __init__(self,directory='xquery'):
8        ''' Loads up xqueries from the internal package directory '''
9        files=pkg_resources.resource_listdir('ndgUtils',directory)
10        # this next bit to get rid of .svn and anything else in testing
11        xq=[]
12        for f in files:
13            if f.find('.xq')!=-1: xq.append(f)
14        #remember that .rstrip doesn't quite do what you think it does :-)
15        self.xq=dict([(re.sub('.xq\Z','',f),pkg_resources.resource_string('ndgUtils','%s/%s'%(directory,f))) for f in xq])
16       
17        # add the various xquery libraries, too
18        self.xqlib=dict()
19        dirs = ['xqueryLib/Vocabs', 'xqueryLib/Utilities', 'xqueryLib/StubB']
20        for dir in dirs:
21            files = pkg_resources.resource_listdir('ndgUtils', dir)
22            for f in files:
23                if f.find('.xquery') == -1: 
24                    continue
25
26                #re.sub('.xquery\Z','',f)] = \
27                self.xqlib[f] = pkg_resources.resource_string('ndgUtils','%s/%s'%(dir,f))
28           
29    def __setitem__(self,key,value):
30        ''' Add a new xquery to the xquery database dynamically'''
31        self.xq[key]=value
32    def __getitem__(self,key):
33        ''' Given a key, return an xquery '''
34        return self.xq[key]
35    def keys(self):
36        ''' Return a list of all the xqueries loaded into this instance '''
37        return self.xq.keys()
38    def help(self,key):
39        ''' Return the comment string at the top of the xquery (if it exists) '''
40        xq=self.xq[key]
41        s=xq.find('(:')
42        e=xq.find(':)')
43        if s!=-1 and e>s: 
44            return xq[s+2:e]
45        else: return 'No help available for [%s]'%key
46    def actual(self,key,targetCollection,repositoryID,localID):
47        ''' Return an actual xquery targetted for a specific localID, repositoryID and
48        targetCollection '''
49        xq=self.xq[key]
50        xq=xq.replace('RepositoryID',repositoryID)
51        xq=xq.replace('LocalID',localID)
52        xq=xq.replace('TargetCollection',targetCollection)
53        return xq
Note: See TracBrowser for help on using the repository browser.