Changeset 1428 in products


Ignore:
Timestamp:
Dec 6, 2009 10:06:45 AM (17 years ago)
Author:
piv
Message:

add condition option to binary section along with tests

Location:
quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier/binary.py

    r1389 r1428  
    99 
    1010from collective.transmogrifier.interfaces import ISection, ISectionBlueprint 
    11 from collective.transmogrifier.utils import defaultMatcher 
     11from collective.transmogrifier.utils import defaultMatcher, Condition 
    1212 
    1313class FileExporterSection(object): 
     
    2424        #self.excludekey = defaultMatcher(options, 'exclude-key', name, 'excluded_fields') 
    2525        self.excludekey = options.get('exclude-key', '_excluded_fields').strip() 
    26          
     26 
     27        self.exclude_fieldtypes = filter(None, [i.strip() for i in 
     28                                         options.get('exclude-fieldtypes', '').splitlines()]) 
    2729        self.doc = minidom.Document() 
     30        self.condition = Condition(options.get('condition', 'python:True'), 
     31                                   transmogrifier, name, options) 
    2832 
    2933    def __iter__(self): 
     
    4549                for field in schema.keys(): 
    4650                    if obj.isBinary(field): 
     51                        binary_field_names.append(field) 
     52                        if not self.condition(item, context=obj, fname=field): 
     53                            continue 
     54#                        ftype = schema[field].getType() 
     55#                        if ftype in self.exclude_fieldtypes: 
     56#                            print '################################ Skipped %s:%s:%s field extraction' % (path, field, ftype) 
     57#                            continue 
    4758                        fname, ct, data = self.extractFile(obj, field) 
    48                         binary_field_names.append(field) 
    4959                        if data == '': 
    5060                            # empty file fields have no data and we skip them 
     
    141151        self.contextkey = defaultMatcher(options, 'context-key', name, 'import_context') 
    142152 
     153        self.condition = Condition(options.get('condition', 'python:True'), 
     154                                   transmogrifier, name, options) 
     155 
    143156    def __iter__(self): 
    144157        for item in self.previous: 
     
    169182                            if data is None: 
    170183                                continue 
     184                        if not self.condition(item, context=obj, fname=field, 
     185                            filename=fname, data=data, mimetype=ct): 
     186                            continue 
    171187                        mutator = obj.getField(field).getMutator(obj) 
    172188                        mutator(data, filename=fname, mimetype=ct) 
  • quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier/binary.txt

    r1426 r1428  
    1717section. 
    1818 
     19Also this section provides condition option which, if specified, exports/imports 
     20this or another binary field only if condition expression evaluates to true. 
     21 
    1922>>> import pprint 
    2023>>> binary = """ 
     
    3235... [fileexporter] 
    3336... blueprint = quintagroup.transmogrifier.fileexporter 
     37... condition = python:fname != 'image' 
    3438...  
    3539... [dataprinter] 
     
    7478 
    7579TODO: write test for getting data for fields from import context 
     80 
     81The ``condition`` expression hass access to the following: 
     82 
     83=================== ========================================================== 
     84 ``item``            the current pipeline item 
     85 ``transmogrifier``  the transmogrifier 
     86 ``name``            the name of the splitter section 
     87 ``options``         the splitter options 
     88 ``modules``         sys.modules 
     89 ``context``         the current content object 
     90 ``fname``           the name of the field being processed 
     91 ``filename``        the file name binary field is loaded into (import only) 
     92 ``data``            data read from the file (import only) 
     93 ``mimetype``        data mimetype (import only) 
     94=================== ========================================================== 
  • quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier/tests.py

    r1380 r1428  
    824824                return 'binary data' 
    825825            else: 
    826                 return '' 
     826                return 'image' 
    827827 
    828828        def getMutator(self, obj): 
Note: See TracChangeset for help on using the changeset viewer.