Skip to content

Commit

Permalink
Clean up code as per a very helpful review.
Browse files Browse the repository at this point in the history
  - Ensure internal state is appropriately initialized.
  - Use clear() as a consistent way to initialize the singleton.
  - Instead of passing data via inter-object variables, pass data through methods. A novel idea, it may catch on. :)
  • Loading branch information
terjekv committed Nov 22, 2023
1 parent 7e1b441 commit 56a47b2
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions mreg_cli/outputmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class OutputManager:

def __new__(cls):
if cls._instance is None:
cls._instance = super(OutputManager, cls).__new__(cls)
cls._instance._lines = []
cls._instance = super().__new__(cls)
cls._instance.clear()
return cls._instance

def clear(self) -> None:
Expand All @@ -32,6 +32,7 @@ def clear(self) -> None:
self._filter_re = None
self._negate = False
self._command = None
self.command = None

def has_output(self) -> bool:
"""Returns True if there is output to display."""
Expand All @@ -44,8 +45,7 @@ def from_command(self, command: str) -> str:
:return: The cleaned command, devoid of filters and other noise.
"""
self.command = command
self._command, self._filter_re, self._negate = self.get_filter()
self._command, self._filter_re, self._negate = self.get_filter(command)
return self._command

def add_line(self, line: str) -> None:
Expand Down Expand Up @@ -80,12 +80,14 @@ def add_formatted_line_with_source(

# We want to use re.Pattern as the type here, but python 3.6 and older re-modules
# don't have that type. So we use Any instead.
def get_filter(self) -> Tuple[str, Any, bool]:
def get_filter(self, command: str) -> Tuple[str, Any, bool]:
"""Returns the filter for the output.
:param command: The command to parse for the filter.
:return: The filter and whether it is a negated filter.
"""
command = self.command
self.command = command
negate = False
filter_re = None

Expand Down

0 comments on commit 56a47b2

Please sign in to comment.