loki.transform.transformation

Base class definition for Transformation pipelines.

Classes

Transformation()

Base class for source code transformations that manipulate source items like Subroutine or Module in place via the item.apply(transform) method.

class Transformation

Bases: object

Base class for source code transformations that manipulate source items like Subroutine or Module in place via the item.apply(transform) method.

The source transformations to be applied should be defined in the following class-specific methods:

The generic dispatch mechanism behind the apply() method will ensure that all hierarchies of the data model are traversed and apply the specific method for each level.

Note that in Sourcefile objects, all Module members will be traversed before standalone Subroutine objects.

transform_subroutine(routine, **kwargs)

Defines the transformation to apply to Subroutine items.

For transformations that modify Subroutine objects, this method should be implemented. It gets called via the dispatch method apply().

Parameters:
  • routine (Subroutine) – The subroutine to be transformed.

  • **kwargs (optional) – Keyword arguments for the transformation.

transform_module(module, **kwargs)

Defines the transformation to apply to Module items.

For transformations that modify Module objects, this method should be implemented. It gets called via the dispatch method apply().

Parameters:
  • module (Module) – The module to be transformed.

  • **kwargs (optional) – Keyword arguments for the transformation.

transform_file(sourcefile, **kwargs)

Defines the transformation to apply to Sourcefile items.

For transformations that modify Sourcefile objects, this method should be implemented. It gets called via the dispatch method apply().

Parameters:
  • sourcefile (Sourcefile) – The sourcefile to be transformed.

  • **kwargs (optional) – Keyword arguments for the transformation.

apply(source, post_apply_rescope_symbols=False, **kwargs)

Dispatch method to apply transformation to all source items in source.

It dispatches to one of the type-specific dispatch methods apply_file(), apply_module(), or apply_subroutine().

Parameters:
  • source (Sourcefile or Module or Subroutine) – The source item to transform.

  • post_apply_rescope_symbols (bool, optional) – Call rescope_symbols on source after applying the transformation to clean up any scoping issues.

  • **kwargs (optional) – Keyword arguments that are passed on to the methods defining the actual transformation.

apply_file(sourcefile, **kwargs)

Apply transformation to all items in sourcefile.

This calls transform_file() and dispatches the transformation for all Module and Subroutine objects in the file.

Parameters:
  • sourcefile (Sourcefile) – The file to transform.

  • **kwargs (optional) – Keyword arguments that are passed on to transformation methods.

apply_subroutine(subroutine, **kwargs)

Apply transformation to a given Subroutine object and its members.

This calls transform_subroutine() and dispatches the transformation for all Subroutine members.

Parameters:
  • subroutine (Subroutine) – The subroutine to transform.

  • **kwargs (optional) – Keyword arguments that are passed on to transformation methods.

apply_module(module, **kwargs)

Apply transformation to a given Module object and its members.

This calls transform_module() and dispatches the transformation for all Subroutine members.

Parameters:
  • module (Module) – The module to transform.

  • **kwargs (optional) – Keyword arguments that are passed on to transformation methods.

post_apply(source, rescope_symbols=False)

Dispatch method for actions to be carried out after applying a transformation to source.

It dispatches to one of the type-specific dispatch methods post_apply_file(), post_apply_module(), or post_apply_subroutine().

Parameters:
post_apply_file(sourcefile, rescope_symbols)

Apply actions after applying a transformation to sourcefile.

Parameters:
  • sourcefile (Sourcefile) – The file to transform.

  • rescope_symbols (bool) – Call rescope_symbols on modules and subroutines in sourcefile

post_apply_subroutine(subroutine, rescope_symbols)

Apply actions after applying a transformation to subroutine.

Parameters:
  • subroutine (Subroutine) – The file to transform.

  • rescope_symbols (bool) – Call rescope_symbols on subroutine

post_apply_module(module, rescope_symbols)

Apply actions after applying a transformation to module.

Parameters:
  • module (Module) – The file to transform.

  • rescope_symbols (bool) – Call rescope_symbols on module