Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use observe in traitlets.config #149

Merged
merged 1 commit into from
Dec 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions traitlets/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)

from traitlets.traitlets import (
Unicode, List, Enum, Dict, Instance, TraitError
Unicode, List, Enum, Dict, Instance, TraitError, observe, default
)
from ipython_genutils.importstring import import_item
from ipython_genutils.text import indent, wrap_paragraphs, dedent
Expand Down Expand Up @@ -177,7 +177,7 @@ def _log_format_changed(self, name, old, new):
_log_formatter = self._log_formatter_cls(fmt=new, datefmt=self.log_datefmt)
_log_handler.setFormatter(_log_formatter)


@default('log')
def _log_default(self):
"""Start logging for this application.

Expand Down Expand Up @@ -215,12 +215,14 @@ def _log_default(self):
# this must be a dict of two-tuples, the first element being the Config/dict
# and the second being the help string for the flag
flags = Dict()
def _flags_changed(self, name, old, new):
@observe('flags')
def _flags_changed(self, change):
"""ensure flags dict is valid"""
for key,value in iteritems(new):
assert len(value) == 2, "Bad flag: %r:%s"%(key,value)
assert isinstance(value[0], (dict, Config)), "Bad flag: %r:%s"%(key,value)
assert isinstance(value[1], string_types), "Bad flag: %r:%s"%(key,value)
new = change['new']
for key, value in new.items():
assert len(value) == 2, "Bad flag: %r:%s" % (key, value)
assert isinstance(value[0], (dict, Config)), "Bad flag: %r:%s" % (key, value)
assert isinstance(value[1], string_types), "Bad flag: %r:%s" % (key, value)


# subcommands for launching other applications
Expand All @@ -242,11 +244,12 @@ def __init__(self, **kwargs):
# options and config files.
if self.__class__ not in self.classes:
self.classes.insert(0, self.__class__)

def _config_changed(self, name, old, new):
SingletonConfigurable._config_changed(self, name, old, new)

@observe('config')
def _config_changed(self, change):
super(Application, self)._config_changed(change)
self.log.debug('Config changed:')
self.log.debug(repr(new))
self.log.debug(repr(change['new']))

@catch_config_error
def initialize(self, argv=None):
Expand Down
10 changes: 5 additions & 5 deletions traitlets/config/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

from __future__ import print_function

import logging
from copy import deepcopy

from .loader import Config, LazyConfigValue
from traitlets.traitlets import HasTraits, Instance
from traitlets.traitlets import HasTraits, Instance, observe, default
from ipython_genutils.text import indent, dedent, wrap_paragraphs
from ipython_genutils.py3compat import iteritems

Expand Down Expand Up @@ -162,8 +161,8 @@ def _load_config(self, cfg, section_names=None, traits=None):
self.log.warning(u"Config option `{option}` not recognized by `{klass}`, do you mean one of : `{matches}`"
.format(option=name, klass=type(self).__name__, matches=' ,'.join(matches)))


def _config_changed(self, name, old, new):
@observe('config')
def _config_changed(self, change):
"""Update all the class traits having ``config=True`` in metadata.

For any class trait with a ``config`` metadata attribute that is
Expand All @@ -177,7 +176,7 @@ def _config_changed(self, name, old, new):
# classes that are Configurable subclasses. This starts with Configurable
# and works down the mro loading the config for each section.
section_names = self.section_names()
self._load_config(new, traits=traits, section_names=section_names)
self._load_config(change['new'], traits=traits, section_names=section_names)

def update_config(self, config):
"""Update config and trigger reload of config via trait events"""
Expand Down Expand Up @@ -329,6 +328,7 @@ class LoggingConfigurable(Configurable):
"""

log = Instance('logging.Logger')
@default('log')
def _log_default(self):
from traitlets import log
return log.get_logger()
Expand Down