Skip to content

Commit

Permalink
Refactor model state management.
Browse files Browse the repository at this point in the history
Maintain entity state history in the model, not in the entities.
Add methods for navigating an entity's history.
  • Loading branch information
tvansteenburgh committed Oct 14, 2016
1 parent 660a58a commit 46887b8
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 55 deletions.
14 changes: 14 additions & 0 deletions examples/relate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
from juju.model import Model, ModelObserver


class MyRemoveObserver(ModelObserver):
async def on_change(self, delta, old, new, model):
if delta.type == 'remove':
assert(new.latest() == new)
assert(new.next() is None)
assert(new.dead)
assert(new.current)
assert(new.connected)
assert(new.previous().dead)
assert(not new.previous().current)
assert(not new.previous().connected)


class MyModelObserver(ModelObserver):
async def on_change(self, delta, old, new, model):
if model.all_units_idle():
Expand All @@ -25,6 +38,7 @@ async def run():
model = Model()
await model.connect_current()

model.add_observer(MyRemoveObserver())
await model.reset(force=True)
model.add_observer(MyModelObserver())

Expand Down
30 changes: 21 additions & 9 deletions juju/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@


def get_entity_delta(d):
_delta_types = {
'action': ActionDelta,
'application': ApplicationDelta,
'annotation': AnnotationDelta,
'machine': MachineDelta,
'unit': UnitDelta,
'relation': RelationDelta,
}

return _delta_types[d.entity](d.deltas)


def get_entity_class(entity_type):
return _delta_types[entity_type].get_entity_class()


class EntityDelta(client.Delta):
def get_id(self):
return self.data['id']

@classmethod
def get_entity_class(self):
return None


class ActionDelta(EntityDelta):
@classmethod
def get_entity_class(self):
from .action import Action
return Action
Expand All @@ -32,6 +29,7 @@ class ApplicationDelta(EntityDelta):
def get_id(self):
return self.data['name']

@classmethod
def get_entity_class(self):
from .application import Application
return Application
Expand All @@ -41,12 +39,14 @@ class AnnotationDelta(EntityDelta):
def get_id(self):
return self.data['tag']

@classmethod
def get_entity_class(self):
from .annotation import Annotation
return Annotation


class MachineDelta(EntityDelta):
@classmethod
def get_entity_class(self):
from .machine import Machine
return Machine
Expand All @@ -56,12 +56,24 @@ class UnitDelta(EntityDelta):
def get_id(self):
return self.data['name']

@classmethod
def get_entity_class(self):
from .unit import Unit
return Unit


class RelationDelta(EntityDelta):
@classmethod
def get_entity_class(self):
from .relation import Relation
return Relation


_delta_types = {
'action': ActionDelta,
'application': ApplicationDelta,
'annotation': AnnotationDelta,
'machine': MachineDelta,
'unit': UnitDelta,
'relation': RelationDelta,
}
2 changes: 2 additions & 0 deletions juju/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class DeadEntityException(Exception):
pass

0 comments on commit 46887b8

Please sign in to comment.