source: ndgCommon/trunk/ndg/common/src/clients/http/vocabserverclient.py @ 5083

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/clients/http/vocabserverclient.py@5083
Revision 5083, 4.7 KB checked in by cbyrom, 13 years ago (diff)

Extend VocabServerClient? adding method to retrieve info on a specified
vocab list. Create new data model, CodeTableType? to model the
returned info + simplify setting up object from ET by iterating over object slots + update and add new tests for new functionality.

Line 
1'''
2Simple client to a vocab server - using a POX (plain old XML) interface
3 
4 @author: B Lawrence?, C Byrom, Tessella, Feb 09
5'''
6import logging, urlparse
7from xml.etree import ElementTree as ET
8from generichttpclient import GenericHTTPClient
9from ndg.common.src.models.vocablist import VocabList
10from ndg.common.src.models.ndgObject import ndgObject
11from ndg.common.src.lib.utilities import httpify
12from ndg.common.src.models.codetabletype import CodeTableType
13
14class VocabServerClient(GenericHTTPClient):
15    '''
16    Provides a POX interface to the vocab server
17    '''
18   
19    GET_LIST_QUERY = '%sgetList?recordKey=%s'
20    WHAT_LISTS_QUERY = '%swhatLists'
21   
22    def __init__(self, 
23                 path = "http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/",
24                 proxyServer = None):
25        logging.debug("Instantiating VocabServerClient")
26        self.path = path
27       
28        # extract the hostname - NB, the parser assumes an http prefix so ensure
29        # this is there
30        url = httpify(path)
31        parsedURL = urlparse.urlparse(url)
32        self.ns = "%s://%s/" %(parsedURL.scheme, parsedURL.hostname)
33
34           
35        super(VocabServerClient, self).__init__(proxyServer = proxyServer)
36        logging.debug("VocabServerClient instantiated")
37
38
39    def getRelated(self,subject):
40        '''
41        Get a related record
42        @param subject: subject text to search for in vocab server
43        @return 2D array of results:
44        [0] - broad result matches
45        [1] - narrow result matches
46        [2] - synonym matches
47        TODO: not sure this actually works since it's difficult to get back
48        any meaningful results.  Possibly things are the wrong way around - the
49        results only return the subject - whereas the interesting info is in a
50        parent entry term to this
51        '''
52        logging.debug("Getting vocab data on subject term, '%s'" %subject)
53        url='%sgetRelatedRecordByCriteria?subjectText=%s&predicate=255&inferences=True&objectList=%slist/P211/current'%(self.path,subject,self.ns)
54        doc = self.readURL(url)
55        x=ET.fromstring(doc)
56        b=x.findall('*/{%s}broadMatch' %ndgObject.VOCAB_NS)
57        n=x.findall('*/{%s}narrowMatch' %ndgObject.VOCAB_NS)
58        s=x.findall('*/{%s}exactMatch' %ndgObject.VOCAB_NS)
59        self.broader=[(i.findtext('{%s}entryTerm' %ndgObject.VOCAB_NS)) for i in b]
60        self.narrower=[(i.findtext('{%s}entryTerm' %ndgObject.VOCAB_NS)) for i in n]
61        self.synonyms=[(i.findtext('{%s}entryTerm' %ndgObject.VOCAB_NS)) for i in s]
62        logging.debug("- returning info on subject term")
63        return [self.broader,self.narrower,self.synonyms]
64
65   
66    def getList(self, listKey):
67        '''
68        Retrieve contents of a list - or a specific term in a list
69        @param listKey: key to use to retrieve list - e.g.
70        for list: http://vocab.ndg.nerc.ac.uk/list/P071/11
71        for term: http://vocab.ndg.nerc.ac.uk/term/P071/11/CFV10N55
72        @raise SystemError if the service returns an error
73        @raise IOError if an invalid listKey is provided
74        @return VocabList with all data loaded - or None, if nothing retrieved
75        '''
76        logging.debug("Retrieving list info for key, '%s'" %listKey)
77        url = self.GET_LIST_QUERY%(self.path, listKey)
78        doc = self.readURL(url)
79        et = ET.fromstring(doc)
80       
81        list = VocabList(et = et)
82       
83        # check for errors
84        if list.error != 'false':
85            raise SystemError("Problem occurred whilst looking up vocab data: %s" %list.error)
86       
87        logging.debug("- returning info in Elementtree object")
88        return list
89   
90   
91    def getListInfo(self, listKey):
92        '''
93        Retrieve info on a vocab list
94        @param listKey: url of the vocab list
95        @raise SystemError: if vocab lookup fails
96        @raise ValueError: if no list info found
97        @return CodeTableType with data about specified list
98        '''
99        logging.debug("Retrieving info on list with key, '%s'" %listKey)
100        url = self.WHAT_LISTS_QUERY %(self.path)
101        doc = self.readURL(url)
102        et = ET.fromstring(doc)
103
104        error = et.findtext('{%s}error' %ndgObject.VOCAB_NS)
105        if error != 'false':
106            raise SystemError("Problem occurred whilst looking up vocab data: %s" %list.error)
107
108        recordElements = et.findall('{%s}codeTableType' %ndgObject.VOCAB_NS)
109       
110        self.records = []
111        for record in recordElements:
112            ctr = CodeTableType(et = record)
113            if ctr.listKey == listKey:
114                logging.debug("- found the specified list - returning data as CodeTableType")
115                return ctr
116           
117        raise ValueError("Vocab list, '%s' not found" %listKey)       
118       
119       
Note: See TracBrowser for help on using the repository browser.