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

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

Add code to retrieve repository info by name - and add appropriate tests.

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