b0055e7c4a0568b09de08a2c6769daff17803776
[hooke.git] / hooke / interaction.py
1 # Copyright
2
3 """The `interaction` module provides :class:`Request`,
4 :class:`Response`, an related classes for handling user interaction
5 through the
6 :class:`hooke.engine.CommandEngine`/:class:`hooke.ui.UserInterface`
7 connection.
8 """
9
10
11 class InList (list):
12     """:class:`Request` validator class.
13
14     Examples
15     --------
16
17     >>> i = InList(['abc', 'def', 5, True])
18     >>> i('abc')
19     >>> i(5)
20     >>> i(False)
21     Traceback (most recent call last):
22       ...
23     ValueError: False
24     """
25     def __init__(self, *args, **kwargs):
26         list.__init__(self, *args, **kwargs)
27
28     def __call__(self, value):
29         """Raises ValueError if a given `value` is not in our internal
30         list.
31         """
32         if value not in self:
33             raise ValueError(value)
34
35 class Interaction (object):
36     """Mid-command inter-process interaction.
37
38     Stores :attr:`type`, a string representing the interaction type
39     ('boolean', 'string', ...).
40     """
41     def __init__(self, type):
42         self.type = type
43
44 class Request (Interaction):
45     """Command engine requests for information from the UI.
46     """
47     def __init__(self, type, response_class,
48                  msg, default=None, validator=None):
49         super(Request, self).__init__(type)
50         self.response_class = response_class
51         self.msg = msg
52         self.default = default
53         self.validator = validator
54
55     def response(self, value):
56         if self.validator != None:
57             self.validator(value)
58         return self.response_class(value)
59
60 class Response (Interaction):
61     """UI response to a :class:`Request`.
62     """
63     def __init__(self, type, value):
64         super(Response, self).__init__(type)
65         self.value = value
66
67 class BooleanRequest (Request):
68     def __init__(self, msg, default=None):
69         super(BooleanRequest, self).__init__(
70             'boolean', BooleanResponse, msg, default,
71             validator = InList([True, False, default]))
72
73 class BooleanResponse (Response):
74     def __init__(self, value):
75         super(BooleanResponse, self).__init__('boolean', value)
76
77 class StringRequest (Request):
78     def __init__(self, msg, default=None):
79         super(StringRequest, self).__init__(
80             'string', StringResponse, msg, default)
81
82 class StringResponse (Response):
83     def __init__(self, value):
84         super(StringResponse, self).__init__('string', value)
85
86 class FloatRequest (Request):
87     def __init__(self, msg, default=None):
88         super(FloatRequest, self).__init__(
89             'float', FloatResponse, msg, default)
90
91 class FloatResponse (Response):
92     def __init__(self, value):
93         super(FloatResponse, self).__init__('float', value)
94
95 class SelectionRequest (Request):
96     def __init__(self, msg, default=None, options=[]):
97         super(SelectionRequest, self).__init__(
98             'selection', SelectionResponse, msg, default)
99         self.options = options
100
101 class SelectionResponse (Response):
102     def __init__(self, value):
103         super(SelectionResponse, self).__init__('selection', value)
104
105 class PointRequest (Request):
106     def __init__(self, msg, curve, block=0, default=None):
107         super(PointRequest, self).__init__(
108             'point', PointResponse, msg, default)
109         self.options = options
110
111 class PointResponse (Response):
112     def __init__(self, value):
113         super(PointResponse, self).__init__('point', value)
114
115
116 class Notification (object):
117     def __init__(self, type):
118         self.type = type
119
120 class ReloadUserInterfaceConfig (Notification):
121     def __init__(self, config):
122         super(ReloadUserInterfaceConfig, self).__init__(
123             'reload user interface config')
124         self.config = config