|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +""" |
| 4 | +A base class for objects that are configurable. |
| 5 | +
|
| 6 | +Authors: |
| 7 | +
|
| 8 | +* Brian Granger |
| 9 | +* Fernando Perez |
| 10 | +""" |
| 11 | + |
| 12 | +#----------------------------------------------------------------------------- |
| 13 | +# Copyright (C) 2008-2010 The IPython Development Team |
| 14 | +# |
| 15 | +# Distributed under the terms of the BSD License. The full license is in |
| 16 | +# the file COPYING, distributed as part of this software. |
| 17 | +#----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +#----------------------------------------------------------------------------- |
| 20 | +# Imports |
| 21 | +#----------------------------------------------------------------------------- |
| 22 | + |
| 23 | +from copy import deepcopy |
| 24 | +import datetime |
| 25 | +from weakref import WeakValueDictionary |
| 26 | + |
| 27 | +from IPython.utils.importstring import import_item |
| 28 | +from loader import Config |
| 29 | +from IPython.utils.traitlets import HasTraits, Instance |
| 30 | + |
| 31 | + |
| 32 | +#----------------------------------------------------------------------------- |
| 33 | +# Helper classes for Configurables |
| 34 | +#----------------------------------------------------------------------------- |
| 35 | + |
| 36 | + |
| 37 | +class ConfigurableError(Exception): |
| 38 | + pass |
| 39 | + |
| 40 | + |
| 41 | +#----------------------------------------------------------------------------- |
| 42 | +# Configurable implementation |
| 43 | +#----------------------------------------------------------------------------- |
| 44 | + |
| 45 | + |
| 46 | +class Configurable(HasTraits): |
| 47 | + |
| 48 | + config = Instance(Config,(),{}) |
| 49 | + created = None |
| 50 | + |
| 51 | + def __init__(self, config=None): |
| 52 | + """Create a conigurable given a config config. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + config : Config |
| 57 | + If this is empty, default values are used. If config is a |
| 58 | + :class:`Config` instance, it will be used to configure the |
| 59 | + instance. |
| 60 | + |
| 61 | + Notes |
| 62 | + ----- |
| 63 | + Subclasses of Configurable must call the :meth:`__init__` method of |
| 64 | + :class:`Configurable` *before* doing anything else and using |
| 65 | + :func:`super`:: |
| 66 | + |
| 67 | + class MyConfigurable(Configurable): |
| 68 | + def __init__(self, config=None): |
| 69 | + super(MyConfigurable, self).__init__(config) |
| 70 | + # Then any other code you need to finish initialization. |
| 71 | +
|
| 72 | + This ensures that instances will be configured properly. |
| 73 | + """ |
| 74 | + super(Configurable, self).__init__() |
| 75 | + if config is not None: |
| 76 | + # We used to deepcopy, but for now we are trying to just save |
| 77 | + # by reference. This *could* have side effects as all components |
| 78 | + # will share config. In fact, I did find such a side effect in |
| 79 | + # _config_changed below. If a config attribute value was a mutable type |
| 80 | + # all instances of a component were getting the same copy, effectively |
| 81 | + # making that a class attribute. |
| 82 | + # self.config = deepcopy(config) |
| 83 | + self.config = config |
| 84 | + self.created = datetime.datetime.now() |
| 85 | + |
| 86 | + #------------------------------------------------------------------------- |
| 87 | + # Static trait notifiations |
| 88 | + #------------------------------------------------------------------------- |
| 89 | + |
| 90 | + def _config_changed(self, name, old, new): |
| 91 | + """Update all the class traits having ``config=True`` as metadata. |
| 92 | +
|
| 93 | + For any class trait with a ``config`` metadata attribute that is |
| 94 | + ``True``, we update the trait with the value of the corresponding |
| 95 | + config entry. |
| 96 | + """ |
| 97 | + # Get all traits with a config metadata entry that is True |
| 98 | + traits = self.traits(config=True) |
| 99 | + |
| 100 | + # We auto-load config section for this class as well as any parent |
| 101 | + # classes that are Configurable subclasses. This starts with Configurable |
| 102 | + # and works down the mro loading the config for each section. |
| 103 | + section_names = [cls.__name__ for cls in \ |
| 104 | + reversed(self.__class__.__mro__) if |
| 105 | + issubclass(cls, Configurable) and issubclass(self.__class__, cls)] |
| 106 | + |
| 107 | + for sname in section_names: |
| 108 | + # Don't do a blind getattr as that would cause the config to |
| 109 | + # dynamically create the section with name self.__class__.__name__. |
| 110 | + if new._has_section(sname): |
| 111 | + my_config = new[sname] |
| 112 | + for k, v in traits.items(): |
| 113 | + # Don't allow traitlets with config=True to start with |
| 114 | + # uppercase. Otherwise, they are confused with Config |
| 115 | + # subsections. But, developers shouldn't have uppercase |
| 116 | + # attributes anyways! (PEP 6) |
| 117 | + if k[0].upper()==k[0] and not k.startswith('_'): |
| 118 | + raise ConfigurableError('Configurable traitlets with ' |
| 119 | + 'config=True must start with a lowercase so they are ' |
| 120 | + 'not confused with Config subsections: %s.%s' % \ |
| 121 | + (self.__class__.__name__, k)) |
| 122 | + try: |
| 123 | + # Here we grab the value from the config |
| 124 | + # If k has the naming convention of a config |
| 125 | + # section, it will be auto created. |
| 126 | + config_value = my_config[k] |
| 127 | + except KeyError: |
| 128 | + pass |
| 129 | + else: |
| 130 | + # print "Setting %s.%s from %s.%s=%r" % \ |
| 131 | + # (self.__class__.__name__,k,sname,k,config_value) |
| 132 | + # We have to do a deepcopy here if we don't deepcopy the entire |
| 133 | + # config object. If we don't, a mutable config_value will be |
| 134 | + # shared by all instances, effectively making it a class attribute. |
| 135 | + setattr(self, k, deepcopy(config_value)) |
| 136 | + |
0 commit comments