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

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

Move VocabList? model to the VocabTermEditor? project, since this is
where it sits most comfortably. Adjust getList method of vocabserverclient to return its data in an elementtree instead of
VocabList? to avoid codebase pollution + move tests + update tests
to cope with loss of VocabList?.

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