Skip to content

Contexts

Tal Liron edited this page Jan 4, 2018 · 7 revisions

Values in a context are always associated with a namespace, so you must specify it:

print ctx.paths.input
if ctx.build.debug: ...
ctx.myproject.myvalue = 'hello'

The context and its values is also always attached to the current thread. (You will probably never need multithreading in your build scripts, but keep that point in mind.)

You can always access the currently attached context by using current_context():

with current_context() as ctx:

When you do this, the ctx is treated as immutable by default: if you try to change its values, you will get an exception. The reason for this is to avoid bugs: unknown code somewhere (hooks, lambdas) that changes values could otherwise lead to unexpected behavior.

Contexts are nested by default. So, if you create a new context, it will inherit all values from the parent context, allowing you to override them but not change them:

def myfunc():
    with new_child_context() as ctx:
        print('myfunc:', ctx.myproject.a)
        ctx.myproject.a = 'hello2'
        ctx.myproject.b = 'hello3'

with new_context() as ctx:
    ctx.myproject.a = 'hello1'
    myfunc()
    print(ctx.myproject.a)
    print(ctx.get('myproject.b'))

This will output:

myfunc: hello1
hello1
None

Again, the reason for this is to avoid bugs: you don't want other parts of your code to mangle your configuration.

The default behavior can be changed: do so only if you know what you're doing.

Clone this wiki locally