3. Einleitung Binding Konfiguration Plugins
PEAK im Überblick
Framework für Komponentenbasiertes Softwaredesign
Inversion-Of-Control Pattern
Domain-Model Pattern
Event basierte Programmierung
Naming
...
Roland Gude
Python Enterprise Application Toolkit (PEAK)
4. Einleitung Binding Konfiguration Plugins
Das binding Modul
Inversion-Of-Control Pattern
kopplung von Komponenten
auffinden von Komponenten
Roland Gude
Python Enterprise Application Toolkit (PEAK)
5. Einleitung Binding Konfiguration Plugins
Das binding Modul
Beispiel
from peak . a p i import ∗
1
class Spam( b i n d i n g . Component ) :
2
def yum ( s e l f ) :
3
p r i n t ’ spam ’
4
class Cook ( b i n d i n g . Component ) :
5
meal = b i n d i n g . Obtain ( ’ meal ’ )
6
def prepareMeal ( s e l f ) :
7
s e l f . meal . yum ( )
8
class HungryGuy ( b i n d i n g . Component ) :
9
cook = b i n d i n g . Obtain ( ’ cook ’ )
10
def buyMeal ( s e l f ) :
11
s e l f . cook . prepareMeal ( )
12
Roland Gude
Python Enterprise Application Toolkit (PEAK)
6. Einleitung Binding Konfiguration Plugins
Das binding Modul
Fortsetzung Beispiel
from b i n d i n g 1 import ∗
1
class R e s t a u r a n t ( b i n d i n g . Component ) :
2
meal = b i n d i n g . Make (Spam)
3
cook = b i n d i n g . Make ( Cook )
4
customer = b i n d i n g . Make ( HungryGuy )
5
def open ( s e l f ) :
6
s e l f . customer . buyMeal ( )
7
8
r e s t a u r a n t = Restaurant ( )
9
r e s t a u r a n t . open ( )
10
#>>> ’ spam ’
11
Roland Gude
Python Enterprise Application Toolkit (PEAK)
7. Einleitung Binding Konfiguration Plugins
Das config Modul
Konfiguration von Komponente
.ini artige konfigurationsdateien
ausführbar
Roland Gude
Python Enterprise Application Toolkit (PEAK)
8. Einleitung Binding Konfiguration Plugins
Das config Modul
Beispiel
# ! / u s r / b i n / env peak r u n I n i
1
2
[ peak . r u n n i n g ]
3
app = i m p o r t S t r i n g ( ’ c o n f i g u r a b l e . BigComputer ’ )
4
5
[ what . i s ]
6
theAnswer = 42
7
Roland Gude
Python Enterprise Application Toolkit (PEAK)
9. Einleitung Binding Konfiguration Plugins
Das config Modul
Beispiel
from peak . a p i import ∗
1
2
class BigComputer ( commands . AbstractCommand ) :
3
answer = b i n d i n g . Obtain (
4
PropertyName ( ’ what . i s . theAnswer ’ ) )
5
def _run ( s e l f ) :
6
p r i n t >> s e l f . s t d o u t , ’ Die A n t w o r t l a u t e t : ’ ,
7
s e l f . answer
8
peak runInI config
»» Die Antwort lautet: 42
Roland Gude
Python Enterprise Application Toolkit (PEAK)
10. Einleitung Binding Konfiguration Plugins
Plugins mit PEAK
Eggs sind:
vergleichbar mit JARs
advertisable / discoverable
Roland Gude
Python Enterprise Application Toolkit (PEAK)
12. Einleitung Binding Konfiguration Plugins
Plugins mit PEAK
spam_plugin.py
Listing 1: spam_plugin.py
p r i n t ’Spam l o a d i n g ! ’
1
2
class Spam :
3
4
def spam ( s e l f ) :
5
r e t u r n ’ spam ’
6
Roland Gude
Python Enterprise Application Toolkit (PEAK)
13. Einleitung Binding Konfiguration Plugins
Plugins mit PEAK
setup.py
Listing 2: setup.py
from s e t u p t o o l s import setup , find_packages
1
2
setup (
3
name= quot;Spamquot; ,
4
v e r s i o n = quot; 0 . 1 rc7−dev9 quot; ,
5
d e s c r i p t i o n = quot; quot; quot;Spam p l u g i n quot; quot; quot; ,
6
a u t h o r = quot; Roland Gude quot; ,
7
packages = [ ’ spam_plugin ’ ] ,
8
entry_points=quot; quot; quot;
9
[ entry . point ]
10
spam = spam_plugin . spam : Spam
11
quot;quot;quot;)
12
Roland Gude
Python Enterprise Application Toolkit (PEAK)
14. Einleitung Binding Konfiguration Plugins
Plugins mit PEAK
build and install
$ cd spam
$ python setup.py bdist_egg
$ sudo easy_install dist/Spam-0.1rc7-dev9-py2.4.egg
Roland Gude
Python Enterprise Application Toolkit (PEAK)
15. Einleitung Binding Konfiguration Plugins
Plugins mit PEAK
plugins benutzen
Listing 3: load_plugins.py
import pkg_resources
1
2
f o r e i n pkg_resources . i t e r _ e n t r y _ p o i n t s (
3
quot; entry . point quot; ) :
4
plugin_class = e . load ( )
5
p r i n t e . name , p l u g i n _ c l a s s
6
$ python load_plugins.py
Spam loading!
spam spam_plugin.spam:Spam
Roland Gude
Python Enterprise Application Toolkit (PEAK)