Snippets

Writing new Snippets:

The snippets are located in a python module (mind the __init__.py in the folder containing the snippets). In order to function, each snippet need to have 4 specific function:

  1. requirements: a function returning a dictionary with the necessary resource to run the snippet (used to allocate resource in queuing systems)
  2. results: a function accepting a dictionary with the snippet arguments and returning a dictionary listing all the files produced by the execution of the snippet
  3. add_parser: a function that implement the argparse module and defines the command line arguments accepted by the snippet
  4. a function named as the snippet file name (without the .py extension), containing the code for the execution of the tool

A very minimalistic snippets structure


def requirements():
    return({'ncpu': 1, 'time': '00:01:00'})


def results(argv):
    return([])


def add_parser(subparsers, module_name):
    return subparsers.add_parser(module_name, help='test_snippets', add_help=False)


def test_args(parser, subparsers, argv):
    parser.add_argument('--test', dest='test',
                        help='Test metavar', required=True)
    parser.add_argument('-o', dest='opt', type=str,
                        help='test option')
    return parser.parse_args(argv)


def test(parser, subparsers, module_name, argv, profile, log):
    args = test_args(add_parser(subparsers, module_name), subparsers, argv)
    if args.opt:
        greetings = args.opt
    else:
        greetings = 'Hello'
    print(" ".join(map(str, [greetings, args.test])))

An example of snippets using the Environment modules and the profile object:

Programmatic snippets access:

It is also possible to programmatically launch a snippet, within python console or within other python programs.

Although the interface it is not well documented yet.

import os
import argparse
os.environ['PYPE_SNIPPETS'] = 'test/data/snippets/'

from pype.modules import snippets

parser = argparse.ArgumentParser(prog='pype', description='Test')
subparsers = parser.add_subparsers(dest='modules')

snippets.snippets(None, subparsers, None, [
                  '--log', 'test/data/tmp', 'test',
                  '--test', 'World'], 'default')
snippets.snippets(None, subparsers, 'test', [
                  '--log', 'test/data/tmp', 'test',
                  '--test', 'World', '-o', 'Hello!'], 'default')