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

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

Add new fields to vocab list data model to handle the parser file data.

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        self.parserDataFile = None
31       
32        # these are the users who can edit this list
33        self.editors = []
34
35        if et:
36            self.fromET(et)
37        logging.info("VocabList initialised")     
38
39
40    def fromET(self, tree):
41        '''
42        Initialise VocabList data using an elementtree
43        @param tree: ElementTree with vocab list data
44        '''
45        logging.info("Ingesting data from ElementTree object")
46        recordElements = tree.findall('{%s}codeTableRecord' %ndgObject.VOCAB_NS)
47       
48        self.records = []
49        for record in recordElements:
50            self.records.append(CodeTableRecord(et = record))
51
52        # NB, can get the list URL from one of its terms
53        if self.records:
54            self.url = self.records[0].listKey
55           
56        self.error = tree.findtext('{%s}error' %ndgObject.VOCAB_NS)
57        logging.info("Data ingested from tree")
58
59
60    def setData(self, url, name, editors = [], newsgroupEmail = None,
61                parserDataFile = None):
62        '''
63        Set data on the vocab list data model
64        @param url: the url corresponding to the vocab term list
65        @param name: the name of the list
66        @keyword editors: a list of editors - mapping to the users that can
67        @keyword newsgroupEmail: email of the news group associated with the vocab list
68        edit entries in this list 
69        @keyword parserDataFile: path to the file containing the parser data for
70        generating standard definitions from the vocab list names
71        '''
72        logging.debug("Setting data on VocabList:\nurl = %s \nname = %s \neditors = %s\nParser data file = %s"
73                      %(url, name, editors, parserDataFile))
74        self.url = httpify(url)
75        self.name = name
76        self.editors = editors
77        self.newsgroupEmail = newsgroupEmail
78        self.parserDataFile = parserDataFile
79        logging.debug("Finished setting data on VocabList")
80       
81       
82    def getVocabTermDataByEntryKey(self, entryKey):
83        '''
84        Retrieve vocab data in vocab list with matching entryKey.  If not
85        found, raise an exception - since we should be using the list as the
86        starting point for data retrieval
87        @param entryKey: entry key of vocab term - this is usually the URL
88        to the term - e.g. 'http://vocab.ndg.nerc.ac.uk/term/P021/37/CDTA'
89        @raise ValueError: if term not found
90        @return: CodeTableRecord relating to the vocab term data 
91        '''
92        logging.debug("Looking up vocab term, '%s'" %entryKey)
93        for record in self.records:
94            if record.entryKey == entryKey:
95                logging.debug("- returning vocab term details")
96                return record
97           
98        raise ValueError("Vocab term, '%s' not found in vocab list" %entryKey)
Note: See TracBrowser for help on using the repository browser.