def remove_state(self, state): """ Remove this conversation from the given state, and potentially deactivate the state if no more conversations are in it. The relation name will be interpolated in the state name, and it is recommended that it be included to avoid conflicts with states from other relations. For example:: conversation.remove_state('{relation_name}.state') If called from a converation handling the relation "foo", this will remove the conversation from the "foo.state" state, and, if no more conversations are in this the state, will deactivate it. """ state = state.format(relation_name=self.relation_name) value = get_state(state) if not value: return if self.key in value['conversations']: value['conversations'].remove(self.key) if value['conversations']: set_state(state, value) else: remove_state(state)
def from_state(cls, state): """ Find relation implementation in the current charm, based on the name of an active state. """ value = get_state(state) if value is None: return None relation_name = value['relation'] conversations = Conversation.load(value['conversations']) return cls.from_name(relation_name, conversations)
def set_state(self, state): """ Activate and put this conversation into the given state. The relation name will be interpolated in the state name, and it is recommended that it be included to avoid conflicts with states from other relations. For example:: conversation.set_state('{relation_name}.state') If called from a converation handling the relation "foo", this will activate the "foo.state" state, and will add this conversation to that state. Note: This uses :mod:`charmhelpers.core.unitdata` and requires that :meth:`~charmhelpers.core.unitdata.Storage.flush` be called. """ state = state.format(relation_name=self.relation_name) value = get_state(state, { 'relation': self.relation_name, 'conversations': [], }) value['conversations'].append(self.key) set_state(state, value)