Narayanan
Narayanan

Reputation: 41

How to access a property variable of buildbot which is a dictionary?

I have a buildbot property which I believe is a dictionary. It appears on the build page like this:

Buildbot property description

This property was set by an extract_fn which was returning a string converted to a dictionary.

My question is: How do I access this property as a key value pair?

For e.g.: Can I do Property('mydictionary[\'aaa\']') ? This doesn't seem to work. I need to access mydictionary['aaa'] in a build step.

Upvotes: 2

Views: 1291

Answers (2)

An Ky
An Ky

Reputation: 133

seems a bit late but I came through a google link so maybe it will help others.

To archive passing further arguments like a key as Narayanan wants, the first way is by using the .withArgs function as it is describe in the docs

@util.renderer
def dictionary_value(props, key):
    mydictionary = props.getProperty('mydictionary')
    return mydictionary[key]

# pass value for key like this
f.addStep(steps.ShellCommand(command=dictionary_value.withArgs('aaa'))) 

withArgs accepts a *args or *kwargs argument so it is pretty flexible.

The other way would be to use a Custom Renderables which implement the IRenderable interface and overwrites its getRenderingFor method

import time
from buildbot.interfaces import IRenderable
from zope.interface import implementer

@implementer(IRenderable)
class FromDict(object):
    def __init__(self, key, default = None):
      self.key = key
      self.default = default
      
    def getRenderingFor(self, props):
       return props.getPropety('mydictionary', {}).value(key, default)

# When used, the renderer must be initialized and 
# the parameters can be passed to its constructor
ShellCommand(command=['echo', FromDict(key='aaa', default='42')])

Upvotes: 2

ayaye
ayaye

Reputation: 451

Property() function takes property name as parameter. You need Renderer which allows to use arbitrary Python code. For example, write renderer:

@util.renderer
def dictionary_value(props):
    mydictionary = props.getProperty('mydictionary')
    return mydictionary['aaa']

And use function name dictionary_value in any place where Property() or Interpolate() could be used.

Upvotes: 1

Related Questions