~openstack-charmers-archive/charms/trusty/ceph-radosgw/next

« back to all changes in this revision

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

  • Committer: Christopher Glass
  • Date: 2014-09-20 07:26:18 UTC
  • mfrom: (25.1.1 ceph-radosgw)
  • Revision ID: christopher.glass@canonical.com-20140920072618-9lyk6yt44nv8js33
Merging lp:~gnuoy/charms/trusty/ceph-radosgw/next-charmhelper-sync [r=tribaal]

The ceph-radosgw charm was in need of a charm-helpers refresh (to get the apt-cache
in-memory indexing).

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
 
 
206
    def __getitem__(self, key):
 
207
        """For regular dict lookups, check the current juju config first,
 
208
        then the previous (saved) copy. This ensures that user-saved values
 
209
        will be returned by a dict lookup.
 
210
 
 
211
        """
 
212
        try:
 
213
            return dict.__getitem__(self, key)
 
214
        except KeyError:
 
215
            return (self._prev_dict or {})[key]
 
216
 
204
217
    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.
 
218
        """Load previous copy of config from disk.
 
219
 
 
220
        In normal usage you don't need to call this method directly - it
 
221
        is called automatically at object initialization.
207
222
 
208
223
        :param path:
209
224
 
218
233
            self._prev_dict = json.load(f)
219
234
 
220
235
    def changed(self, key):
221
 
        """Return true if the value for this key has changed since
222
 
        the last save.
 
236
        """Return True if the current value for this key is different from
 
237
        the previous value.
223
238
 
224
239
        """
225
240
        if self._prev_dict is None:
228
243
 
229
244
    def previous(self, key):
230
245
        """Return previous value for this key, or None if there
231
 
        is no "previous" value.
 
246
        is no previous value.
232
247
 
233
248
        """
234
249
        if self._prev_dict:
238
253
    def save(self):
239
254
        """Save this config to disk.
240
255
 
241
 
        Preserves items in _prev_dict that do not exist in self.
 
256
        If the charm is using the :mod:`Services Framework <services.base>`
 
257
        or :meth:'@hook <Hooks.hook>' decorator, this
 
258
        is called automatically at the end of successful hook execution.
 
259
        Otherwise, it should be called directly by user code.
 
260
 
 
261
        To disable automatic saves, set ``implicit_save=False`` on this
 
262
        instance.
242
263
 
243
264
        """
244
265
        if self._prev_dict:
285
306
        raise
286
307
 
287
308
 
288
 
def relation_set(relation_id=None, relation_settings={}, **kwargs):
 
309
def relation_set(relation_id=None, relation_settings=None, **kwargs):
289
310
    """Set relation information for the current unit"""
 
311
    relation_settings = relation_settings if relation_settings else {}
290
312
    relation_cmd_line = ['relation-set']
291
313
    if relation_id is not None:
292
314
        relation_cmd_line.extend(('-r', relation_id))
477
499
        hook_name = os.path.basename(args[0])
478
500
        if hook_name in self._hooks:
479
501
            self._hooks[hook_name]()
 
502
            cfg = config()
 
503
            if cfg.implicit_save:
 
504
                cfg.save()
480
505
        else:
481
506
            raise UnregisteredHookError(hook_name)
482
507