comparison libervia/frontends/tools/misc.py @ 4074:26b7ed2817da

refactoring: rename `sat_frontends` to `libervia.frontends`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:12:38 +0200
parents sat_frontends/tools/misc.py@524856bd7b19
children
comparison
equal deleted inserted replaced
4073:7c5654c54fed 4074:26b7ed2817da
1 #!/usr/bin/env python3
2
3
4 # SAT helpers methods for plugins
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 class InputHistory(object):
22 def _update_input_history(self, text=None, step=None, callback=None, mode=""):
23 """Update the lists of previously sent messages. Several lists can be
24 handled as they are stored in a dictionary, the argument "mode" being
25 used as the entry key. There's also a temporary list to allow you play
26 with previous entries before sending a new message. Parameters values
27 can be combined: text is None and step is None to initialize a main
28 list and the temporary one, step is None to update a list and
29 reinitialize the temporary one, step is not None to update
30 the temporary list between two messages.
31 @param text: text to be saved.
32 @param step: step to move the temporary index.
33 @param callback: method to display temporary entries.
34 @param mode: the dictionary key for main lists.
35 """
36 if not hasattr(self, "input_histories"):
37 self.input_histories = {}
38 history = self.input_histories.setdefault(mode, [])
39 if step is None and text is not None:
40 # update the main list
41 if text in history:
42 history.remove(text)
43 history.append(text)
44 length = len(history)
45 if step is None or length == 0:
46 # prepare the temporary list and index
47 self.input_history_tmp = history[:]
48 self.input_history_tmp.append("")
49 self.input_history_index = length
50 if step is None:
51 return
52 # update the temporary list
53 if text is not None:
54 # save the current entry
55 self.input_history_tmp[self.input_history_index] = text
56 # move to another entry if possible
57 index_tmp = self.input_history_index + step
58 if index_tmp >= 0 and index_tmp < len(self.input_history_tmp):
59 if callback is not None:
60 callback(self.input_history_tmp[index_tmp])
61 self.input_history_index = index_tmp
62
63
64 class FlagsHandler(object):
65 """Small class to handle easily option flags
66
67 the instance is initialized with an iterable
68 then attribute return True if flag is set, False else.
69 """
70
71 def __init__(self, flags):
72 self.flags = set(flags or [])
73 self._used_flags = set()
74
75 def __getattr__(self, flag):
76 self._used_flags.add(flag)
77 return flag in self.flags
78
79 def __getitem__(self, flag):
80 return getattr(self, flag)
81
82 def __len__(self):
83 return len(self.flags)
84
85 def __iter__(self):
86 return self.flags.__iter__()
87
88 @property
89 def all_used(self):
90 """Return True if all flags have been used"""
91 return self._used_flags.issuperset(self.flags)
92
93 @property
94 def unused(self):
95 """Return flags which has not been used yet"""
96 return self.flags.difference(self._used_flags)