#!/usr/bin/env python
#   pipescaler/core/processor.py
#
#   Copyright (C) 2020-2021 <NAME>
#   All rights reserved.
#
#   This software may be modified and distributed under the terms of the
#   BSD license.
from __future__ import annotations

from abc import abstractmethod
from argparse import ArgumentParser
from inspect import cleandoc
from typing import Any, List, Optional

from pipescaler.common import CLTool
from pipescaler.core.stage import Stage


class Processor(Stage, CLTool):
    """Base class for processors"""

    def __init__(self, suffix: Optional[str] = None, **kwargs: Any) -> None:
        """
        Validates and stores static configuration.

        Arguments:
            suffix (Optional[str]): suffix to append to images
            kwargs (Any): Additional keyword arguments
        """
        super().__init__(**kwargs)

        # Store configuration
        if suffix is not None:
            self.suffix = suffix
        else:
            self.suffix = self.name

    def __call__(self, infile: str, outfile: str) -> None:
        """
        Processes infile and writes the resulting output to outfile.

        Arguments:
            infile (str): Input file
            outfile (str): Output file
        """
        self.process_file(infile, outfile)

    @property
    def inlets(self) -> List[str]:
        return ["inlet"]

    @property
    def outlets(self) -> List[str]:
        return ["outlet"]

    @abstractmethod
    def process_file(cls, infile: str, outfile: str) -> None:
        raise NotImplementedError()

    @classmethod
    def construct_argparser(cls, **kwargs: Any) -> ArgumentParser:
        """
        Constructs argument parser.

        Args:
            kwargs (Any): Additional keyword arguments

        Returns:
            parser (ArgumentParser): Argument parser
        """
        description = kwargs.pop("description", cleandoc(cls.__doc__))
        parser = super().construct_argparser(description=description, **kwargs)

        # Input
        parser.add_argument("infile", type=cls.input_path_arg(), help="input file")
        parser.add_argument("outfile", type=cls.output_path_arg(), help="output file")

        return parser

    @classmethod
    def main(cls) -> None:
        """Parses arguments, initializes processor, and processes file."""
        parser = cls.construct_argparser()
        kwargs = vars(parser.parse_args())
        infile = kwargs.pop("infile")
        outfile = kwargs.pop("outfile")
        cls(**kwargs)(infile, outfile)

#!/usr/bin/env python
#   pipescaler/core/processor.py
#
#   Copyright (C) 2020-2021 <NAME>
#   All rights reserved.
#
#   This software may be modified and distributed under the terms of the
#   BSD license.
from __future__ import annotations

from abc import abstractmethod
from argparse import ArgumentParser
from inspect import cleandoc
from typing import Any, List, Optional

from pipescaler.common import CLTool
from pipescaler.core.stage import Stage


class Processor(Stage, CLTool):
    """Base class for processors"""

    def __init__(self, suffix: Optional[str] = None, **kwargs: Any) -> None:
        """
        Validates and stores static configuration.

        Arguments:
            suffix (Optional[str]): suffix to append to images
            kwargs (Any): Additional keyword arguments
        """
        super().__init__(**kwargs)

        # Store configuration
        if suffix is not None:
            self.suffix = suffix
        else:
            self.suffix = self.name

    def __call__(self, infile: str, outfile: str) -> None:
        """
        Processes infile and writes the resulting output to outfile.

        Arguments:
            infile (str): Input file
            outfile (str): Output file
        """
        self.process_file(infile, outfile)

    @property
    def inlets(self) -> List[str]:
        return ["inlet"]

    @property
    def outlets(self) -> List[str]:
        return ["outlet"]

    @abstractmethod
    def process_file(cls, infile: str, outfile: str) -> None:
        raise NotImplementedError()

    @classmethod
    def construct_argparser(cls, **kwargs: Any) -> ArgumentParser:
        """
        Constructs argument parser.

        Args:
            kwargs (Any): Additional keyword arguments

        Returns:
            parser (ArgumentParser): Argument parser
        """
        description = kwargs.pop("description", cleandoc(cls.__doc__))
        parser = super().construct_argparser(description=description, **kwargs)

        # Input
        parser.add_argument("infile", type=cls.input_path_arg(), help="input file")
        parser.add_argument("outfile", type=cls.output_path_arg(), help="output file")

        return parser

    @classmethod
    def main(cls) -> None:
        """Parses arguments, initializes processor, and processes file."""
        parser = cls.construct_argparser()
        kwargs = vars(parser.parse_args())
        infile = kwargs.pop("infile")
        outfile = kwargs.pop("outfile")
        cls(**kwargs)(infile, outfile)

#!/usr/bin/env python #   pipescaler/core/processor.py # #   Copyright (C) 2020-2021 <NAME> #   All rights reserved. # #   This software may be modified and distributed under the terms of the #   BSD license. Base class for processors Validates and stores static configuration.          Arguments:             suffix (Optional[str]): suffix to append to images             kwargs (Any): Additional keyword arguments # Store configuration Processes infile and writes the resulting output to outfile.          Arguments:             infile (str): Input file             outfile (str): Output file Constructs argument parser.          Args:             kwargs (Any): Additional keyword arguments          Returns:             parser (ArgumentParser): Argument parser # Input Parses arguments, initializes processor, and processes file.