1 Introduction
The purpose of this page is to give a quick overview of how to compose component hierarchies using the PEAK framework, using working code. I have inlined comments in the code example, so hopefully, it will be self explanatory.
2 Code
Here's the code example that I came up with while working out how to compose hiearchies, and "aquire" objects from parent components in PEAK.
1 #file components1.py
2 from peak.api import *
3
4 #------------------------------------------------------------
5 class ISpam(protocols.Interface):
6 """Spam interface"""
7 def spam():
8 """provide spam"""
9
10 class Spam(binding.Component):
11 protocols.advise(instancesProvide=[ISpam],)
12 def spam(self):
13 return "spam"
14
15 #------------------------------------------------------------
16 class IEggs(protocols.Interface):
17 """Eggs interface"""
18 def eggs():
19 """provide eggs"""
20
21 class Eggs(binding.Component):
22 protocols.advise(instancesProvide=[IEggs],)
23 def eggs(self):
24 return "eggs"
25
26 class OtherEggs(binding.Component):
27 protocols.advise(instancesProvide=[IEggs],)
28 def eggs(self):
29 return "othereggs"
30
31
32 #------------------------------------------------------------
33 class GrandChildCmp(binding.Component):
34 #acquired from SimpleCmp
35 gcSpam = binding.bindToProperty("simplecmpApp.spam")
36 gcColor = binding.bindToProperty("simplecmpApp.color")
37 gcEggs = binding.bindToProperty("simplecmpApp.eggs")
38 #acquired from ChildCmp
39 gcOtherEggs = binding.bindTo(IEggs)
40 #acquired from SimpleCmp
41 gcStr = binding.bindTo("sc_string")
42
43 class ChildCmp(binding.Component):
44 #acquired from SimpleCmp
45 cSpam = binding.bindTo(ISpam)
46 cStr = binding.bindTo("sc_string")
47 #overrides IEggs provided by SimpleCmp
48 cEggs = binding.bindTo(IEggs)
49 __eggs = binding.New(OtherEggs, offerAs=[IEggs])
50
51 #make a GrandChild child of this component
52 grandChild = binding.New(GrandChildCmp)
53
54 class SimpleCmp(binding.Component):
55 #simple component variable. Child components can look up directly by name
56 sc_string = "simple-c!"
57 #__eggs and __spam are registered as Interfaces and properties
58 #They can be bound either using binding.bindTo(<InterfaceName>)
59 #or binding.bindToProperty(<property.name>)
60 __eggs = binding.New(Eggs, offerAs=[IEggs, PropertyName("simplecmpApp.eggs")])
61 __spam = binding.New(Spam, offerAs=[ISpam, PropertyName("simplecmpApp.spam")])
62 #__color is a Constant bound to a property
63 __color = binding.Constant(value="red", offerAs=["simplecmpApp.color"])
64
65 #make a ChildCmp child of this component
66 child = binding.New(ChildCmp)
67
68 #------------------------------------------------------------
69 if __name__ == "__main__":
70
71 sc = SimpleCmp()
72 c = sc.child
73 gc = c.grandChild
74
75
76 print "c.eggs: ",c.cEggs.eggs()
77 print "c.spam: ",c.cSpam.spam()
78 print "c.cStr: ",c.cStr
79 print "gc.spam: ",gc.gcSpam.spam()
80 print "gc.eggs: ",gc.gcEggs.eggs()
81 print "gcOther.eggs: ",gc.gcOtherEggs.eggs()
82 print "gc.myColor: ",gc.gcColor
83 print "gc.gcStr: ",gc.gcStr
84 print "done"
3 Running the example
Running this example should produce output similar to below:
[joel@banzai playground1]$ python components1.py
c.eggs: othereggs
c.eggs: spam
c.cStr: simple-c!
gc.spam: spam
gc.eggs: eggs
gcOther.eggs: othereggs
gc.myColor: red
gc.gcStr: simple-c!
done
--Joel Boehland