Skip to content

Commit

Permalink
ChoiceStringParameter.getChoices: use a member function instead of Dy…
Browse files Browse the repository at this point in the history
…namicStringChoiceParameter
  • Loading branch information
Jared Grubb committed Feb 24, 2013
1 parent ff57453 commit 1076819
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 89 deletions.
37 changes: 7 additions & 30 deletions master/buildbot/schedulers/forcesched.py
Expand Up @@ -209,41 +209,18 @@ def parse_from_arg(self, s):
raise ValidationError("'%s' does not belongs to list of available choices '%s'"%(s, self.choices))
return s

def getChoices(self, master, scheduler, buildername):
return self.choices

class DynamicChoiceStringParameter(ChoiceStringParameter):
"""A list of strings, allowing the selection of one of the predefined values.
The 'strict' parameter controls whether values outside the predefined list
of choices are allowed"""
type = ChoiceStringParameter.type + ["dynamic"]

# Assume not strict. In general, there is no "session" to remember what each
# user was presented with, so there is no way to validate a user's selection.
# Custom validation can be implemented in a derived 'updateFromKwargs', if desired.
strict = False

# A functor to return the choices available. This will be called for each
# form generation, and the first element will be presented as the default
# choice.
dynamicChoices = lambda masterStatus, buildername: []


class InheritBuildParameter(DynamicChoiceStringParameter):
class InheritBuildParameter(ChoiceStringParameter):
"""A parameter that takes its values from another build"""
type = DynamicChoiceStringParameter.type + ["inherit"]
type = ChoiceStringParameter.type + ["inherit"]
name = "inherit"
compatible_builds = None
strict = True

def __init__(self, compatible_builds=None, **kw):
def dynamicChoices(masterStatus, buildername):
# cache the choices because that's what we've always done
self.choices = self.compatible_builds(masterStatus, buildername)
return self.choices

DynamicChoiceStringParameter.__init__(self,
compatible_builds=compatible_builds,
dynamicChoices=dynamicChoices,
**kw)
def getChoices(self, master, scheduler, buildername):
return self.compatible_builds(master.status, buildername)

def getFromKwargs(self, kwargs):
raise ValidationError("InheritBuildParameter can only be used by properties")

Expand Down
6 changes: 3 additions & 3 deletions master/buildbot/status/web/builder.py
Expand Up @@ -177,11 +177,11 @@ def buildForceContextForField(req, default_props, sch, field, master, buildernam

default = field.default

if "dynamic" in field.type:
choices = field.dynamicChoices(master.status, buildername)
default_props[pname+".choices"] = choices
if "list" in field.type:
choices = field.getChoices(master, sch, buildername)
if choices:
default = choices[0]
default_props[pname+".choices"] = choices

default = req.args.get(pname, [default])[0]
if "bool" in field.type:
Expand Down
5 changes: 2 additions & 3 deletions master/buildbot/status/web/templates/forms.html
Expand Up @@ -94,9 +94,8 @@
<span class="label">{{f.label}}</span>
<span class="select">
<select name='{{f.fullName}}' {{ f.multiple and "multiple" or ""}}>
{% set choices = f.choices if 'dynamic' not in f.type else default_props[sch.name+"."+f.fullName+".choices"] %}
{% for c in choices %}
<option {{(c in default_props[sch.name+"."+f.fullName]) and "selected" or ""}}>{{c}}</option>
{% for c in default_props[sch.name+"."+f.fullName+".choices"] %}
<option {{(c in default_props[sch.name+"."+f.fullName]) and "selected" or ""}}>{{c}}</option>
{% endfor %}
</select>
</span>
Expand Down
21 changes: 0 additions & 21 deletions master/buildbot/test/unit/test_schedulers_forcesched.py
Expand Up @@ -22,7 +22,6 @@
from buildbot.schedulers.forcesched import ChoiceStringParameter, ValidationError
from buildbot.schedulers.forcesched import NestedParameter, AnyPropertyParameter
from buildbot.schedulers.forcesched import CodebaseParameter, BaseParameter
from buildbot.schedulers.forcesched import DynamicChoiceStringParameter
from buildbot.test.util import scheduler
from buildbot.test.util.config import ConfigErrorsMixin

Expand Down Expand Up @@ -399,26 +398,6 @@ def test_ChoiceParameterMultipleError(self):
multiple=True, debug=False)


