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

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

Move code to augment atom data when doing the atom to DIF xquery
transform - from xml_creation package to ndgUtils - putting in a new
package here. Also, move the organisation data, used in this
process, to new 'resource' dir in ndgUtils.

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    # these are the different schemae needed to validate the atoms - NB, the main
10    # one is molesAtom1.0 - this imports the others
11    ATOM_MOLES_SCHEMA = "molesAtom1.0"
12    MOLES_SCHEMA = "moles2.0"
13    ATOM_SCHEMA = "atom1.0"
14   
15    def __init__(self,directory='xquery'):
16        ''' Loads up xqueries from the internal package directory '''
17        files=pkg_resources.resource_listdir('ndgUtils',directory)
18        # this next bit to get rid of .svn and anything else in testing
19        xq=[]
20        xsd = []
21        for f in files:
22            if f.find('.xq') != -1: 
23                xq.append(f)
24            elif f.find('.xsd') != -1:
25                xsd.append(f)
26               
27        #remember that .rstrip doesn't quite do what you think it does :-)
28        self.xq = dict([(re.sub('.xq\Z','',f),\
29                       pkg_resources.resource_string('ndgUtils','%s/%s'%(directory,f))) for f in xq])
30        self.xsd = dict([(re.sub('.xsd\Z','',f),\
31               pkg_resources.resource_string('ndgUtils',\
32                                             '%s/%s'%(directory, f))) for f in xsd])
33       
34        # add the various xquery libraries, too
35        self.xqlib=dict()
36        dirs = ['xqueryLib/Vocabs', 'xqueryLib/Utilities', 'xqueryLib/StubB']
37        for dir in dirs:
38            files = pkg_resources.resource_listdir('ndgUtils', dir)
39            for f in files:
40                if f.find('.xquery') == -1: 
41                    continue
42
43                self.xqlib[f] = pkg_resources.resource_string('ndgUtils','%s/%s'%(dir,f))
44       
45        # also add the organisation data files - NB, this class should be generalised
46        # to make more sense...
47        files = pkg_resources.resource_listdir('ndgUtils', 'resources')
48        self.resources = {}
49        for f in files:
50            if f.find('.xml') == -1: 
51                continue
52            self.resources[f] = pkg_resources.resource_string('ndgUtils','resources/%s'%f)
53       
54    def __setitem__(self,key,value):
55        ''' Add a new xquery to the xquery database dynamically'''
56        self.xq[key]=value
57    def __getitem__(self,key):
58        ''' Given a key, return an xquery '''
59        return self.xq[key]
60   
61    def getSchema(self, schemaName):
62        '''
63        Return the specified schema
64        @param schemaName: Name of schema to retrieve - NB, use the class constants to specify the schemaName
65        - e.g. self.ATOM_MOLES_SCHEMA
66        @return: schema in string format
67        '''
68        schema =  self.xsd.get(schemaName)
69        if not schema:
70            raise ValueError("Unrecognised schema, '%s'" %schemaName)
71       
72        return schema
73       
74       
75    def keys(self):
76        ''' Return a list of all the xqueries loaded into this instance '''
77        return self.xq.keys()
78    def help(self,key):
79        ''' Return the comment string at the top of the xquery (if it exists) '''
80        xq=self.xq[key]
81        s=xq.find('(:')
82        e=xq.find(':)')
83        if s!=-1 and e>s: 
84            return xq[s+2:e]
85        else: return 'No help available for [%s]'%key
86       
87       
88    def actual(self,key,targetCollection,repositoryID,localID):
89        ''' Return an actual xquery targetted for a specific localID, repositoryID and
90        targetCollection '''
91        xq=self.xq[key]
92        xq=xq.replace('RepositoryID',repositoryID)
93        xq=xq.replace('LocalID',localID)
94        xq=xq.replace('TargetCollection',targetCollection)
95        return xq
Note: See TracBrowser for help on using the repository browser.