Switch to unified view

a/src/cdplugins/pycommon/cmdtalkplugin.py b/src/cdplugins/pycommon/cmdtalkplugin.py
1
/home/dockes/projets/docklib/cmdtalk/cmdtalkplugin.py
1
#!/usr/bin/env python
2
# Copyright (C) 2016 J.F.Dockes
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the
15
# Free Software Foundation, Inc.,
16
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18
from __future__ import print_function
19
20
import cmdtalk
21
22
prcnmkey = "cmdtalk:proc"
23
24
class Dispatch:
25
    def __init__(self):
26
        self.map = {}
27
        
28
    def record(self, nm):
29
        def decorator(func):
30
            self.map[nm] = func
31
            return func
32
        return decorator
33
34
    def run(self, nm, params):
35
        func = self.map[nm]
36
        return func(params)
37
    
38
class Processor:
39
    def __init__(self, dispatcher):
40
        self.em = cmdtalk.CmdTalk()
41
        self.dispatcher = dispatcher
42
        
43
    def log(self, s, doexit = 0, exitvalue = 1):
44
        self.em.log(s, doexit, exitvalue)
45
        
46
    def process(self, params):
47
        self.em.log("pCmdTalkProcessor.process: [%s]" % params)
48
        if not prcnmkey in params:
49
            raise Exception('%s not in args' % prcnmkey)
50
        
51
        return self.dispatcher.run(params[prcnmkey], params)
52
53
    def mainloop(self):
54
        cmdtalk.main(self.em, self)