Skip to content

Context/config/state documentation. #320

Open
@thebjorn

Description

@thebjorn

The Context documentation (http://docs.pyinvoke.org/en/308/concepts/context.html) is a bit minimal, and my search of github for examples didn't yield much enlightenment either... Would it be possible to expand the documentation with some examples/best practices/common usage patterns?

I've started my own library of tasks (https://github.com/datakortet/dk-tasklib), and I seem to be increasingly confused around the configuration/state handling.

As an example, I would like to write a task to compile .less files into .css files and place them into the correct structure for django, e.g. simplified:

@task
def build_less(ctx, input_dir=None, input_fname=None, output_dir=None, output_fname=None):

    input_dir = input_dir or ctx.lessc.input_dir.format(**ctx)
    input_fname = input_fname or ctx.lessc.input_fname.format(**ctx)
    input_path = os.path.join(input_dir, input_fname)

    output_dir = output_dir or ctx.lessc.output_dir.format(**ctx)
    output_fname = output_fname or ctx.lessc.output_fname.format(**ctx)
    output_path = os.path.join(ctx.lessc.build_dir, output_fname)

    ctx.run('lessc --autoprefix="last 4 versions" {} {}'.format(input_path, output_path))
    return output_path

ns = Collection('lessc', build_less)
ns.configure({
    'pkg': {
        'name': '<you need to configure pkg.name>',
        'version': '<you need to configure pkg.version>',
    },
    'lessc': {
        'input_dir': 'less',
        'input_fname': '{pkg.name}.less',
        'output_dir': 'static/{pkg.name}/css/',
        'output_fname': '{pkg.name}.css',
    }
})

i.e. it will look for the root less file is in less/foo.less and place the output in static/foo/css/foo.css (for a Django app named 'foo').

When using this task for the 'foo' app, foo/tasks.py would then look like:

from invoke import ctask as task, collection, config
from dktasklib import lessc
# other imports

@task
def build_all(ctx):
    lessc.build_less(ctx)
    # other tasks

ns = collection.Collection(build_all, lessc)
ns.configure({
    'pkg': {
        'name': "foo",
        'version': "1.0.1"
    }
})

i.e. overriding the pkg subtree of the configuration but leaving everything else at their default values.

The problem is that the lessc key doesn't exist when I run inv build_all(*) -- while inv lessc.build_less works as I would expect (ie. it picks up the pkg key from foo/tasks.py and the lessc key from dktasklib/lessc.py.

(*) the error I'm getting is

    input_dir = input_dir or ctx.lessc.input_dir.format(**ctx)
  File "w:\srv\venv\dev\lib\site-packages\invoke\config.py", line 81, in __getattr__
    raise AttributeError(err)
AttributeError: No attribute or config key found for 'lessc'

Valid keys: ['pkg', 'run', 'tasks']

Valid real attributes: ['from_data', 'run']

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions