"Fossies" - the Fresh Open Source Software Archive 
Member "polysh-polysh-0.13/polysh/display_names.py" (11 May 2020, 4047 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "display_names.py" see the
Fossies "Dox" file reference documentation.
1 """Polysh - Displaying Names
2
3 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
4 Copyright (c) 2018 InnoGames GmbH
5 """
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 from typing import Dict, List, Optional
20 from collections import defaultdict
21 # The prefix is the key, the value is a list of booleans. A boolean at an
22 # index is True if that index is currently in use.
23 PREFIXES = defaultdict(lambda: []) # type: Dict[str, List[bool]]
24
25 # dict with key:len(display_name) value:nr of enabled shells with a
26 # display_name of such a length
27 NR_ENABLED_DISPLAY_NAMES_BY_LENGTH = dict() # type: Dict[int, int]
28
29 # Used for terminal size and layout
30 max_display_name_length = 0
31
32
33 def acquire_prefix_index(prefix: str) -> int:
34 # Search and reuse removed host suffix
35 for idx, item in enumerate(PREFIXES[prefix]):
36 if not item:
37 PREFIXES[prefix][idx] = True
38 return idx
39
40 # Add new suffix if no old suffix can be reused
41 PREFIXES[prefix].append(True)
42 return len(PREFIXES[prefix]) - 1
43
44
45 def release_prefix_index(prev_display_name: str) -> None:
46 split = prev_display_name.split('#', 1)
47 prefix = split[0]
48 if len(split) == 1:
49 suffix = 0
50 else:
51 suffix = int(split[1])
52
53 # We are not deleting the host with the highest suffix. Therefore we need
54 # to mark the current suffix index as unused.
55 if suffix < len(PREFIXES[prefix]) - 1:
56 PREFIXES[prefix][suffix] = False
57 return None
58
59 # We are deleting the host with the highest suffix. Therefore we need to
60 # delete it.
61 del PREFIXES[prefix][suffix]
62
63 # Remove holes previously left.
64 for idx, in_use in reversed(list(enumerate(PREFIXES[prefix]))):
65 if in_use:
66 return None
67 del PREFIXES[prefix][idx]
68
69 # If we arrived here, we just deleted the last item with a specific
70 # prefix. Therefore we need to delete the whole prefix now.
71 del PREFIXES[prefix]
72
73
74 def make_unique_name(prefix: str) -> str:
75 suffix = acquire_prefix_index(prefix)
76 if suffix:
77 return '{}#{}'.format(prefix, suffix)
78
79 return prefix
80
81
82 def update_max_display_name_length() -> None:
83 from polysh.dispatchers import update_terminal_size
84 new_max = max(NR_ENABLED_DISPLAY_NAMES_BY_LENGTH.keys(), default=0)
85 global max_display_name_length
86 if new_max != max_display_name_length:
87 max_display_name_length = new_max
88 update_terminal_size()
89
90
91 def change(
92 prev_display_name: Optional[str],
93 new_prefix: Optional[str]
94 ) -> Optional[str]:
95 if new_prefix and '#' in new_prefix:
96 raise Exception('Names cannot contain #')
97
98 if prev_display_name is not None:
99 if new_prefix is not None:
100 set_enabled(prev_display_name, False)
101 release_prefix_index(prev_display_name)
102 if new_prefix is None:
103 return None
104
105 name = make_unique_name(new_prefix)
106 set_enabled(name, True)
107
108 return name
109
110
111 def set_enabled(display_name: str, enabled: bool) -> None:
112 length = len(display_name)
113 if enabled:
114 if length in NR_ENABLED_DISPLAY_NAMES_BY_LENGTH:
115 NR_ENABLED_DISPLAY_NAMES_BY_LENGTH[length] += 1
116 else:
117 NR_ENABLED_DISPLAY_NAMES_BY_LENGTH[length] = 1
118 else:
119 NR_ENABLED_DISPLAY_NAMES_BY_LENGTH[length] -= 1
120 if not NR_ENABLED_DISPLAY_NAMES_BY_LENGTH[length]:
121 del NR_ENABLED_DISPLAY_NAMES_BY_LENGTH[length]
122
123 update_max_display_name_length()