xpybuild.utils.functors

Contains xpybuild.utils.functors.Composable and associated classes for creating late-binding functions/functors.

Composable

class xpybuild.utils.functors.Composable[source]

Bases: object

Base class for objects which are late-bound and can be composed together and with strings, with a resolveToString method to produce the output string.

Compose

class xpybuild.utils.functors.Compose(left, right)[source]

Bases: xpybuild.utils.functors.Composable

A functor that composes other functors or strings

Parameters
  • left – The left object to compose

  • right – The right object to compose

resolveToString(context)[source]

Compose the two objects. Strings as literals, other objects are invoked with context argument.

Parameters

context – a BuildContext

>>> import xpybuild.buildcontext
>>> Compose('a', 'b').resolveToString(None)
'ab'
>>> Compose(Compose('a', 'b'), 'c').resolveToString(None)
'abc'
>>> Compose('a', Compose('b', 'c')).resolveToString(None)
'abc'
>>> Compose(Compose('a', 'b'), Compose('c', 'd')).resolveToString(None)
'abcd'
>>> str("a"+Compose('b', 'c'))
'a+b+c'
>>> ("a"+Compose('b', 'c')).resolveToString(None)
'abc'
>>> str(Compose('a', 'b')+"c")
'a+b+c'
>>> (Compose('a', 'b')+"c").resolveToString(None)
'abc'

ComposableFn

class xpybuild.utils.functors.ComposableFn(fn, *args, **kwargs)[source]

Bases: xpybuild.utils.functors.Composable

A functor wrapping an arbitrary function and arguments that can be composed.

Parameters
  • fn – The function to wrap. Must have the format fn(context, *args).

  • args – Arguments to the function

  • name – optional display name for the functor

resolveToString(context)[source]

Execute the function with the context and arguments as arguments,

Parameters

context – a BuildContext

>>> import xpybuild.buildcontext
>>> ComposableFn(lambda context, a, b: a+b, 'a', 'b').resolveToString(None)
'ab'
>>> str("a"+ComposableFn(lambda context, x: x, 'b'))
'a+<lambda>(b)'
>>> ("a"+ComposableFn(lambda context, x: x, 'b')).resolveToString(None)
'ab'

ComposableWrapper

class xpybuild.utils.functors.ComposableWrapper(fn, name=None)[source]

Bases: object

A functor which wraps a function and can be curried into a ComposableFunction.

Parameters

fn – a function of the form fn(context, *args)

makeFunctor

xpybuild.utils.functors.makeFunctor(fn, name=None)[source]

Take an arbitrary function and return a functor that can take arbitrary arguments and that can in turn be curried into a composable object for use in property contexts.

Example:

def fn(context, input):
        ... # do something
        return input

myfn = makeFunctor(fn)

target = "${OUTPUT_DIR}/" + myfn("${MYVAR}")

This will execute fn(context, "${MYVAR}") at property expansion time and then prepend the expanded ${OUTPUT_DIR}/.

Parameters

fn – a function of the form fn(context, *args).

>>> import xpybuild.buildcontext
>>> str(makeFunctor(lambda context, x: context.expandPropertyValues(x))('${INPUT}'))
'<lambda>(${INPUT})'
>>> str(makeFunctor(lambda context, x: context.expandPropertyValues(x), name='foobar')('${INPUT}'))
'foobar(${INPUT})'
>>> makeFunctor(lambda context, x: context.expandPropertyValues(x))('${INPUT}').resolveToString(xpybuild.buildcontext.BaseContext({'INPUT':'foo'}))
'foo'
>>> str("output/"+makeFunctor(lambda context, x: context.expandPropertyValues(x))('${INPUT}'))
'output/+<lambda>(${INPUT})'
>>> ("output/"+makeFunctor(lambda context, x: context.expandPropertyValues(x))('${INPUT}')).resolveToString(xpybuild.buildcontext.BaseContext({'INPUT':'foo'}))
'output/foo'