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 modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # 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 >>> r = Request('test', 'Does response_class work?')
64 >>> r.response_class()
65 <class 'hooke.interaction.Response'>
67 def __init__(self, type, msg, default=None, validator=None):
68 super(Request, self).__init__(type)
70 self.default = default
71 self.validator = validator
73 def response_class(self):
74 class_name = self.__class__.__name__.replace('Request', 'Response')
75 return globals()[class_name]
77 def response(self, value):
78 if self.validator != None:
80 return self.response_class()(value)
82 class Response (Interaction):
83 """UI response to a :class:`Request`.
85 def __init__(self, type, value):
86 super(Response, self).__init__(type)
89 class BooleanRequest (Request):
90 def __init__(self, msg, default=None):
91 super(BooleanRequest, self).__init__(
92 'boolean', msg, default,
93 validator = InList([True, False, default]))
95 class BooleanResponse (Response):
96 def __init__(self, value):
97 super(BooleanResponse, self).__init__('boolean', value)
99 class StringRequest (Request):
100 def __init__(self, msg, default=None):
101 super(StringRequest, self).__init__('string', msg, default)
103 class StringResponse (Response):
104 def __init__(self, value):
105 super(StringResponse, self).__init__('string', value)
107 class FloatRequest (Request):
108 def __init__(self, msg, default=None):
109 super(FloatRequest, self).__init__('float', msg, default)
111 class FloatResponse (Response):
112 def __init__(self, value):
113 super(FloatResponse, self).__init__('float', value)
115 class SelectionRequest (Request):
116 def __init__(self, msg, default=None, options=[]):
117 super(SelectionRequest, self).__init__('selection', msg, default)
118 self.options = options
120 class SelectionResponse (Response):
121 def __init__(self, value):
122 super(SelectionResponse, self).__init__('selection', value)
124 class PointRequest (Request):
125 def __init__(self, msg, curve, block=0, default=None):
126 super(PointRequest, self).__init__('point', msg, default)
130 class PointResponse (Response):
131 def __init__(self, value):
132 super(PointResponse, self).__init__('point', value)
135 class Notification (object):
136 def __init__(self, type):
139 class ReloadUserInterfaceConfig (Notification):
140 def __init__(self, config):
141 super(ReloadUserInterfaceConfig, self).__init__(
142 'reload user interface config')