-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change format to use new stages style
- Loading branch information
Showing
3 changed files
with
109 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright(C) 2024 Red Hat, Inc. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
|
||
class ConversionPhase: | ||
""" | ||
Conversion phase to hold name and logging-friendly name. | ||
""" | ||
|
||
def __init__(self, name, log_name=None): # type: (str, str|None) -> None | ||
self.name = name | ||
self.log_name = log_name | ||
|
||
def __str__(self): | ||
return self.log_name if self.log_name else self.name | ||
|
||
def __repr__(self): | ||
return self.name | ||
|
||
def __hash__(self): | ||
Check warning Code scanning / CodeQL Inconsistent equality and hashing Warning
Class
ConversionPhase Error loading related location Loading |
||
return hash(self.name) | ||
|
||
|
||
class ConversionPhases: | ||
"""During conversion we will be in different states depending on | ||
where we are in the execution. This class establishes the different phases | ||
that we have as well as what the current phase is set to. | ||
""" | ||
|
||
POST_CLI = ConversionPhase(name="POST_CLI") | ||
PREPARE = ConversionPhase(name="PREPARE", log_name="Prepare") | ||
# PONR means Point Of No Return | ||
PRE_PONR_CHANGES = ConversionPhase(name="PRE_PONR_CHANGES", log_name="Analyze") | ||
# Phase to exit the Analyze SubCommand early | ||
ANALYZE_EXIT = ConversionPhase(name="ANALYZE_EXIT") | ||
POST_PONR_CHANGES = ConversionPhase(name="POST_PONR_CHANGES", log_name="Convert") | ||
ROLLBACK = (ConversionPhase(name="ROLLBACK", log_name="Rollback"),) | ||
|
||
current_phase = None # type: ConversionPhase|None | ||
|
||
@classmethod | ||
def get(cls, key): # type: (str) -> ConversionPhase | ||
return next((phase for phase in cls.__dict__ if isinstance(phase, ConversionPhase) and phase.name == key)) | ||
|
||
@classmethod | ||
def has(cls, key): # type: (str) -> bool | ||
try: | ||
cls.get(key) | ||
return True | ||
except StopIteration: | ||
return False | ||
|
||
@classmethod | ||
def set_current(cls, phase): # type: (str|ConversionPhase|None) -> None | ||
if phase is None: | ||
cls.current_phase = None | ||
elif isinstance(phase, str) and cls.has(phase): | ||
cls.current_phase = cls.get(phase) | ||
elif isinstance(phase, ConversionPhase) and phase.name in cls.__dict__: | ||
cls.current_phase = phase | ||
else: | ||
raise NotImplementedError("The {} phase is not implemented in the {} class".format(phase, cls.__name__)) | ||
|
||
@classmethod | ||
def is_current(cls, phase): # type: (str|ConversionPhase) -> bool | ||
if isinstance(phase, str): | ||
return cls.current_phase == cls.get(phase) | ||
elif isinstance(phase, ConversionPhase) and phase.name in cls.__dict__: | ||
return cls.current_phase == phase | ||
raise TypeError("Unexpected type, wanted str or {}".format(ConversionPhase.__name__)) |