Changeset 4961 for main/waeup.sirp/trunk/src/waeup/sirp
- Timestamp:
- 1 Feb 2010, 16:24:53 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/datacenter.py
r4920 r4961 7 7 import os 8 8 import shutil 9 import struct10 9 import grok 11 10 from datetime import datetime 12 from zope.component import getMultiAdapter13 11 from zope.component.interfaces import ObjectEvent 14 from waeup.sirp.csvfile import getCSVFile 15 from waeup.sirp.interfaces import (IDataCenter, IWAeUPCSVImporter, 16 ICSVDataReceivers, IDataCenterFile, 17 IDataCenterStorageMovedEvent) 12 from waeup.sirp.interfaces import (IDataCenter, IDataCenterFile, 13 IDataCenterStorageMovedEvent) 18 14 from waeup.sirp.utils.helpers import copyFileSystemTree 19 15 … … 73 69 return 74 70 75 def getReceivers(self):76 receivers = []77 curr_obj = getattr(self, '__parent__', None)78 while curr_obj is not None:79 if ICSVDataReceivers.providedBy(curr_obj):80 receivers = self.getCSVDataReceivers(curr_obj)81 break82 curr_obj = getattr(curr_obj, '__parent__', None)83 return receivers84 85 def getReceiverIds(self):86 """Get a dict of available receivers.87 88 The keys of the result are the receiver ids.89 """90 receivers = self.getReceivers()91 return dict([(self.getReceiverId(x), x) for x in receivers])92 93 def getImporters(self):94 """Get a list of all importers available.95 96 The search for available importers is done in two steps:97 98 1) Look for a utility providing ICSVDataReceiver,99 100 2) For every item of that utility: try to get an adapter101 providing IWAeUPCSVImporter.102 """103 result = []104 receivers = self.getReceivers()105 files = self.getFiles()106 for receiver in receivers:107 for file in files:108 wrapped_file = getCSVFile(file.context)109 try:110 importer = getMultiAdapter((wrapped_file, receiver),111 IWAeUPCSVImporter)112 result.append(importer)113 except:114 # No multi-adapter for this combination available...115 pass116 return result117 118 71 def getFiles(self, sort='name'): 119 72 """Get a list of files stored in `storage`. … … 171 124 return not_copied 172 125 173 def getCSVDataReceivers(self, obj):174 """Get a list of attributes, that can receive CSV data.175 176 We also get a list of values, if obj is a container that177 contains value items.178 """179 result = []180 for attr_name in dir(obj):181 if attr_name.startswith('_'):182 continue183 try:184 attr = getattr(obj, attr_name)185 # This might fail...186 #IWAeUPCSVImporter(attr)187 result.append(attr)188 except:189 pass190 if hasattr(obj, 'values'):191 try:192 result.extend(obj.values())193 except:194 pass195 return result196 197 def getPossibleImports(self):198 """Get list of possible imports.199 200 A single import is defined as a tuple201 202 ( <file-descr>, <importers> )203 204 where ``<file-descr>`` is an `IDataCenterFile` object and205 ``<importers>`` is a list of `IWAeUPCSVImporter` objects.206 """207 result = []208 importers = self.getImporters()209 for filedescr in self.getFiles():210 possible_importers = []211 for importer in importers:212 if importer.csvfile.path != filedescr.context:213 continue214 importer_context = (215 importer, self.getReceiverId(importer.receiver))216 possible_importers.append(importer_context)217 if len(possible_importers) == 0:218 continue219 result.append((filedescr, possible_importers))220 return result221 222 def getReceiverId(self, obj):223 """Get a unique id for an object.224 225 If the object is stored in ZODB it should contain a `_p_oid`226 attribute, which is guaranteed to be unique over the ZODB.227 228 If the object has no such attribute, then it will be held in229 memory only and will vanish as soon as the request is over. In230 this case we can use the memory address of it.231 232 Both, the id() result and the ZODB oid, are actually integers233 which we return as strings. To make the difference chrystal234 clear, we prepend ``z`` to ids taken from ZODB oids.235 """236 if not hasattr(obj, '_p_oid'):237 return str(id(obj))238 oid = getattr(obj, '_p_oid')239 if oid is None:240 # The persistent object is not stored in the ZODB241 return str(id(obj))242 return 'z%s' % struct.unpack('>Q', oid)[0]243 244 def doImport(self, path, receiverid, clear=None):245 receivers = self.getReceiverIds()246 if receiverid not in receivers.keys():247 raise ValueError('The requested data receiver cannot be found.')248 receiver = receivers[receiverid]249 filewrapper = getCSVFile(path)250 if filewrapper is None:251 raise ValueError('Format of CSV file not supported.')252 importer = getMultiAdapter((filewrapper, receiver), IWAeUPCSVImporter)253 if clear is not None:254 importer.doImport(clear_old_data=clear)255 else:256 importer.doImport()257 return258 259 126 def _moveFile(self, source, dest): 260 127 """Move file source to dest preserving ctime, mtime, etc. … … 422 289 def getSourcePath(self): 423 290 return self._getMarker('source') or None 424 425 class Import(object): 426 """Helper class to aggregate imports and their data. 427 """ 428 def __init__(self, filedescr, importers): 429 self.file = filedescr 430 self.importers = [] 431 for importer, receiverid in importers: 432 importer.receiverid = receiverid 433 self.importers.append(importer) 291 434 292 435 293 class DataCenterStorageMovedEvent(ObjectEvent):
Note: See TracChangeset for help on using the changeset viewer.