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

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

Add help page to application + tidy up codebase remove uneccessary code
and fixing name references, etc.

  • 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"
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 object
41        '''
42        logging.debug("Initialising new ProviderInfo document object")
43
44        logging.debug("Initialising fields")
45        super(ProviderInfo, self).__init__(et = et) 
46        logging.debug("Fields initialised")
47       
48        logging.debug("ProviderInfo initialised")
49
50
51
52    def setData(self, providerName, repositoryInfos):
53        '''
54        Simple method to set data on object
55        @param providerName: Name of the provider - NB, this should equal their login ID
56        @param repositoryInfos:  RepositoryInfo objects with data on the providers repositories
57        '''
58        logging.debug("Setting data on ProviderInfo document object")
59        self.name = providerName
60        self.oldName = self.name
61        self.repositoryInfos = repositoryInfos
62        logging.debug("ProviderInfo data set")
63
64
65    def fromET(self, et):
66        '''
67        Extract data from an elementtree object and add to ProviderInfo object
68       
69        @param et: Elementtree object representing a ProviderInfo
70        '''
71        logging.debug("Adding data from elementtree entry element")
72        if not ET.iselement(et):
73            raise ValueError("Input et [%s] is not an elementtree Element object" %et)
74
75        self.name = et.attrib.get(NAME_ATT_NAME)
76        self.oldName = self.name
77       
78        repositoryInfos = et.findall(RI_ROOT_ELEMENT_NAME)
79       
80        self.repositoryInfos = []
81        if repositoryInfos:
82            for ri in repositoryInfos:
83                self.repositoryInfos.append(RepositoryInfo(et = ri))
84       
85        logging.debug("Data added from elementtree")
86   
87   
88    def toET(self):
89        '''
90        Create an Elementtree object representing the data held by the provider info
91        object.
92       
93        @return Elementree representing the ProviderInfo data
94        '''
95        logging.debug("Creating Elementtree Elements from ProviderInfo data")
96        root = ET.Element(PI_ROOT_EL_NAME)
97        root.attrib[NAME_ATT_NAME] = self.name
98       
99        if self.repositoryInfos:
100            for ri in self.repositoryInfos:
101                root.append(ri.toET())
102
103        logging.debug("Returning the array of Elementtree Elements created")
104        return root
105   
106   
107    def addRepositoryInfo(self, repositoryInfo):
108        '''
109        Add the input RepositoryInfo info data to the provider info; if this
110        info exists already (i.e. a RepositoryInfo object with the same name), this
111        will be replaced with the input data
112        @param repositoryInfo: RepositoryInfo data to add
113        '''
114        logging.debug("Adding repository info to provider info")
115        ris = []
116        isNew = True
117        for ri in self.repositoryInfos:
118            if ri.name == repositoryInfo.name:
119                logging.debug("- updating existing provider info")
120               
121                ri = repositoryInfo
122                ri.version = str(int(ri.version) + 1)
123                isNew = False
124
125            ris.append(pi)
126
127        self.repositoryInfos = pis
128        if isNew:
129            self.repositoryInfos.append(repositoryInfo)
130       
131        logging.debug("- repository info added")
132
133   
134    def removeRepositoryInfo(self, repositoryInfo):
135        '''
136        Remove a Repository Info record from the collection
137        @param repositoryInfo: a RepositoryInfo to remove from the collection
138        '''
139        logging.debug("Removing repository info, '%s', from collection" %repositoryInfo)
140
141        ris = []
142        for ri in self.repositoryInfos:
143            # NB, just skip the ri to be removed
144            if ri.name == repositoryInfo.name:
145                logging.debug("- removing existing provider info")
146            else:
147                ris.append(ri)
148
149        self.repositoryInfos = ris
150        logging.debug("- repository info removed")
151
152
153    def getRepositoryInfoByName(self, repositoryInfoName):
154        '''
155        Return repository info record with the specified name
156        @param repositoryInfoName: name of repository info record to return
157        @raise ValueError: if name not found
158        @return RepositoryInfo object with matching name
159        '''
160        logging.debug("Retrieving provider info with name, '%s'" %repositoryInfoName)
161
162        for ri in self.repositoryInfos:
163            if ri.name == repositoryInfoName:
164                logging.debug("- returning repository info with matching name")
165                return ri
166               
167        raise ValueError("Repository info with name, '%s' not found in collection" %repositoryInfoName)
Note: See TracBrowser for help on using the repository browser.