| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 1 | """ |
| 2 | This example: |
| 3 | |
| 4 | 1. Connects to the current model |
| 5 | 2. Watches the model and prints all changes |
| 6 | 3. Runs forever (kill with Ctrl-C) |
| 7 | |
| 8 | """ |
| 9 | import asyncio |
| 10 | |
| 11 | from juju.model import Model |
| 12 | from juju import loop |
| 13 | |
| 14 | |
| 15 | async def on_model_change(delta, old, new, model): |
| 16 | print(delta.entity, delta.type, delta.data) |
| 17 | print(old) |
| 18 | print(new) |
| 19 | print(model) |
| 20 | |
| 21 | |
| 22 | async def watch_model(): |
| 23 | model = Model() |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 24 | await model.connect() |
| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame] | 25 | |
| 26 | model.add_observer(on_model_change) |
| 27 | |
| 28 | |
| 29 | if __name__ == '__main__': |
| 30 | # Run loop until the process is manually stopped (watch_model will loop |
| 31 | # forever). |
| 32 | loop.run(watch_model()) |