A little bug fix
[jcd.git] / Chain.py
blob06f638a0b0182a97b7ae2443e5040477807aedbc
1 #!/usr/bin/python
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
26 #TODO: Polishing
28 ##########################
29 ##### IMPORT SECTION #####
30 ##########################
32 from Syncronized import Syncronized as _Syncronized
33 from Syncronized import AutoLock as _AutoLock
35 import sys as _sys
37 ####################################
38 ##### DEFAULT JUNCTORS SECTION #####
39 ####################################
41 #Input junctor for no args reinterpretation
42 def inputDirectJunctor(args, kwargs):
43 return (args, kwargs)
45 #Output junctor for no return reinterpretation
46 def outputDirectJunctor(ret, args, kwargs):
47 return ([ret], {})
49 ################################
50 ##### MAIN CLASSES SECTION #####
51 ################################
53 #A node of the chain
54 class Node(_Syncronized):
55 def __init__(self, inputJunctor = inputDirectJunctor, outputJunctor = outputDirectJunctor):
56 _Syncronized.__init__(self)
58 self.inputJunctor = inputJunctor
59 self.outputJunctor = outputJunctor
61 #Call the node
62 @_AutoLock
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):
74 def __init__(self):
75 _Syncronized.__init__(self)
76 self.nodes = []
78 #Call the chain
79 @_AutoLock
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)
87 return rargs, rkwargs
89 ########################
90 ##### TEST SECTION #####
91 ########################
93 if __name__ == '__main__':
94 class Try1(Node):
95 def work(self, var):
96 return var + 1
98 t1 = Chain()
99 t1.nodes += [Try1(), Try1()]
101 import inspect
102 print inspect.getargspec(Try1().work)
104 print t1(1)[0][0]