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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgXqueries.py@4483
Revision 4483, 3.1 KB checked in by cbyrom, 12 years ago (diff)

Add schemas to ndgXqueries object and add method to retrieve these + tidy up logic in a few other scripts.

Line 
1import pkg_resources
2import re
3class ndgXqueries(object):
4    '''
5    Holds a set of ndg xqueries after having read them from disk (which is
6    done in order to share the xqueries amongst different applications and
7    programming languages).
8    '''
9    ATOM_MOLES_SCHEMA = "molesAtom1.0"
10    def __init__(self,directory='xquery'):
11        ''' Loads up xqueries from the internal package directory '''
12        files=pkg_resources.resource_listdir('ndgUtils',directory)
13        # this next bit to get rid of .svn and anything else in testing
14        xq=[]
15        xsd = []
16        for f in files:
17            if f.find('.xq') != -1: 
18                xq.append(f)
19            elif f.find('.xsd') != -1:
20                xsd.append(f)
21               
22        #remember that .rstrip doesn't quite do what you think it does :-)
23        self.xq = dict([(re.sub('.xq\Z','',f),\
24                       pkg_resources.resource_string('ndgUtils','%s/%s'%(directory,f))) for f in xq])
25        self.xsd = dict([(re.sub('.xsd\Z','',f),\
26               pkg_resources.resource_string('ndgUtils',\
27                                             '%s/%s'%(directory, f))) for f in xsd])
28       
29        # add the various xquery libraries, too
30        self.xqlib=dict()
31        dirs = ['xqueryLib/Vocabs', 'xqueryLib/Utilities', 'xqueryLib/StubB']
32        for dir in dirs:
33            files = pkg_resources.resource_listdir('ndgUtils', dir)
34            for f in files:
35                if f.find('.xquery') == -1: 
36                    continue
37
38                self.xqlib[f] = pkg_resources.resource_string('ndgUtils','%s/%s'%(dir,f))
39       
40    def __setitem__(self,key,value):
41        ''' Add a new xquery to the xquery database dynamically'''
42        self.xq[key]=value
43    def __getitem__(self,key):
44        ''' Given a key, return an xquery '''
45        return self.xq[key]
46   
47    def getSchema(self, schemaName):
48        '''
49        Return the specified schema
50        @param schemaName: Name of schema to retrieve - NB, use the class constants to specify the schemaName
51        - e.g. self.ATOM_MOLES_SCHEMA
52        @return: schema in string format
53        '''
54        schema =  self.xsd.get(schemaName)
55        if not schema:
56            raise ValueError("Unrecognised schema, '%s'" %schemaName)
57       
58        return schema
59       
60       
61    def keys(self):
62        ''' Return a list of all the xqueries loaded into this instance '''
63        return self.xq.keys()
64    def help(self,key):
65        ''' Return the comment string at the top of the xquery (if it exists) '''
66        xq=self.xq[key]
67        s=xq.find('(:')
68        e=xq.find(':)')
69        if s!=-1 and e>s: 
70            return xq[s+2:e]
71        else: return 'No help available for [%s]'%key
72    def actual(self,key,targetCollection,repositoryID,localID):
73        ''' Return an actual xquery targetted for a specific localID, repositoryID and
74        targetCollection '''
75        xq=self.xq[key]
76        xq=xq.replace('RepositoryID',repositoryID)
77        xq=xq.replace('LocalID',localID)
78        xq=xq.replace('TargetCollection',targetCollection)
79        return xq
Note: See TracBrowser for help on using the repository browser.