def test_DynamicChoiceStringParameter(self):
self.do_ParameterTest(value='t1', expect='t1',
klass=DynamicChoiceStringParameter, dynamicChoices=lambda m,b: ['t1','t2'])

def test_DynamicChoiceStringParameter_notStrict(self):
# the 'dynamicChoices' parameter really doesnt get called, so it really doesnt apply here
self.do_ParameterTest(value='t3', expect='t3',
strict=False,
klass=DynamicChoiceStringParameter, dynamicChoices=lambda m,b: ['t1','t2'])

def test_DynamicChoiceStringParameter_strict(self):
# the 'dynamicChoices' value doesnt affect the validation, so even a "GOOD" value will
# fail for strict=True tests, unless programmer overrides and provides their own validation
self.do_ParameterTest(value='t1',
strict=True,
expect=ValidationError,
expectKind=Exception,
klass=DynamicChoiceStringParameter, dynamicChoices=lambda m,b: ['t1','t2'],
debug=False)

def test_NestedParameter(self):
fields = [
IntParameter(name="foo")
Expand Down
33 changes: 3 additions & 30 deletions master/docs/manual/cfg-schedulers.rst
Expand Up @@ -1071,6 +1071,8 @@ use the authenticated user instead of displaying a text-entry box.
``need_email`` (optional; default True)
If true, require a full email address rather than arbitrary text.

.. bb:sched:: ChoiceStringParameter
ChoiceStringParameter
#####################

Expand Down Expand Up @@ -1123,7 +1125,7 @@ CodebaseParameter

::

CodebaseParameter(codebase="mycode")
CodebaseParameter(codebase="myrepo")

This is a parameter group to specify a sourcestamp for a given codebase.

Expand Down Expand Up @@ -1152,35 +1154,6 @@ This is a parameter group to specify a sourcestamp for a given codebase.
the build. The default value is a string parameter.


.. bb:sched:: DynamicChoiceStringParameter
DynamicChoiceStringParameter
############################

::

DynamicChoiceStringParameter(name="param",
dynamicChoices=functor)

This is a special parameter base class for generating dynamic content. It shares
the same fields as :py:class:`~ChoiceStringParameter`, but the choices will be
generated by a call to ``dynamicChoices``.

By default, this class works with ``strict=False``, and the selection will NOT be
validated. Since there is no method for tracking a user "session", it is not possible
to store the values displayed for a particular user and validate their selection against
"their" values. For a reference of ways to implement custom validation, please refer
to the source for the :py:class:`~InheritBuildParameter` subclass.

``dynamicChoices``

A function to generate the choices to be presented. This function is
given the master :py:class:`~buildbot.status.master.Status` instance as
first argument, and the current builder name as second argument. It should
return a list of values, from which the first element in the list will be
selected by default.


InheritBuildParameter
#####################

Expand Down
5 changes: 3 additions & 2 deletions master/docs/relnotes/index.rst
Expand Up @@ -27,8 +27,6 @@ Features

* :bb:step:`CopyDirectory` has been added.

* :bb:sched:`DynamicChoiceStringParameter` has been added.

* default.css now wraps preformatted text by default.

* Slaves can now be paused through the web status.
Expand Down Expand Up @@ -65,6 +63,9 @@ Changes for Developers
to send a message back to the committer.
See the linked documentation for details.

* bb:sched:`ChoiceStringParameter` has a new method ``getChoices`` that can be used to generate
content dynamically for Force scheduler forms.

Slave
-----

Expand Down

0 comments on commit 1076819

Please sign in to comment.