~brad-marshall/charms/trusty/ntp/remove-ntp-status-check

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/hookenv.py

  • Committer: Matt Bruzek
  • Date: 2014-09-11 20:46:39 UTC
  • mfrom: (16.1.1 ntp)
  • Revision ID: matthew.bruzek@canonical.com-20140911204639-hi63o5fwwbr0umwv
[rcj] Add missing jinja templating module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
156
156
 
157
157
 
158
158
class Config(dict):
159
 
    """A Juju charm config dictionary that can write itself to
160
 
    disk (as json) and track which values have changed since
161
 
    the previous hook invocation.
162
 
 
163
 
    Do not instantiate this object directly - instead call
164
 
    ``hookenv.config()``
 
159
    """A dictionary representation of the charm's config.yaml, with some
 
160
    extra features:
 
161
 
 
162
    - See which values in the dictionary have changed since the previous hook.
 
163
    - For values that have changed, see what the previous value was.
 
164
    - Store arbitrary data for use in a later hook.
 
165
 
 
166
    NOTE: Do not instantiate this object directly - instead call
 
167
    ``hookenv.config()``, which will return an instance of :class:`Config`.
165
168
 
166
169
    Example usage::
167
170
 
170
173
        >>> config = hookenv.config()
171
174
        >>> config['foo']
172
175
        'bar'
 
176
        >>> # store a new key/value for later use
173
177
        >>> config['mykey'] = 'myval'
174
 
        >>> config.save()
175
178
 
176
179
 
177
180
        >>> # user runs `juju set mycharm foo=baz`
188
191
        >>> # keys/values that we add are preserved across hooks
189
192
        >>> config['mykey']
190
193
        'myval'
191
 
        >>> # don't forget to save at the end of hook!
192
 
        >>> config.save()
193
194
 
194
195
    """
195
196
    CONFIG_FILE_NAME = '.juju-persistent-config'
196
197
 
197
198
    def __init__(self, *args, **kw):
198
199
        super(Config, self).__init__(*args, **kw)
 
200
        self.implicit_save = True
199
201
        self._prev_dict = None
200
202
        self.path = os.path.join(charm_dir(), Config.CONFIG_FILE_NAME)
201
203
        if os.path.exists(self.path):
202
204
            self.load_previous()
203
205
 
204
206
    def load_previous(self, path=None):
205
 
        """Load previous copy of config from disk so that current values
206
 
        can be compared to previous values.
 
207
        """Load previous copy of config from disk.
 
208
 
 
209
        In normal usage you don't need to call this method directly - it
 
210
        is called automatically at object initialization.
207
211
 
208
212
        :param path:
209
213
 
218
222
            self._prev_dict = json.load(f)
219
223
 
220
224
    def changed(self, key):
221
 
        """Return true if the value for this key has changed since
222
 
        the last save.
 
225
        """Return True if the current value for this key is different from
 
226
        the previous value.
223
227
 
224
228
        """
225
229
        if self._prev_dict is None:
228
232
 
229
233
    def previous(self, key):
230
234
        """Return previous value for this key, or None if there
231
 
        is no "previous" value.
 
235
        is no previous value.
232
236
 
233
237
        """
234
238
        if self._prev_dict:
238
242
    def save(self):
239
243
        """Save this config to disk.
240
244
 
241
 
        Preserves items in _prev_dict that do not exist in self.
 
245
        If the charm is using the :mod:`Services Framework <services.base>`
 
246
        or :meth:'@hook <Hooks.hook>' decorator, this
 
247
        is called automatically at the end of successful hook execution.
 
248
        Otherwise, it should be called directly by user code.
 
249
 
 
250
        To disable automatic saves, set ``implicit_save=False`` on this
 
251
        instance.
242
252
 
243
253
        """
244
254
        if self._prev_dict:
285
295
        raise
286
296
 
287
297
 
288
 
def relation_set(relation_id=None, relation_settings={}, **kwargs):
 
298
def relation_set(relation_id=None, relation_settings=None, **kwargs):
289
299
    """Set relation information for the current unit"""
 
300
    relation_settings = relation_settings if relation_settings else {}
290
301
    relation_cmd_line = ['relation-set']
291
302
    if relation_id is not None:
292
303
        relation_cmd_line.extend(('-r', relation_id))
477
488
        hook_name = os.path.basename(args[0])
478
489
        if hook_name in self._hooks:
479
490
            self._hooks[hook_name]()
 
491
            cfg = config()
 
492
            if cfg.implicit_save:
 
493
                cfg.save()
480
494
        else:
481
495
            raise UnregisteredHookError(hook_name)
482
496