Ignore:
Timestamp:
1 Feb 2010, 16:24:53 (15 years ago)
Author:
uli
Message:

Remove quick-import functionality from datacenter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/datacenter.py

    r4920 r4961  
    77import os
    88import shutil
    9 import struct
    109import grok
    1110from datetime import datetime
    12 from zope.component import getMultiAdapter
    1311from 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)
     12from waeup.sirp.interfaces import (IDataCenter, IDataCenterFile,
     13                                   IDataCenterStorageMovedEvent)
    1814from waeup.sirp.utils.helpers import copyFileSystemTree
    1915
     
    7369        return
    7470   
    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                 break
    82             curr_obj = getattr(curr_obj, '__parent__', None)
    83         return receivers
    84 
    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 adapter
    101            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                     pass
    116         return result
    117    
    11871    def getFiles(self, sort='name'):
    11972        """Get a list of files stored in `storage`.
     
    171124        return not_copied
    172125
    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 that
    177         contains value items.
    178         """
    179         result = []
    180         for attr_name in dir(obj):
    181             if attr_name.startswith('_'):
    182                 continue
    183             try:
    184                 attr = getattr(obj, attr_name)
    185                 # This might fail...
    186                 #IWAeUPCSVImporter(attr)
    187                 result.append(attr)
    188             except:
    189                 pass
    190         if hasattr(obj, 'values'):
    191             try:
    192                 result.extend(obj.values())
    193             except:
    194                 pass
    195         return result
    196 
    197     def getPossibleImports(self):
    198         """Get list of possible imports.
    199 
    200         A single import is defined as a tuple
    201        
    202           ( <file-descr>, <importers> )
    203        
    204         where ``<file-descr>`` is an `IDataCenterFile` object and
    205         ``<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                     continue
    214                 importer_context = (
    215                     importer, self.getReceiverId(importer.receiver))
    216                 possible_importers.append(importer_context)
    217             if len(possible_importers) == 0:
    218                 continue
    219             result.append((filedescr, possible_importers))
    220         return result
    221 
    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 in
    229         memory only and will vanish as soon as the request is over. In
    230         this case we can use the memory address of it.
    231 
    232         Both, the id() result and the ZODB oid, are actually integers
    233         which we return as strings. To make the difference chrystal
    234         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 ZODB
    241             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         return
    258 
    259126    def _moveFile(self, source, dest):
    260127        """Move file source to dest preserving ctime, mtime, etc.
     
    422289    def getSourcePath(self):
    423290        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
    434292
    435293class DataCenterStorageMovedEvent(ObjectEvent):
Note: See TracChangeset for help on using the changeset viewer.