Skip to content

Commit

Permalink
make profile_list union of List and Callable
Browse files Browse the repository at this point in the history
  • Loading branch information
bitnik committed Jun 21, 2018
1 parent f98fc21 commit e2058b1
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,10 @@ def _hub_connect_port_default(self):
"""
)

profile_list = List(
trait=Dict(),
default_value=None,
minlen=0,
profile_list = Union([
List(trait=Dict()),
Callable()
],
config=True,
help="""
List of profiles to offer for selection by the user.
Expand Down Expand Up @@ -955,6 +955,13 @@ def _hub_connect_port_default(self):
}
}
]
Instead of a list of dictionaries, this could also be a callable that takes as one
parameter the current spawner instance and returns a list of dictionaries. The
callable will be called asynchronously if it returns a future, rather than
a list. Note that the interface of the spawner class is not deemed stable
across versions, so using this functionality might cause your JupyterHub
or kubespawner upgrades to break.
"""
)

Expand Down Expand Up @@ -1403,6 +1410,16 @@ def get_args(self):
break
return args

def _render_options_form(self, profile_list):
self._profile_list = profile_list
profile_form_template = Environment(loader=BaseLoader).from_string(self.profile_form_template)
return profile_form_template.render(profile_list=profile_list)

@gen.coroutine
def _render_options_form_dynamically(self, current_spawner):
profile_list = yield gen.maybe_future(self.profile_list(current_spawner))
return self._render_options_form(profile_list)

def _options_form_default(self):
'''
Build the form template according to the `profile_list` setting.
Expand All @@ -1413,8 +1430,10 @@ def _options_form_default(self):
'''
if not self.profile_list:
return ''
profile_form_template = Environment(loader=BaseLoader).from_string(self.profile_form_template)
return profile_form_template.render(profile_list=self.profile_list)
if callable(self.profile_list):
return self._render_options_form_dynamically
else:
return self._render_options_form(self.profile_list)

def options_from_form(self, formdata):
"""get the option selected by the user on the form
Expand All @@ -1438,12 +1457,11 @@ def options_from_form(self, formdata):
Returns:
the selected user option
"""

if not self.profile_list:
if not self.profile_list or not hasattr(self, '_profile_list'):
return formdata
# Default to first profile if somehow none is provided
selected_profile = int(formdata.get('profile', [0])[0])
options = self.profile_list[selected_profile]
options = self._profile_list[selected_profile]
self.log.debug("Applying KubeSpawner override for profile '%s'", options['display_name'])
kubespawner_override = options.get('kubespawner_override', {})
for k, v in kubespawner_override.items():
Expand Down

0 comments on commit e2058b1

Please sign in to comment.