Merged my unitary FFT wrappers (FFT_tools) as hooke.util.fft.
[hooke.git] / hooke / interaction.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
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.
9 #
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.
14 #
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/>.
18
19 """The `interaction` module provides :class:`Request`,
20 :class:`Response`, an related classes for handling user interaction
21 through the
22 :class:`hooke.engine.CommandEngine`/:class:`hooke.ui.UserInterface`
23 connection.
24 """
25
26
27 class InList (list):
28     """:class:`Request` validator class.
29
30     Examples
31     --------
32
33     >>> i = InList(['abc', 'def', 5, True])
34     >>> i('abc')
35     >>> i(5)
36     >>> i(False)
37     Traceback (most recent call last):
38       ...
39     ValueError: False
40     """
41     def __init__(self, *args, **kwargs):
42         list.__init__(self, *args, **kwargs)
43
44     def __call__(self, value):
45         """Raises ValueError if a given `value` is not in our internal
46         list.
47         """
48         if value not in self:
49             raise ValueError(value)
50
51 class Interaction (object):
52     """Mid-command inter-process interaction.
53
54     Stores :attr:`type`, a string representing the interaction type
55     ('boolean', 'string', ...).
56     """
57     def __init__(self, type):
58         self.type = type
59
60 class Request (Interaction):
61     """Command engine requests for information from the UI.
62     """
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
67         self.msg = msg
68         self.default = default
69         self.validator = validator
70
71     def response(self, value):
72         if self.validator != None:
73             self.validator(value)
74         return self.response_class(value)
75
76 class Response (Interaction):
77     """UI response to a :class:`Request`.
78     """
79     def __init__(self, type, value):
80         super(Response, self).__init__(type)
81         self.value = value
82
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]))
88
89 class BooleanResponse (Response):
90     def __init__(self, value):
91         super(BooleanResponse, self).__init__('boolean', value)
92
93 class StringRequest (Request):
94     def __init__(self, msg, default=None):
95         super(StringRequest, self).__init__(
96             'string', StringResponse, msg, default)
97
98 class StringResponse (Response):
99     def __init__(self, value):
100         super(StringResponse, self).__init__('string', value)
101
102 class FloatRequest (Request):
103     def __init__(self, msg, default=None):
104         super(FloatRequest, self).__init__(
105             'float', FloatResponse, msg, default)
106
107 class FloatResponse (Response):
108     def __init__(self, value):
109         super(FloatResponse, self).__init__('float', value)
110
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
116
117 class SelectionResponse (Response):
118     def __init__(self, value):
119         super(SelectionResponse, self).__init__('selection', value)
120
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
126
127 class PointResponse (Response):
128     def __init__(self, value):
129         super(PointResponse, self).__init__('point', value)
130
131
132 class Notification (object):
133     def __init__(self, type):
134         self.type = type
135
136 class ReloadUserInterfaceConfig (Notification):
137     def __init__(self, config):
138         super(ReloadUserInterfaceConfig, self).__init__(
139             'reload user interface config')
140         self.config = config