1 |
# Copyright (C) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
7 |
|
8 |
""" |
9 |
Command Objects. |
10 |
|
11 |
Command objects represent a command that a user can invoke and act as |
12 |
mediators between the GUI and the application. |
13 |
|
14 |
This module also defines a command registry that maps command names to |
15 |
command objects. |
16 |
""" |
17 |
|
18 |
__version__ = "$Revision$" |
19 |
|
20 |
|
21 |
from types import TupleType |
22 |
|
23 |
|
24 |
class Command: |
25 |
|
26 |
""" |
27 |
Represent a single command. |
28 |
|
29 |
A command is identified by a name, it has a title (used in menu |
30 |
items and buttons, etc) and a callable object that can be invoked |
31 |
with the context as a single parameter. The context is an object |
32 |
with a few public attributes for the application object, the session |
33 |
and the main window. |
34 |
|
35 |
Additionally, a command may have functions that can determine |
36 |
whether the command can be invoked or whether it is checked in case |
37 |
of a toggled command. These functions are called with just the |
38 |
context as parameters. |
39 |
""" |
40 |
|
41 |
args = () |
42 |
kwargs = None |
43 |
sensitive = None |
44 |
checked = None |
45 |
dyntext = None |
46 |
|
47 |
def __init__(self, name, title, function, helptext = "", icon = "", |
48 |
args = (), kwargs = None, |
49 |
sensitive = None, checked = None, dyntext = None): |
50 |
self.name = name |
51 |
self.title = title |
52 |
self.function = function |
53 |
self.helptext = helptext |
54 |
self.icon = icon |
55 |
if args != (): |
56 |
if type(args) != TupleType: |
57 |
args = (args,) |
58 |
self.args = args |
59 |
if kwargs is not None: |
60 |
self.kwargs = kwargs |
61 |
if sensitive is not None: |
62 |
self.sensitive = sensitive |
63 |
if checked is not None: |
64 |
self.checked = checked |
65 |
if dyntext is not None: |
66 |
self.dyntext = dyntext |
67 |
|
68 |
def Name(self): |
69 |
return self.name |
70 |
|
71 |
def Title(self): |
72 |
return self.title |
73 |
|
74 |
def HelpText(self): |
75 |
return self.helptext |
76 |
|
77 |
def Icon(self): |
78 |
return self.icon |
79 |
|
80 |
def Sensitive(self, context): |
81 |
if self.sensitive is not None: |
82 |
return self.sensitive(context) |
83 |
return 1 |
84 |
|
85 |
def Checked(self, context): |
86 |
if self.checked is not None: |
87 |
return self.checked(context) |
88 |
return 0 # XXX raise an exception? |
89 |
|
90 |
def IsCheckCommand(self): |
91 |
return self.checked is not None |
92 |
|
93 |
def DynText(self, context): |
94 |
if self.dyntext is not None: |
95 |
return self.dyntext(context) |
96 |
return self.Title() |
97 |
|
98 |
def HasDynText(self): |
99 |
return self.dyntext is not None |
100 |
|
101 |
def IsDynamic(self): |
102 |
"""Return true if the command is in any way dynamic""" |
103 |
return (self.sensitive is not None |
104 |
or self.checked is not None |
105 |
or self.dyntext is not None) |
106 |
|
107 |
def Execute(self, context, args = ()): |
108 |
kw = self.kwargs |
109 |
if kw is None: |
110 |
kw = {} |
111 |
if type(args) != TupleType: |
112 |
args = (args,) |
113 |
#print self.name, self.args, args |
114 |
apply(self.function, (context,) + self.args + args, kw) |
115 |
|
116 |
|
117 |
class CommandRegistry: |
118 |
|
119 |
""" |
120 |
A CommandRegistry maps command names to command objects |
121 |
""" |
122 |
|
123 |
def __init__(self): |
124 |
self.registry = {} |
125 |
|
126 |
def Add(self, script): |
127 |
self.registry[script.name] = script |
128 |
|
129 |
def AddFunction(self, name, title, function, args = (), sensitive = None): |
130 |
self.Add(Command(name, title, function, args = args, |
131 |
sensitive = sensitive)) |
132 |
|
133 |
def Command(self, name): |
134 |
return self.registry.get(name, None) |
135 |
|
136 |
|
137 |
|
138 |
# The central command registry |
139 |
registry = CommandRegistry() |