1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
19 """The `interaction` module provides :class:`Request`,
20 :class:`Response`, an related classes for handling user interaction
22 :class:`hooke.engine.CommandEngine`/:class:`hooke.ui.UserInterface`
28 """:class:`Request` validator class.
33 >>> i = InList(['abc', 'def', 5, True])
37 Traceback (most recent call last):
41 def __init__(self, *args, **kwargs):
42 list.__init__(self, *args, **kwargs)
44 def __call__(self, value):
45 """Raises ValueError if a given `value` is not in our internal
49 raise ValueError(value)
51 class Interaction (object):
52 """Mid-command inter-process interaction.
54 Stores :attr:`type`, a string representing the interaction type
55 ('boolean', 'string', ...).
57 def __init__(self, type):
60 class Request (Interaction):
61 """Command engine requests for information from the UI.
63 def __init__(self, type, response_class,
64 msg, default=None, validator=None):
65 super(Request, self).__init__(type)
66 self.response_class = response_class
68 self.default = default
69 self.validator = validator
71 def response(self, value):
72 if self.validator != None:
74 return self.response_class(value)
76 class Response (Interaction):
77 """UI response to a :class:`Request`.
79 def __init__(self, type, value):
80 super(Response, self).__init__(type)
83 class BooleanRequest (Request):
84 def __init__(self, msg, default=None):
85 super(BooleanRequest, self).__init__(
86 'boolean', BooleanResponse, msg, default,
87 validator = InList([True, False, default]))
89 class BooleanResponse (Response):
90 def __init__(self, value):
91 super(BooleanResponse, self).__init__('boolean', value)
93 class StringRequest (Request):
94 def __init__(self, msg, default=None):
95 super(StringRequest, self).__init__(
96 'string', StringResponse, msg, default)
98 class StringResponse (Response):
99 def __init__(self, value):
100 super(StringResponse, self).__init__('string', value)
102 class FloatRequest (Request):
103 def __init__(self, msg, default=None):
104 super(FloatRequest, self).__init__(
105 'float', FloatResponse, msg, default)
107 class FloatResponse (Response):
108 def __init__(self, value):
109 super(FloatResponse, self).__init__('float', value)
111 class SelectionRequest (Request):
112 def __init__(self, msg, default=None, options=[]):
113 super(SelectionRequest, self).__init__(
114 'selection', SelectionResponse, msg, default)
115 self.options = options
117 class SelectionResponse (Response):
118 def __init__(self, value):
119 super(SelectionResponse, self).__init__('selection', value)
121 class PointRequest (Request):
122 def __init__(self, msg, curve, block=0, default=None):
123 super(PointRequest, self).__init__(
124 'point', PointResponse, msg, default)
125 self.options = options
127 class PointResponse (Response):
128 def __init__(self, value):
129 super(PointResponse, self).__init__('point', value)
132 class Notification (object):
133 def __init__(self, type):
136 class ReloadUserInterfaceConfig (Notification):
137 def __init__(self, config):
138 super(ReloadUserInterfaceConfig, self).__init__(
139 'reload user interface config')