Skip to content

Commit d872696

Browse files
committed
Simplify get favorites
1 parent e4dc3cb commit d872696

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

pyhon/appliance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
_LOGGER = logging.getLogger(__name__)
2222

23-
T = TypeVar('T')
23+
T = TypeVar("T")
2424

2525

2626
# pylint: disable=too-many-public-methods,too-many-instance-attributes

pyhon/command_loader.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,23 +184,38 @@ def _recover_last_command_states(self) -> None:
184184
def _add_favourites(self) -> None:
185185
"""Patch program categories with favourites"""
186186
for favourite in self._favourites:
187-
name = favourite.get("favouriteName", {})
188-
command = favourite.get("command", {})
189-
command_name = command.get("commandName", "")
190-
program_name = self._clean_name(command.get("programName", ""))
191-
if not (base := self.commands[command_name].categories.get(program_name)):
187+
name, command_name, base = self._get_favourite_info(favourite)
188+
if base:
192189
continue
193190
base_command: HonCommand = copy(base)
194-
for data in command.values():
195-
if isinstance(data, str):
191+
self._update_base_command_with_data(base_command, favourite)
192+
self._update_base_command_with_favourite(base_command)
193+
self._update_program_categories(command_name, name, base_command)
194+
195+
def _get_favourite_info(self, favourite: Dict[str, Any]):
196+
name = favourite.get("favouriteName", {})
197+
command = favourite.get("command", {})
198+
command_name = command.get("commandName", "")
199+
program_name = self._clean_name(command.get("programName", ""))
200+
base_command = self.commands[command_name].categories.get(program_name)
201+
return name, command_name, base_command
202+
203+
def _update_base_command_with_data(self, base_command, command):
204+
for data in command.values():
205+
if isinstance(data, str):
206+
continue
207+
for key, value in data.items():
208+
if not (parameter := base_command.parameters.get(key)):
196209
continue
197-
for key, value in data.items():
198-
if parameter := base_command.parameters.get(key):
199-
with suppress(ValueError):
200-
parameter.value = value
201-
extra_param = HonParameterFixed("favourite", {"fixedValue": "1"}, "custom")
202-
base_command.parameters.update(favourite=extra_param)
203-
program = base_command.parameters["program"]
204-
if isinstance(program, HonParameterProgram):
205-
program.set_value(name)
206-
self.commands[command_name].categories[name] = base_command
210+
with suppress(ValueError):
211+
parameter.value = value
212+
213+
def _update_base_command_with_favourite(self, base_command):
214+
extra_param = HonParameterFixed("favourite", {"fixedValue": "1"}, "custom")
215+
base_command.parameters.update(favourite=extra_param)
216+
217+
def _update_program_categories(self, command_name, name, base_command):
218+
program = base_command.parameters["program"]
219+
if isinstance(program, HonParameterProgram):
220+
program.set_value(name)
221+
self.commands[command_name].categories[name] = base_command

0 commit comments

Comments
 (0)