Skip to content

Transformer

James Lott edited this page Mar 12, 2018 · 5 revisions

[[TOC]]

Synopsis

from mkproject.transformer import BaseTransformer
from mkproject.transformer import TransformerError

class MyTransformer(BaseTransformer):
  name = 'myname'
  @staticmethod
  def transform(cfg, path, data, meta):
    try:
      # Use values from cfg and/or meta to perform transformations
      # on any or all of data, path, and meta
      self.log('transformed file: %s' % path)
      return path, data, meta
    except EnvironmentError:
      raise TransformerError

BaseTransformer.name

Description

This attribute must be defined in class implementations. This is the name which assets should specify in their pipeline metadata property in order to invoke this transformer.

BaseTransformer.log

This attribute is a function allowing the transformer to output informational messages. The base implementation does not output messages messages anywhere, and this method should be overridden by the instantiator.

BaseTransformer.transform

Spec

BaseTransformer.transform(cfg, path, data, meta)

Description

The purpose of transformers is to provide a method through which assets can be paramaterized/templatized, allowing outputs to be written with project specific values.

This method must be implemented as a static method, as transformer classes are never instantiated. It takes as its parameters a dictionary providing project level configuration data (cfg) and all of the properties of an asset (path, data, and meta). This method can use any of the information it is passed to transform any of the other information it is passed (e.g., data might be a template which can be rendered using the values in cfg). After the asset is transformed, the transformer must return all of the asset properties. If the expected transformation of the asset fails for any reason, the transformer is expected to raise TransformerError.

Clone this wiki locally