|
| 1 | +# BSD 2-Clause License |
| 2 | +# |
| 3 | +# Copyright (c) 2021-2024, Hewlett Packard Enterprise |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +import typing as t |
| 28 | +from collections.abc import MutableSequence |
| 29 | + |
| 30 | +from ...settings.launchCommand import LauncherType |
| 31 | + |
| 32 | + |
| 33 | +class Command(MutableSequence[str]): |
| 34 | + """Basic container for command information""" |
| 35 | + |
| 36 | + def __init__(self, launcher: LauncherType, command: t.List[str]) -> None: |
| 37 | + """Command constructor""" |
| 38 | + self._launcher = launcher |
| 39 | + self._command = command |
| 40 | + |
| 41 | + @property |
| 42 | + def launcher(self) -> LauncherType: |
| 43 | + """Get the launcher type. |
| 44 | + Return a reference to the LauncherType. |
| 45 | + """ |
| 46 | + return self._launcher |
| 47 | + |
| 48 | + @property |
| 49 | + def command(self) -> t.List[str]: |
| 50 | + """Get the command list. |
| 51 | + Return a reference to the command list. |
| 52 | + """ |
| 53 | + return self._command |
| 54 | + |
| 55 | + def __getitem__(self, idx: int) -> str: |
| 56 | + """Get the command at the specified index.""" |
| 57 | + return self._command[idx] |
| 58 | + |
| 59 | + def __setitem__(self, idx: int, value: str) -> None: |
| 60 | + """Set the command at the specified index.""" |
| 61 | + self._command[idx] = value |
| 62 | + |
| 63 | + def __delitem__(self, idx: int) -> None: |
| 64 | + """Delete the command at the specified index.""" |
| 65 | + del self._command[idx] |
| 66 | + |
| 67 | + def __len__(self) -> int: |
| 68 | + """Get the length of the command list.""" |
| 69 | + return len(self._command) |
| 70 | + |
| 71 | + def insert(self, idx: int, value: str) -> None: |
| 72 | + """Insert a command at the specified index.""" |
| 73 | + self._command.insert(idx, value) |
| 74 | + |
| 75 | + def __str__(self) -> str: # pragma: no cover |
| 76 | + string = f"\nLauncher: {self.launcher.value}\n" |
| 77 | + string += f"Command: {' '.join(str(cmd) for cmd in self.command)}" |
| 78 | + return string |
0 commit comments