Ignore:
Timestamp:
03/03/09 15:14:28 (11 years ago)
Author:
cbyrom
Message:

Extend vocabserver client, providing a method to retrieve a vocab list
+ add new data models to handle the returned info + update interface
details and usage + add new file utility method for creating
directories + extend tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ndgCommon/trunk/ndg/common/src/clients/http/vocabserverclient.py

    r4990 r5058  
    66import logging, urlparse 
    77from 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 
    811from ndg.common.src.lib.utilities import httpify 
    9 from generichttpclient import GenericHTTPClient 
    10      
     12 
    1113class VocabServerClient(GenericHTTPClient): 
    1214    '''  
    1315    Provides a POX interface to the vocab server  
    1416    ''' 
    15     VOCAB_NS = '{urn:vocab/types}' 
    1617    def __init__(self,  
    1718                 path = "http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/", 
     
    4849        doc = self.readURL(url) 
    4950        x=ET.fromstring(doc) 
    50         b=x.findall('*/%sbroadMatch' %self.VOCAB_NS) 
    51         n=x.findall('*/%snarrowMatch' %self.VOCAB_NS) 
    52         s=x.findall('*/%sexactMatch' %self.VOCAB_NS) 
    53         self.broader=[(i.findtext('%sentryTerm' %self.VOCAB_NS)) for i in b] 
    54         self.narrower=[(i.findtext('%sentryTerm' %self.VOCAB_NS)) for i in n] 
    55         self.synonyms=[(i.findtext('%sentryTerm' %self.VOCAB_NS)) for i in s] 
     51        b=x.findall('*/{%s}broadMatch' %ndgObject.VOCAB_NS) 
     52        n=x.findall('*/{%s}narrowMatch' %ndgObject.VOCAB_NS) 
     53        s=x.findall('*/{%s}exactMatch' %ndgObject.VOCAB_NS) 
     54        self.broader=[(i.findtext('{%s}entryTerm' %ndgObject.VOCAB_NS)) for i in b] 
     55        self.narrower=[(i.findtext('{%s}entryTerm' %ndgObject.VOCAB_NS)) for i in n] 
     56        self.synonyms=[(i.findtext('{%s}entryTerm' %ndgObject.VOCAB_NS)) for i in s] 
    5657        logging.debug("- returning info on subject term") 
    5758        return [self.broader,self.narrower,self.synonyms] 
    5859 
     60     
     61    def getList(self, listKey): 
     62        ''' 
     63        Retrieve contents of a list - or a specific term in a list 
     64        @param listKey: key to use to retrieve list - e.g. 
     65        for list: http://vocab.ndg.nerc.ac.uk/list/P071/11 
     66        for term: http://vocab.ndg.nerc.ac.uk/term/P071/11/CFV10N55 
     67        @raise SystemError if the service returns an error 
     68        @raise IOError if an invalid listKey is provided 
     69        @return VocabList with all data loaded - or None, if nothing retrieved 
     70        ''' 
     71        logging.debug("Retrieving list info for key, '%s'" %listKey) 
     72        url = '%sgetList?recordKey=%s'%(self.path, listKey) 
     73        doc = self.readURL(url) 
     74        et = ET.fromstring(doc) 
     75         
     76        list = VocabList(et = et) 
     77         
     78        # check for errors 
     79        if list.error != 'false': 
     80            raise SystemError("Problem occurred whilst looking up vocab data: error") 
     81         
     82        logging.debug("- returning info in Elementtree object") 
     83        return list 
Note: See TracChangeset for help on using the changeset viewer.