from os.path import abspath, dirname, join
import re

from coalib.bearlib.abstractions.Lint import Lint
from coalib.bears.LocalBear import LocalBear
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.misc.Shell import escape_path_argument


class ScalaLintBear(LocalBear, Lint):
    executable = 'java'
    current_file_dir = dirname(abspath(__file__))
    jar = join(current_file_dir, 'scalastyle.jar')
    scalastyle_config_file = join(current_file_dir, 'scalastyle_config.xml')

    output_regex = re.compile(
        r'(?P<severity>warning)\s(file=)(?P<filename>.+)\s(message=)'
        r'(?P<message>.+)(line=)(?P<line>\d+)(\s(column=)(?P<column>\d+))*'
    )

    severity_map = {
        "warning": RESULT_SEVERITY.NORMAL
    }

    prerequisite_command = [
        "java", "-jar", jar, "-c",
        scalastyle_config_file, ".", "-q", "true"
    ]

    prerequisite_fail_msg = ("jar file {} is invalid"
                             "and cannot be used".format(jar))

    def run(self,
            filename,
            file,
            scalalint_config: str=""):
        '''
        Checks the code with `scalastyle` on each file separately.

        :param scalalint_config: Path to a custom configuration file.
        '''
        self.arguments = ' -jar ' + self.jar
        self.arguments += ' {filename}'
        scala_config_file = self.scalastyle_config_file
        if scalalint_config:
            scala_config_file = scalalint_config
        self.arguments += (' --config ' +
                           escape_path_argument(scala_config_file))
        return self.lint(filename)

from os.path import abspath, dirname, join

from coalib.bearlib.abstractions.Linter import linter


jar = join(dirname(abspath(__file__)), 'scalastyle.jar')
scalastyle_config_file = join(dirname(abspath(__file__)),
                              'scalastyle_config.xml')


@linter(executable='java',
        output_format='regex',
        output_regex=r'(?P<severity>warning) file=.+ message=(?P<message>.+) '
                     r'line=(?P<line>\d+)(?: column=(?P<column>\d+))?',
        prerequisite_check_command=('java', '-jar', jar, '-c',
                                    scalastyle_config_file, '.', '-q', 'true'),
        prerequisite_check_fail_message='Required jar file ' + jar +
                                        ' not found.')
class ScalaLintBear:
    """
    Checks the code with ``scalastyle`` on each file separately.
    """

    @staticmethod
    def create_arguments(filename, file, config_file,
                         scalalint_config: str=scalastyle_config_file):
        """
        :param scalalint_config: Path to a custom configuration file.
        """
        return '-jar', jar, filename, '--config', scalalint_config

Refactor to Linter v2 API