source: TI01-discovery/trunk/OAIInfoEditor/oai_info_editor/model/repositoryinfo.py @ 5229

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

Add edit functionality to allow provider info, and associated
repository info to be updated via the web interface. Add code to
extract inputs, using indexing prefixes to allow multiple repository
infos to be specified, then use this to update the backend XML file -
with this being backed up in the process.

  • Property svn:executable set to *
Line 
1'''
2 Class representing a repository info - with functionality to allow data to be
3 validated and edited
4 
5 @author: C Byrom, Tessella Apr 2009
6'''
7from xml.etree import cElementTree as ET
8import logging, urllib, datetime
9from oai_info_editor.lib.constants import *
10from ndg.common.src.models.abstractrecord import AbstractRecord
11from ndg.common.src.lib.utilities import getBool
12
13# values of the XML data structure
14RI_ROOT_ELEMENT_NAME = 'repositoryInfo'
15NAME_ELEMENT_NAME = 'name'
16URL_ELEMENT_NAME = 'url'
17DATA_FORMAT_ELEMENT_NAME = 'dataFormat'
18SET_SPEC_ELEMENT_NAME = 'setSpec'
19MODIFICATION_DATE_ELEMENT_NAME = 'modificationDate'
20VERSION_ELEMENT_NAME = 'version'
21HARVEST_TIME_NAME = 'harvestTime'
22SPLIT_BY_SET_NAME = 'splitBySet'
23
24CHECKBOX_SUFFIX = "checked"
25
26# Delimiter to use when specifying input names - to allow multiple RI's in same
27# input form
28RI_DELIMITER = "---------"
29
30def createRepositoryInfoWithData(repositoryName, repositoryUrl, dataFormat, 
31                                    setSpec, version = '1', modificationDate = None,
32                                    harvestTime = None, splitBySet = False):
33    '''
34    Factory style method for creating RepositoryInfo with initial data
35    @param repositoryName: Name of repository
36    @param repositoryUrl: url of the repository to harvest from
37    @param dataFormat: format of the data being harvested
38    @param version: version number of repository info data. Default = '1'
39    @keyword modificationDate: date of creation/modification of repository info. 
40    Default = None => create date is set at the time of the object being
41    instantiated
42    @keyword harvestTime: time to harvest the repository data - in format HH:mm
43    @keyword splitBySet: if True and if 'setSpec' is set then harvested data
44    will be saved in directories matching the value of 'setSpec'. Default = False
45    '''
46    logging.debug("Creating RepositoryInfo with initial data")
47    ri = RepositoryInfo()
48    ri.setData(repositoryName, repositoryUrl, dataFormat, setSpec,
49               version = version, modificationDate = modificationDate,
50               harvestTime = harvestTime, splitBySet = splitBySet)
51    return ri
52
53
54class RepositoryInfo(AbstractRecord):
55
56    # define the expected object attributes
57    __slots__ = [NAME_ELEMENT_NAME, URL_ELEMENT_NAME, DATA_FORMAT_ELEMENT_NAME,
58                 SET_SPEC_ELEMENT_NAME, MODIFICATION_DATE_ELEMENT_NAME,
59                 HARVEST_TIME_NAME, SPLIT_BY_SET_NAME, VERSION_ELEMENT_NAME]
60
61    def __init__(self, et = None, **inputs):
62        '''
63        Constructor - initialise the RepositoryInfo object
64        @keyword et: elementtree entry representing a Repository Info entry - to
65        read in
66        @param **inputs: dict to pass in attribute/value pairs - will overwrite ET values
67        '''
68        logging.debug("Initialising RepositoryInfo object")
69        logging.debug("Initialising fields")
70
71        super(RepositoryInfo, self).__init__(et = et, **inputs)
72       
73        # don't include mod date when doing an object compare
74        self.ATTS_TO_SKIP.extend(MODIFICATION_DATE_ELEMENT_NAME)
75        if not self.version:
76            self.version = '1'
77
78        # ensure the variable is set to a valid boolean value
79        if self.splitBySet:
80            self.splitBySet = getBool(self.splitBySet)
81        else:
82            self.splitBySet = False
83
84        logging.debug("Fields initialised")
85           
86        logging.debug("RepositoryInfo initialised")
87
88
89    def toET(self):
90        '''
91        Convert the RepositoryInfo data into ElementTree representation and return this
92        @return: ElementTree version of RepositoryInfo
93        '''
94        logging.debug("Creating formatted Elementtree version of RepositoryInfo")
95        rootElement = ET.Element(RI_ROOT_ELEMENT_NAME)
96
97        if self.harvestTime:
98            htEl = ET.SubElement(rootElement, HARVEST_TIME_NAME)
99            htEl.text = self.harvestTime
100           
101        sbsEl = ET.SubElement(rootElement, SPLIT_BY_SET_NAME)
102        sbsEl.text = self.splitBySet
103       
104        if self.name:
105            nameElement = ET.SubElement(rootElement, NAME_ELEMENT_NAME)
106            nameElement.text = self.name
107           
108        if self.url:
109            urlElement = ET.SubElement(rootElement, URL_ELEMENT_NAME)
110            urlElement.text = self.url
111       
112        if self.dataFormat:
113            formatElement = ET.SubElement(rootElement, DATA_FORMAT_ELEMENT_NAME)
114            formatElement.text = self.dataFormat
115           
116        if self.setSpec:
117            setSpecElement = ET.SubElement(rootElement, SET_SPEC_ELEMENT_NAME)
118            setSpecElement.text = self.setSpec
119
120        dateElement = ET.SubElement(rootElement, MODIFICATION_DATE_ELEMENT_NAME)
121        dateElement.text = self.modificationDate
122
123        versionElement = ET.SubElement(rootElement, VERSION_ELEMENT_NAME)
124        versionElement.text = self.version
125
126        logging.debug("Elementtree version of RepositoryInfo created")
127        return rootElement
128
129
130    def fromET(self, et):
131        '''
132        Extract and add data from an elementtree object
133        @param et: Elementtree object representing a Repository Info entry
134        '''
135        logging.debug("Adding data to RepositoryInfo from elementtree entry element")
136        if not ET.iselement(et):
137            raise ValueError("Input et [%s] is not an elementtree Element object" %et)
138
139        self.harvestTime = et.findtext(HARVEST_TIME_NAME) or ''
140        self.splitBySet = et.findtext(SPLIT_BY_SET_NAME) or False
141        self.name = et.findtext(NAME_ELEMENT_NAME) or ''
142        self.url = et.findtext(URL_ELEMENT_NAME) or ''
143        self.dataFormat = et.findtext(DATA_FORMAT_ELEMENT_NAME) or ''
144        self.setSpec = et.findtext(SET_SPEC_ELEMENT_NAME) or ''
145        self.modificationDate = et.findtext(MODIFICATION_DATE_ELEMENT_NAME) or ''
146        self.version = et.findtext(VERSION_ELEMENT_NAME) or '1'
147        logging.debug("Data added from elementtree")
148   
149       
150    def setData(self, repositoryName, repositoryUrl, dataFormat, setSpec, 
151                version = '1', modificationDate = None, harvestTime = None,
152                splitBySet = False):
153        '''
154        Simple method to set data on object
155        @param repositoryName: Name of repository
156        @param repositoryUrl: url of the repository to harvest from
157        @param dataFormat: format of the data being harvested
158        @param version: version number of repository info data. Default = '1'
159        @keyword modificationDate: date of creation/modification of repository info. 
160        Default = None => create date is set at the time of the object being
161        @keyword harvestTime: time to harvest the repository data - in format HH:mm
162        instantiated
163        @keyword splitBySet: if True and if 'setSpec' is set then harvested data
164        will be saved in directories matching the value of 'setSpec'. Default = False
165        '''
166        logging.debug("Setting data on RepositoryInfo object")
167        self.name = repositoryName
168        self.url = repositoryUrl
169        self.dataFormat = dataFormat
170        self.setSpec = setSpec
171       
172        if modificationDate:
173            self.modificationDate = modificationDate
174        else:
175            self.modificationDate = datetime.datetime.now().strftime(DATE_FORMAT)
176           
177        if harvestTime:
178            self.harvestTime = harvestTime
179       
180        self.splitBySet = splitBySet
181       
182        self.version = version
183           
184        logging.debug("RepositoryInfo data set")
Note: See TracBrowser for help on using the repository browser.