source: ndgCommon/trunk/ndg/common/src/models/vocablist.py @ 5082

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/models/vocablist.py@5082
Revision 5082, 3.3 KB checked in by cbyrom, 11 years ago (diff)

Add new method to VocabList? model to retrieve specified vocab terms
from list + add additional fields for editors and newsgroup emails - to
group related info for the vocab term editor + add new test class to
exercise the data model.

Line 
1'''
2 Class representing vocab list data - with access methods for the different records
3 
4 @author: C Byrom, Tessella Feb 2009
5'''
6from xml.etree import cElementTree as ET
7import logging
8from ndg.common.src.lib.utilities import escapeSpecialCharacters
9from ndg.common.src.models.ndgObject import ndgObject
10from ndg.common.src.models.codetablerecord import CodeTableRecord
11from ndg.common.src.lib.utilities import httpify
12
13class VocabList(object):
14   
15    def __init__(self, et = None):
16        '''
17        Constructor - initialise the vocab list variables
18        '''
19        logging.info("Initialising VocabList")
20
21        # an array of CodeTableRecord objects
22        self.records = []
23
24        # error messages associated with the list retrieval
25        self.error = None
26       
27        self.url = None
28        self.name = None
29        self.newsgroupEmail = ''
30       
31        # these are the users who can edit this list
32        self.editors = []
33
34        if et:
35            self.fromET(et)
36        logging.info("VocabList initialised")     
37
38
39    def fromET(self, tree):
40        '''
41        Initialise VocabList data using an elementtree
42        @param tree: ElementTree with vocab list data
43        '''
44        logging.info("Ingesting data from ElementTree object")
45        recordElements = tree.findall('{%s}codeTableRecord' %ndgObject.VOCAB_NS)
46       
47        self.records = []
48        for record in recordElements:
49            self.records.append(CodeTableRecord(et = record))
50
51        # NB, can get the list URL from one of its terms
52        if self.records:
53            self.url = self.records[0].listKey
54           
55        self.error = tree.findtext('{%s}error' %ndgObject.VOCAB_NS)
56        logging.info("Data ingested from tree")
57
58
59    def setData(self, url, name, editors = [], newsgroupEmail = None):
60        '''
61        Set data on the vocab list data model
62        @param url: the url corresponding to the vocab term list
63        @param name: the name of the list
64        @keyword editors: a list of editors - mapping to the users that can
65        @keyword newsgroupEmail: email of the news group associated with the vocab list
66        edit entries in this list 
67        '''
68        logging.debug("Setting data on VocabList:\nurl = %s \nname = %s \neditors = %s"
69                      %(url, name, editors))
70        self.url = httpify(url)
71        self.name = name
72        self.editors = editors
73        self.newsgroupEmail = newsgroupEmail
74        logging.debug("Finished setting data on VocabList")
75       
76       
77    def getVocabTermDataByEntryKey(self, entryKey):
78        '''
79        Retrieve vocab data in vocab list with matching entryKey.  If not
80        found, raise an exception - since we should be using the list as the
81        starting point for data retrieval
82        @param entryKey: entry key of vocab term - this is usually the URL
83        to the term - e.g. 'http://vocab.ndg.nerc.ac.uk/term/P021/37/CDTA'
84        @raise ValueError: if term not found
85        @return: CodeTableRecord relating to the vocab term data 
86        '''
87        logging.debug("Looking up vocab term, '%s'" %entryKey)
88        for record in self.records:
89            if record.entryKey == entryKey:
90                logging.debug("- returning vocab term details")
91                return record
92           
93        raise ValueError("Vocab term, '%s' not found in vocab list" %entryKey)
Note: See TracBrowser for help on using the repository browser.