Source code for cioprocessor.processors.transform
"""`transform` processor."""
from os.path import dirname, basename, splitext, join, exists
from os.path import normpath, relpath
from ..lib.processor import Processor
# =============================================================================
[docs]class ProcessorTransform(Processor):
"""Class to manage the processor `transform`."""
# -------------------------------------------------------------------------
[docs] def run(self, pbuild):
"""Execute the processor build.
:type pbuild: .lib.pbuild.PBuild
:param pbuild:
Current processor build object.
"""
if not pbuild.files:
return
# Loop over input file
zipped = pbuild.values.get('__zip__')
for num, input_file in enumerate(pbuild.files):
for pbuild.current in pbuild.currents(input_file):
pbuild.progress_file(increase=pbuild.current['step_delta'])
self._process_file(pbuild, zipped)
if not pbuild.current_reset():
break
pbuild.progress_file(current=num + 1)
pbuild.progress_save()
if pbuild.aborted():
break
# -------------------------------------------------------------------------
def _process_file(self, pbuild, zipped):
"""Process one file.
:type pbuild: .lib.pbuild.PBuild
:param pbuild:
Current processor build object.
:param bool zipped:
``True`` if the result has to be zipped.
"""
# Loop over the steps
step = {}
error = None
pbuild.progress_step(0, len(pbuild.steps))
for step in pbuild.steps:
error = self._step.execute(pbuild, step)
if error == 'STOP':
error = None
break
if error is not None:
if step.get('failed') == 'warning':
pbuild.warning(error)
else:
pbuild.error(error)
break
if pbuild.aborted():
break
pbuild.progress_step(increase=1)
if error is not None or pbuild.aborted():
return
# Save the result
if 'output' in self.environment \
and 'file_name' in self.environment['output']:
path = self.environment['output']['file_name'].format(
fid=pbuild.current['file_id'],
ext=splitext(pbuild.current['input_file'])[1],
parent=basename(dirname(pbuild.current['input_file'])),
**pbuild.current['values'])
filename = pbuild.save_dup(path)
if filename is None:
path = normpath(join(pbuild.current['workdir'], path))
if path.startswith(pbuild.directories['ongoing']) \
and exists(path):
filename = relpath(path, pbuild.directories['ongoing'])
if filename is not None and not zipped:
if 'files' not in pbuild.result:
pbuild.result['files'] = []
pbuild.result['files'].append(filename)