2 # -*- coding: utf-8 -*-
3 # vim: expandtab:shiftwidth=4:fileencoding=utf-8 :
5 # Copyright ® 2008 Fulvio Satta
7 # If you want contact me, send an email to Yota_VGA@users.sf.net
9 # This file is part of jcd
11 # jcd is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # jcd is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #TODO: Test, test, test
28 ##########################
29 ##### IMPORT SECTION #####
30 ##########################
32 from Syncronized
import Syncronized
as _Syncronized
33 from Syncronized
import AutoLock
as _AutoLock
37 ####################################
38 ##### DEFAULT JUNCTORS SECTION #####
39 ####################################
41 #Input junctor for no args reinterpretation
42 def inputDirectJunctor(args
, kwargs
):
45 #Output junctor for no return reinterpretation
46 def outputDirectJunctor(ret
, args
, kwargs
):
49 ################################
50 ##### MAIN CLASSES SECTION #####
51 ################################
54 class Node(_Syncronized
):
55 def __init__(self
, inputJunctor
= inputDirectJunctor
, outputJunctor
= outputDirectJunctor
):
56 _Syncronized
.__init
__(self
)
58 self
.inputJunctor
= inputJunctor
59 self
.outputJunctor
= outputJunctor
63 def __call__(self
, *args
, **kwargs
):
64 rargs
, rkwargs
= self
.inputJunctor(args
, kwargs
)
65 return self
.outputJunctor(self
.work(*rargs
, **rkwargs
), rargs
, rkwargs
)
67 #The work of the node, you must reimplement this
68 def work(self
, *args
, **kwargs
):
69 print >> _sys
.stderr
, 'You must reimplement the work method in the Chain.Node inherited classes!'
70 raise NotImplemented, 'You must reimplement the work method in the Chain.Node inherited classes!'
72 #The chain (nodes must be callable, but not strictly Node classes)
73 class Chain(_Syncronized
):
75 _Syncronized
.__init
__(self
)
80 def __call__(self
, *args
, **kwargs
):
81 rargs
, rkwargs
= args
, kwargs
83 #Recursively work with the nodes
84 for node
in self
.nodes
:
85 rargs
, rkwargs
= node(*rargs
, **rkwargs
)
89 ########################
90 ##### TEST SECTION #####
91 ########################
93 if __name__
== '__main__':
99 t1
.nodes
+= [Try1(), Try1()]
102 print inspect
.getargspec(Try1().work
)