state.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright © 2025 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from .const import ERROR_SINK_STATE_ID
  13. def get_state_from_id(states: list, state_id: int) -> dict:
  14. for state in states:
  15. possible_id = state.get("id", None)
  16. if isinstance(possible_id, int):
  17. if possible_id == state_id:
  18. return state
  19. return {}
  20. def contains_id(states: list, state_id: int) -> bool:
  21. found_id = False
  22. for found_states in states:
  23. if found_states["id"] == state_id:
  24. found_id = True
  25. break
  26. return found_id
  27. def get_state_id_from_exit_code(states: list, exit_code: int) -> int:
  28. if not contains_id(states, exit_code):
  29. exit_code = ERROR_SINK_STATE_ID
  30. return exit_code
  31. def get_state_ids(states: list) -> list:
  32. output = set()
  33. for state in states:
  34. possible_id = state.get("id", None)
  35. if isinstance(possible_id, int):
  36. output.add(possible_id)
  37. return list(output)