Ignore:
Timestamp:
26 May 2009, 20:07:37 (15 years ago)
Author:
uli
Message:

Make datacenter storage setting more useful, add helper class for imports, add method to get receiver ids.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/datacenter.py

    r4170 r4173  
    55"""
    66import os
     7import shutil
    78import sys
    89import grok
     
    1819    storage = os.path.join(os.path.dirname(__file__), 'files')
    1920
    20     def getImporters(self):
    21         """Get a list of all importers available.
    22 
    23         The search for available importers is done in two steps:
    24 
    25         1) Look for a utility providing ICSVDataReceiver,
    26 
    27         2) For every item of that utility: try to get an adapter
    28            providing IWAeUPCSVImporter.
    29         """
    30         result = []
     21    def getReceivers(self):
    3122        receivers = []
    3223        curr_obj = getattr(self, '__parent__', None)
     
    3627                break
    3728            curr_obj = getattr(curr_obj, '__parent__', None)
     29        return receivers
     30   
     31    def getImporters(self):
     32        """Get a list of all importers available.
     33
     34        The search for available importers is done in two steps:
     35
     36        1) Look for a utility providing ICSVDataReceiver,
     37
     38        2) For every item of that utility: try to get an adapter
     39           providing IWAeUPCSVImporter.
     40        """
     41        result = []
     42        receivers = self.getReceivers()
    3843        for receiver in receivers:
    3944            try:
     
    6368        if not os.path.exists(path):
    6469            raise ValueError('The path given does not exist: %s' % path)
     70        if move is True:
     71            # copy contents of old location to new one
     72            srcdir = self.storage
     73            dstdir = path
     74            for item in os.listdir(srcdir):
     75                if os.path.isdir(item):
     76                    shutil.copytree(os.path.join(srcdir, item),
     77                                    os.path.join(dstdir, item))
     78                else:
     79                    shutil.copy2(os.path.join(srcdir, item), dstdir)
     80       
    6581        self.storage = path
    6682
     
    102118                except:
    103119                    continue
    104                 possible_importers.append(importer)
     120                importer_context = (
     121                    importer, self.getReceiverId(importer.context))
     122                possible_importers.append(importer_context)
    105123            if len(possible_importers) == 0:
    106124                continue
    107125            result.append((filedescr, possible_importers))
    108126        return result
     127
     128    def getReceiverId(self, obj):
     129        """Get a unique id for an object.
     130
     131        If the object is stored in ZODB it should contain a `_p_oid`
     132        attribute, which is guaranteed to be unique over the ZODB.
     133
     134        If the object has no such attribute, then it will be held in
     135        memory only and will vanish as soon as the request is over. In
     136        this case we can use the memory address of it.
     137        """
     138        if hasattr(obj, '_p_oid'):
     139            return getattr(obj, '_p_oid')
     140        return str(id(obj))
    109141
    110142       
     
    213245    def render(self):
    214246        return u'<div class="portlet"><a href="@@settings">Settings</a></div>'
    215    
     247
     248class Import(object):
     249    def __init__(self, filedescr, importers):
     250        self.file = filedescr
     251        self.importers = importers
     252       
    216253class ImportCSVMain(grok.Viewlet):
    217254    grok.viewletmanager(MainArea)
     
    219256    grok.view(ImportCSV)
    220257    grok.require('waeup.manageUniversity')
     258
     259    def getImports(self):
     260        result = []
     261        imports = self.context.getPossibleImports()
     262        result = [Import(x, y) for x, y in imports]
     263        return result
    221264   
    222265    def update(self, *args, **kw):
    223         print "ARGS: ", args, kw
    224266        return super(ImportCSVMain, self).update(*args, **kw)
    225267       
Note: See TracChangeset for help on using the changeset viewer.