Skip to content

Commit

Permalink
🏗️ Make execution_mode class attribute to avoid running init
Browse files Browse the repository at this point in the history
  • Loading branch information
michprev committed Jan 30, 2024
1 parent 980db5b commit 723e470
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 38 deletions.
27 changes: 8 additions & 19 deletions wake/detectors/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class Detector(Visitor, metaclass=ABCMeta):
paths: List[Path]
extra: Dict[Any, Any]
lsp_provider: Optional[LspProvider]
execution_mode: Literal["cli", "lsp", "both"] = "both" # TODO is this needed?

@property
def visit_mode(self) -> Literal["paths", "all"]:
Expand All @@ -151,18 +152,6 @@ def visit_mode(self) -> Literal["paths", "all"]:
"""
return "paths"

# TODO is this needed?
@property
def execution_mode(self) -> Literal["cli", "lsp", "both"]:
"""
Configurable execution mode of the detector. If set to `cli`, the detector will be executed only when running `wake detect`.
If set to `lsp`, the detector will be executed by the LSP server. If set to `both`, the detector will be executed in both cases.
Returns:
Execution mode of the detector.
"""
return "both"

@abstractmethod
def detect(self) -> List[DetectorResult]:
"""
Expand Down Expand Up @@ -497,6 +486,13 @@ def _callback( # pyright: ignore reportGeneralTypeIssues
original_callback = command.callback
command.callback = partial(_callback, command.name)

if lsp_provider is not None and cls.execution_mode == "cli":
detectors.remove(command)
continue
elif lsp_provider is None and cls.execution_mode == "lsp":
detectors.remove(command)
continue

try:
instance = object.__new__(cls)
instance.build = build
Expand All @@ -521,13 +517,6 @@ def _callback( # pyright: ignore reportGeneralTypeIssues
with sub_ctx:
sub_ctx.command.invoke(sub_ctx)

if lsp_provider is not None and instance.execution_mode == "cli":
detectors.remove(command)
continue
elif lsp_provider is None and instance.execution_mode == "lsp":
detectors.remove(command)
continue

collected_detectors[command.name] = instance
if instance.visit_mode == "all":
visit_all_detectors.add(command.name)
Expand Down
27 changes: 8 additions & 19 deletions wake/printers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Printer(Visitor, metaclass=ABCMeta):
paths: List[Path]
extra: Dict[Any, Any]
lsp_provider: Optional[LspProvider]
execution_mode: Literal["cli", "lsp", "both"] = "cli" # TODO remove both?

@property
def visit_mode(self) -> Literal["paths", "all"]:
Expand All @@ -63,18 +64,6 @@ def visit_mode(self) -> Literal["paths", "all"]:
"""
return "paths"

# TODO remove both?
@property
def execution_mode(self) -> Literal["cli", "lsp", "both"]:
"""
Configurable execution mode of the printer. If set to `cli`, the printer will be executed only when running `wake print printer-name`.
If set to `lsp`, the printer will be executed by the LSP server. If set to `both`, the printer will be executed in both cases.
Returns:
Execution mode of the printer.
"""
return "cli"

@abstractmethod
def print(self) -> None:
"""
Expand Down Expand Up @@ -292,6 +281,13 @@ def _callback( # pyright: ignore reportGeneralTypeIssues
original_callback = command.callback
command.callback = partial(_callback, command.name)

if lsp_provider is not None and cls.execution_mode == "cli":
printers.remove(command)
continue
elif lsp_provider is None and cls.execution_mode == "lsp":
printers.remove(command)
continue

try:
instance = object.__new__(cls)
instance.build = build
Expand All @@ -317,13 +313,6 @@ def _callback( # pyright: ignore reportGeneralTypeIssues
with sub_ctx:
sub_ctx.command.invoke(sub_ctx)

if lsp_provider is not None and instance.execution_mode == "cli":
printers.remove(command)
continue
elif lsp_provider is None and instance.execution_mode == "lsp":
printers.remove(command)
continue

collected_printers[command.name] = instance
if instance.visit_mode == "all":
visit_all_printers.add(command.name)
Expand Down

0 comments on commit 723e470

Please sign in to comment.