initialise repo
[debian/make-magic.git] / doc / example_python_items.py
1 #!/usr/bin/env python
2
3 '''simple example of how to define items in pure python
4
5 These are the same items as in examples/sample_items.json,
6 but rather than converting them from JSON, they are defined
7 using pure python (actually, the supplied doc/sample_items.json 
8 was generated using this module and core.marshall.ItemConverter)
9
10 breakfast_task_factory is a task factory exactly like the
11 one used in lib/magic.py. If you want to define your items using
12 python rather than JSON, this is a pretty good way to do it
13 '''
14
15 import lib.loaders
16 from core.bits import *
17 from core.marshal import ItemConverter
18 from digraphtools.predicate import predicate
19
20 want_coffee = ItemConverter().predicate_string_to_callable(' coffee ')
21 assert want_coffee(['coffee','tv']) == True
22 assert want_coffee(['fish','tv']) == False
23
24 # Items 
25 class wake_up(Item):
26         pass
27
28 class get_up(Item):
29         depends = (wake_up,)
30
31 class make_breakfast(Item):
32         depends = (get_up,)
33
34 class eat_breakfast(Item):
35         depends = (make_breakfast,)
36
37 class make_coffee(Item):
38         depends = (get_up,)
39         predicate = want_coffee
40
41 class drink_coffee(Item):
42         depends = (make_coffee,)
43         predicate = want_coffee
44
45 class have_breakfast(Group):
46         depends = (get_up,)
47         contains = (eat_breakfast, drink_coffee)
48
49 class walk_out_door(Item):
50         depends = (get_up, have_breakfast)
51
52 class go_to_work(Item):
53         description = 'Leave to go to work'
54         depends = (walk_out_door,)
55
56 # Tasks
57
58 items = [wake_up, get_up, have_breakfast, make_breakfast, eat_breakfast, make_coffee, drink_coffee, walk_out_door, go_to_work]
59 breakfast_task_factory = lib.loaders.TaskFactory(items)