2

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Narayanan
  • 41
  • 3

2 Answers2

2

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')])
An Ky
  • 113
  • 11
1

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.

ayaye
  • 441
  • 2
  • 5
  • Thanks for replying. – Narayanan Jan 25 '17 at 04:49
  • 1
    In the function you defined def dictionary_value(), it takes a standard argument called props. But what if I want to pass my own arguments? I would like to pass the key as an argument so that I can call the same function with different keys. – Narayanan Jan 25 '17 at 04:50
  • Narayanan, you can set key to other property: mydictionary = props.getProperty('mydictionary') mykey = props.getProperty('mykey') return mydictionary[mykey] – ayaye Jan 25 '17 at 13:43