Changeset 5083 for ndgCommon


Ignore:
Timestamp:
09/03/09 10:31:08 (10 years ago)
Author:
cbyrom
Message:

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.

Location:
ndgCommon/trunk/ndg/common
Files:
3 added
5 edited

Legend:

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

    r5058 r5083  
    1010from ndg.common.src.models.ndgObject import ndgObject 
    1111from ndg.common.src.lib.utilities import httpify 
     12from ndg.common.src.models.codetabletype import CodeTableType 
    1213 
    1314class VocabServerClient(GenericHTTPClient): 
     
    1516    Provides a POX interface to the vocab server  
    1617    ''' 
     18     
     19    GET_LIST_QUERY = '%sgetList?recordKey=%s' 
     20    WHAT_LISTS_QUERY = '%swhatLists' 
     21     
    1722    def __init__(self,  
    1823                 path = "http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/", 
     
    7075        ''' 
    7176        logging.debug("Retrieving list info for key, '%s'" %listKey) 
    72         url = '%sgetList?recordKey=%s'%(self.path, listKey) 
     77        url = self.GET_LIST_QUERY%(self.path, listKey) 
    7378        doc = self.readURL(url) 
    7479        et = ET.fromstring(doc) 
     
    7883        # check for errors 
    7984        if list.error != 'false': 
    80             raise SystemError("Problem occurred whilst looking up vocab data: error") 
     85            raise SystemError("Problem occurred whilst looking up vocab data: %s" %list.error) 
    8186         
    8287        logging.debug("- returning info in Elementtree object") 
    8388        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         
  • ndgCommon/trunk/ndg/common/src/models/codetablerecord.py

    r5058 r5083  
    11''' 
    2  Class representing vocab list data - with access methods for the different records 
     2 Class representing vocab list data - the individual terms in these lists are  
     3 represented as code table records 
    34   
    45 @author: C Byrom, Tessella Feb 2009 
     
    1011class CodeTableRecord(object): 
    1112     
     13    __slots__ = ['listKey', 'entryKey', 'entryTerm', 'entryTermAbbr', 
     14                 'entryTermDef', 'entryTermLastMod'] 
    1215    def __init__(self, et = None): 
    1316        ''' 
    14         Constructor - initialise the atom variables 
     17        Constructor - initialise the code table record variables 
    1518        ''' 
    1619        logging.info("Initialising CodeTableRecord") 
    17  
    18         # an array of CodeTableRecord objects 
    19         self.records = [] 
    20  
    21         # error messages associated with the list retrieval 
    22         self.error = None 
     20        for att in self.__slots__: 
     21            setattr(self, att, '') 
    2322 
    2423        if et: 
     
    2928    def fromET(self, tree): 
    3029        ''' 
    31         Initialise VocabList data using an elementtree 
     30        Initialise CodeTableRecord data using an elementtree 
    3231        @param tree: ElementTree with vocab list data 
    3332        ''' 
    3433        logging.info("Ingesting data from ElementTree object") 
    35         self.listKey = tree.findtext('{%s}listKey' %ndgObject.VOCAB_NS) 
    36         self.entryKey = tree.findtext('{%s}entryKey' %ndgObject.VOCAB_NS) 
    37         self.entryTerm = tree.findtext('{%s}entryTerm' %ndgObject.VOCAB_NS) 
    38         self.entryTermAbbr = tree.findtext('{%s}entryTermAbbr' %ndgObject.VOCAB_NS) 
    39         self.entryTermDef = tree.findtext('{%s}entryTermDef' %ndgObject.VOCAB_NS) 
    40         self.entryTermLastMod = tree.findtext('{%s}entryTermLastMod' %ndgObject.VOCAB_NS) 
     34         
     35        for att in self.__slots__: 
     36            setattr(self, att, tree.findtext('{%s}%s' %(ndgObject.VOCAB_NS, att))) 
    4137        logging.info("Data ingested from tree") 
     38         
     39         
  • ndgCommon/trunk/ndg/common/unittests/clients/http/testvocabserverclient.py

    r5058 r5083  
    5555         
    5656         
     57    def testGetListInfo(self): 
     58        info = self.client.getListInfo(tc.STANDARD_NAMES_LIST) 
     59        self.assertEquals(tc.SN_LIST_MOD_DATE, info.listLastMod,  
     60                          "The list used by this test has changed; update the values in testdata.py to match the new ones") 
     61        self.assertEquals(tc.STANDARD_NAMES_LIST, info.listKey) 
     62        self.assertEquals(tc.SN_LIST_DEF, info.listDefinition) 
     63        self.assertEquals(tc.SN_LIST_VERSION, info.listVersion) 
     64        self.assertEquals(tc.SN_LIST_LONG_NAME, info.listLongName) 
     65        self.assertEquals(tc.SN_LIST_SHORT_NAME, info.listShortName) 
     66 
  • ndgCommon/trunk/ndg/common/unittests/models/testatom.py

    r5012 r5083  
    392392        self.assertFalse(atom.deployments) 
    393393 
     394    def testGetSubTypePrettyNameUndefined(self): 
     395        self.atom.fromString(tc.xmlString) 
     396        self.assertEquals(self.atom.SUB_TYPE_NOT_DEFINED_NAME, self.atom.getSubTypePrettyName()) 
     397 
     398    def testGetSubTypePrettyNameDefined(self): 
     399        self.atom.fromString(tc.xmlString) 
     400        self.atom.subtypeID = VTD.DEPLOYMENT_TERM 
     401        self.assertEquals(VTD.DEPLOYMENT_TERM, self.atom.getSubTypePrettyName()) 
    394402         
    395403    def tearDown(self): 
  • ndgCommon/trunk/ndg/common/unittests/testconstants.py

    r5058 r5083  
    3131parentDir = '..' + os.sep 
    3232TEST_DATA_DIR = getTestDataDir('testdata' + os.sep) 
    33  
    3433 
    3534TEST_COLLECTION_PATH = '/db/testcollection' 
     
    210209 
    211210STANDARD_NAMES_LIST = "http://vocab.ndg.nerc.ac.uk/list/P071/11" 
     211STANDARD_NAME_TERM = "http://vocab.ndg.nerc.ac.uk/term/P071/11/CFSN0012" 
     212SN_TERM_NAME = "aerosol_angstrom_exponent" 
     213SN_ABBREV = "" 
     214SN_DEF = "'Aerosol' means the suspended liquid or solid particles in air (except cloud droplets)." 
     215SN_MOD_DATE = "2006-09-26T17:12:43.000+00:00" 
     216SN_LIST_MOD_DATE = "2008-11-11T16:08:00.000+00:00" 
     217SN_LIST_DEF = "Terms used for definitive but not necessarily comprehensive descriptions of measured phenomena in the CF conventions (a content standard for data stored in NetCDF)." 
     218SN_LIST_VERSION = "11" 
     219SN_LIST_LONG_NAME = "Climate and Forecast Standard Names" 
     220SN_LIST_SHORT_NAME = "CF Standard Names" 
     221SN_LIST_MAIL_GROUP = "test@test.com" 
Note: See TracChangeset for help on using the changeset viewer.