from coalib.bearlib.abstractions.Lint import Lint
from coalib.bears.LocalBear import LocalBear
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY


class JuliaLintBear(LocalBear, Lint):
    executable = 'julia'
    arguments = '-e \'import Lint.lintfile; lintfile("{filename}")\''
    prerequisite_command = ['julia', '-e', 'import Lint.lintfile']
    prerequisite_fail_msg = 'Run `Pkg.add("Lint")` from Julia to install Lint.'
    output_regex = r'(^.*\.jl):(?P<line>\d+) (?P<severity>.)\d+ (?P<message>.*)'
    use_stdout = True
    severity_map = {
        "E": RESULT_SEVERITY.MAJOR,
        "W": RESULT_SEVERITY.NORMAL,
        "I": RESULT_SEVERITY.INFO
    }

    def run(self, filename, file):
        '''
        Lints Julia code using ``Lint.jl``.
        https://github.com/tonyhffong/Lint.jl
        '''
        return self.lint(filename, file)

from coalib.parsing.StringProcessing import escape

from coalib.bearlib.abstractions.Linter import linter
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY


@linter(executable='julia',
        output_format='regex',
        output_regex=r'.+:(?P<line>\d+) (?P<severity>.)\d+ (?P<message>.*)',
        severity_map={'E': RESULT_SEVERITY.MAJOR,
                      'W': RESULT_SEVERITY.NORMAL,
                      'I': RESULT_SEVERITY.INFO},
        prerequisite_check_command=('julia', '-e', 'import Lint.lintfile'),
        prerequisite_check_fail_message='Lint package not installed. Run '
                                        '`Pkg.add("Lint")` from Julia to '
                                        'install Lint.')
class JuliaLintBear:
    """
    Lints Julia code using ``Lint.jl``.
    https://github.com/tonyhffong/Lint.jl
    """

    @staticmethod
    def create_arguments(filename, file, config_file):
        lintcode = ('import Lint.lintfile; lintfile("' +
                    escape(filename, '"\\') + '")')
        return '-e', lintcode

Refactor to Linter v2 API