Skip to content

wake.printers.api module #

Printer class #

Bases: Visitor

Base class for printers.

Attributes:

Name Type Description
paths List[Path]

Paths the printer should operate on. May be empty if a user did not specify any paths, e.g. when running wake print printer-name. In this case, the printer should operate on all paths. May be ignored unless visit_mode is all.

extra Dict[Any, Any]

Extra data set by the execution engine.

Source code in wake/printers/api.py
class Printer(Visitor, metaclass=ABCMeta):
    """
    Base class for printers.

    Attributes:
        paths: Paths the printer should operate on. May be empty if a user did not specify any paths, e.g. when running `wake print printer-name`.
            In this case, the printer should operate on all paths. May be ignored unless [visit_mode][wake.printers.api.Printer.visit_mode] is `all`.
        extra: Extra data set by the execution engine.
    """

    console: Console
    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"]:
        """
        Configurable visit mode of the printer. If set to `paths`, the printer `visit_` methods will be called only for the paths specified by the user.
        If set to `all`, the printer `visit_` methods will be called for all paths. In this case, the printer should use the `paths` attribute to decide what to print.

        Returns:
            Visit mode of the printer.
        """
        return "paths"

    @abstractmethod
    def print(self) -> None:
        """
        Abstract method that must be implemented in every printer. This method is called after all `visit_` methods have been called.
        """
        ...

    def _run(self) -> None:
        from wake.utils.file_utils import is_relative_to

        for path, source_unit in self.build.source_units.items():
            if (
                self.visit_mode == "all"
                or len(self.paths) == 0
                or any(is_relative_to(path, p) for p in self.paths)
            ):
                for node in source_unit:
                    visit_map[node.ast_node.node_type](self, node)

        self.print()

visit_mode: Literal['paths', 'all'] property #

Configurable visit mode of the printer. If set to paths, the printer visit_ methods will be called only for the paths specified by the user. If set to all, the printer visit_ methods will be called for all paths. In this case, the printer should use the paths attribute to decide what to print.

Returns:

Type Description
Literal['paths', 'all']

Visit mode of the printer.

print() abstractmethod #

Abstract method that must be implemented in every printer. This method is called after all visit_ methods have been called.

Source code in wake/printers/api.py
@abstractmethod
def print(self) -> None:
    """
    Abstract method that must be implemented in every printer. This method is called after all `visit_` methods have been called.
    """
    ...