source: TI01-discovery/trunk/OAIInfoEditor/oai_info_editor/model/providerinfo.py @ 5255

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/OAIInfoEditor/oai_info_editor/model/providerinfo.py@5255
Revision 5255, 6.3 KB checked in by cbyrom, 10 years ago (diff)

Extend OAI Info editor documentation and help pages + improve the
setup config + extend test suite and fix problem with setting
email addresses in providerinfo.

  • Property svn:executable set to *
Line 
1'''
2 Class representing provider info data
3 - effectively gathering a collection of RepositoryInfos together with name
4 information to group them
5 
6 @author: C Byrom, Tessella Apr 2009
7'''
8from xml.etree import cElementTree as ET
9import logging, datetime
10from oai_info_editor.lib.constants import *
11from ndg.common.src.models.abstractrecord import AbstractRecord
12from oai_info_editor.model.repositoryinfo import *
13
14# names for the various XML elements used to house the object field data
15PI_ROOT_EL_NAME = "providerInfo"
16NAME_ATT_NAME = "name"
17EMAIL_ADDRESS_NAME = "email"
18
19
20def createProviderInfoWithData(providerName, repositoryInfos, providerEmail = ''):
21    '''
22    Factory method to create ProviderInfo object with set values
23    @param providerName: Name of the provider - NB, this should equal their login ID
24    @param repositoryInfos:  RepositoryInfo objects with data on the providers repositories
25    @keyword providerEmail: email address for provider
26    @return ProviderInfo with specified data
27    '''
28    pi = ProviderInfo()
29    pi.setData(providerName, repositoryInfos, providerEmail = providerEmail)
30    return pi
31
32
33class ProviderInfo(AbstractRecord):
34   
35    # define the expected object attributes
36    # NB, 'oldName' is used when the provider name is being updated - so the
37    # old record can be effectively updated
38    __slots__ = [NAME_ATT_NAME, EMAIL_ADDRESS_NAME, 'repositoryInfos', 'oldName']
39
40    def __init__(self, et = None):
41        '''
42        Constructor - initialise the ProviderInfo name object
43        @keyword et: elementtree entry representing a ProviderInfo object
44        '''
45        logging.debug("Initialising new ProviderInfo document object")
46
47        logging.debug("Initialising fields")
48        super(ProviderInfo, self).__init__(et = et) 
49        logging.debug("Fields initialised")
50       
51        logging.debug("ProviderInfo initialised")
52
53
54
55    def setData(self, providerName, repositoryInfos, providerEmail = ''):
56        '''
57        Simple method to set data on object
58        @param providerName: Name of the provider - NB, this should equal their login ID
59        @param repositoryInfos:  RepositoryInfo objects with data on the providers repositories
60        @keyword providerEmail: email address for provider. Default = None
61        '''
62        logging.debug("Setting data on ProviderInfo document object")
63        self.name = providerName
64        self.oldName = self.name
65        self.repositoryInfos = repositoryInfos
66        self.email = providerEmail
67        logging.debug("ProviderInfo data set")
68
69
70    def fromET(self, et):
71        '''
72        Extract data from an elementtree object and add to ProviderInfo object
73       
74        @param et: Elementtree object representing a ProviderInfo
75        '''
76        logging.debug("Adding data from elementtree entry element")
77        if not ET.iselement(et):
78            raise ValueError("Input et [%s] is not an elementtree Element object" %et)
79
80        self.name = et.attrib.get(NAME_ATT_NAME)
81        self.email = et.findtext(EMAIL_ADDRESS_NAME) or ''
82        self.oldName = self.name
83       
84        repositoryInfos = et.findall(RI_ROOT_ELEMENT_NAME)
85       
86        self.repositoryInfos = []
87        if repositoryInfos:
88            for ri in repositoryInfos:
89                self.repositoryInfos.append(RepositoryInfo(et = ri))
90       
91        logging.debug("Data added from elementtree")
92   
93   
94    def toET(self):
95        '''
96        Create an Elementtree object representing the data held by the provider info
97        object.
98       
99        @return Elementree representing the ProviderInfo data
100        '''
101        logging.debug("Creating Elementtree Elements from ProviderInfo data")
102        root = ET.Element(PI_ROOT_EL_NAME)
103        root.attrib[NAME_ATT_NAME] = self.name
104
105        if self.email:
106            emailElement = ET.SubElement(root, EMAIL_ADDRESS_NAME)
107            emailElement.text = self.email
108       
109        if self.repositoryInfos:
110            for ri in self.repositoryInfos:
111                root.append(ri.toET())
112
113        logging.debug("Returning the array of Elementtree Elements created")
114        return root
115   
116   
117    def addRepositoryInfo(self, repositoryInfo):
118        '''
119        Add the input RepositoryInfo info data to the provider info; if this
120        info exists already (i.e. a RepositoryInfo object with the same name), this
121        will be replaced with the input data
122        @param repositoryInfo: RepositoryInfo data to add
123        '''
124        logging.debug("Adding repository info to provider info")
125        ris = []
126        isNew = True
127        for ri in self.repositoryInfos:
128            if ri.name == repositoryInfo.name:
129                logging.debug("- updating existing provider info")
130               
131                ri = repositoryInfo
132                ri.version = str(int(ri.version) + 1)
133                isNew = False
134
135            ris.append(pi)
136
137        self.repositoryInfos = pis
138        if isNew:
139            self.repositoryInfos.append(repositoryInfo)
140       
141        logging.debug("- repository info added")
142
143   
144    def removeRepositoryInfo(self, repositoryInfo):
145        '''
146        Remove a Repository Info record from the collection
147        @param repositoryInfo: a RepositoryInfo to remove from the collection
148        '''
149        logging.debug("Removing repository info, '%s', from collection" %repositoryInfo)
150
151        ris = []
152        for ri in self.repositoryInfos:
153            # NB, just skip the ri to be removed
154            if ri.name == repositoryInfo.name:
155                logging.debug("- removing existing provider info")
156            else:
157                ris.append(ri)
158
159        self.repositoryInfos = ris
160        logging.debug("- repository info removed")
161
162
163    def getRepositoryInfoByName(self, repositoryInfoName):
164        '''
165        Return repository info record with the specified name
166        @param repositoryInfoName: name of repository info record to return
167        @raise ValueError: if name not found
168        @return RepositoryInfo object with matching name
169        '''
170        logging.debug("Retrieving provider info with name, '%s'" %repositoryInfoName)
171
172        for ri in self.repositoryInfos:
173            if ri.name == repositoryInfoName:
174                logging.debug("- returning repository info with matching name")
175                return ri
176               
177        raise ValueError("Repository info with name, '%s' not found in collection" %repositoryInfoName)
Note: See TracBrowser for help on using the repository browser.