# Path: charms/reactive/bus.py
# class FlagWatch(object):
#     key = 'reactive.state_watch'
# 
#     @classmethod
#     def _store(cls):
#         return unitdata.kv()
# 
#     @classmethod
#     def _get(cls):
#         return cls._store().get(cls.key, {
#             'iteration': 0,
#             'changes': [],
#             'pending': [],
#         })
# 
#     @classmethod
#     def _set(cls, data):
#         cls._store().set(cls.key, data)
# 
#     @classmethod
#     def reset(cls):
#         cls._store().unset(cls.key)
# 
#     @classmethod
#     def iteration(cls, i):
#         data = cls._get()
#         data['iteration'] = i
#         cls._set(data)
# 
#     @classmethod
#     def watch(cls, watcher, flags):
#         data = cls._get()
#         iteration = data['iteration']
#         changed = bool(set(flags) & set(data['changes']))
#         return iteration == 0 or changed
# 
#     @classmethod
#     def change(cls, flag):
#         data = cls._get()
#         data['pending'].append(flag)
#         cls._set(data)
# 
#     @classmethod
#     def commit(cls):
#         data = cls._get()
#         data['changes'] = data['pending']
#         data['pending'] = []
#         cls._set(data)
# 
# Path: charms/reactive/trace.py
# def tracer():
#     global _tracer
#     return _tracer


from charmhelpers.cli import cmdline
from charmhelpers.core import hookenv
from charmhelpers.core import unitdata
from charms.reactive.bus import FlagWatch
from charms.reactive.trace import tracer
    unitdata.kv().update({flag: value}, prefix='reactive.states.')
    if flag not in old_flags:
        tracer().set_flag(flag)
        FlagWatch.change(flag)
        trigger = _get_trigger(flag, None)
        for flag_name in trigger['set_flag']:
            set_flag(flag_name)
        for flag_name in trigger['clear_flag']:
            clear_flag(flag_name)
        for callback in trigger['callbacks']:
            callback()


@cmdline.subcommand()
@cmdline.no_output
def clear_flag(flag):
    """
    Clear / deactivate a flag.

    :param str flag: Name of flag to set.

    .. note:: **Changes to flags are reset when a handler crashes.** Changes to
       flags happen immediately, but they are only persisted at the end of a
       complete and successful run of the reactive framework. All unpersisted
       changes are discarded when a hook crashes.
    """
    old_flags = get_flags()
    unitdata.kv().unset('reactive.states.%s' % flag)
    unitdata.kv().set('reactive.dispatch.removed_state', True)
    if flag in old_flags:

# Path: charms/reactive/bus.py
# class FlagWatch(object):
#     key = 'reactive.state_watch'
# 
#     @classmethod
#     def _store(cls):
#         return unitdata.kv()
# 
#     @classmethod
#     def _get(cls):
#         return cls._store().get(cls.key, {
#             'iteration': 0,
#             'changes': [],
#             'pending': [],
#         })
# 
#     @classmethod
#     def _set(cls, data):
#         cls._store().set(cls.key, data)
# 
#     @classmethod
#     def reset(cls):
#         cls._store().unset(cls.key)
# 
#     @classmethod
#     def iteration(cls, i):
#         data = cls._get()
#         data['iteration'] = i
#         cls._set(data)
# 
#     @classmethod
#     def watch(cls, watcher, flags):
#         data = cls._get()
#         iteration = data['iteration']
#         changed = bool(set(flags) & set(data['changes']))
#         return iteration == 0 or changed
# 
#     @classmethod
#     def change(cls, flag):
#         data = cls._get()
#         data['pending'].append(flag)
#         cls._set(data)
# 
#     @classmethod
#     def commit(cls):
#         data = cls._get()
#         data['changes'] = data['pending']
#         data['pending'] = []
#         cls._set(data)
# 
# Path: charms/reactive/trace.py
# def tracer():
#     global _tracer
#     return _tracer


# Path: charms/reactive/flags.py
from charmhelpers.cli import cmdline
from charmhelpers.core import hookenv
from charmhelpers.core import unitdata
from charms.reactive.bus import FlagWatch
from charms.reactive.trace import tracer

    unitdata.kv().update({flag: value}, prefix='reactive.states.')
    if flag not in old_flags:
        tracer().set_flag(flag)
        FlagWatch.change(flag)
        trigger = _get_trigger(flag, None)
        for flag_name in trigger['set_flag']:
            set_flag(flag_name)
        for flag_name in trigger['clear_flag']:
            clear_flag(flag_name)
        for callback in trigger['callbacks']:
            callback()


@cmdline.subcommand()
@cmdline.no_output
def clear_flag(flag):
    """
    Clear / deactivate a flag.

    :param str flag: Name of flag to set.

    .. note:: **Changes to flags are reset when a handler crashes.** Changes to
       flags happen immediately, but they are only persisted at the end of a
       complete and successful run of the reactive framework. All unpersisted
       changes are discarded when a hook crashes.
    """
    old_flags = get_flags()
    unitdata.kv().unset('reactive.states.%s' % flag)
    unitdata.kv().set('reactive.dispatch.removed_state', True)
    if flag in old_flags:

        tracer().clear_flag(flag)