Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions pylint_runner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import colorama
import pylint
import pylint.lint
from pylint.lint.pylinter import PyLinter

from pylint_runner import __version__

Expand All @@ -23,7 +24,6 @@ class Runner:

DEFAULT_IGNORE_FOLDERS = [".git", ".idea", "__pycache__"]
DEFAULT_ARGS = ["--reports=n", "--output-format=colorized"]
DEFAULT_RCFILE = ".pylintrc"

def __init__(self, args=None):
if args is None:
Expand All @@ -32,8 +32,8 @@ def __init__(self, args=None):
colorama.init(autoreset=True)

self.verbose = False
self.args = self.DEFAULT_ARGS
self.rcfile = self.DEFAULT_RCFILE
self.args = list(self.DEFAULT_ARGS)
self.rcfile = None
self.ignore_folders = self.DEFAULT_IGNORE_FOLDERS

self._parse_args(args)
Expand All @@ -59,9 +59,7 @@ def _parse_args(self, args):
"--rcfile",
dest="rcfile",
action="store",
default=".pylintrc",
help="A relative or absolute path to your pylint rcfile. Defaults to\
`.pylintrc` at the current working directory",
help="A relative or absolute path to your pylint rcfile.",
)

parser.add_argument(
Expand All @@ -78,10 +76,7 @@ def _parse_args(self, args):

self.verbose = options.verbose

if options.rcfile:
if not os.path.isfile(options.rcfile):
options.rcfile = os.getcwd() + "/" + options.rcfile
self.rcfile = options.rcfile
self.rcfile = options.rcfile

self.args += rest

Expand All @@ -98,25 +93,25 @@ def _parse_ignores(self):
+ colorama.Fore.RESET
)

if not os.path.isfile(self.rcfile):
if not self._is_using_default_rcfile():
print(error_message)
sys.exit(1)
else:
return
if self.rcfile is not None and not os.path.isfile(self.rcfile):
print(error_message)
sys.exit(1)

config = configparser.ConfigParser()
linter = PyLinter(pylintrc=self.rcfile)
try:
config.read(self.rcfile)
linter.read_config_file()
except configparser.MissingSectionHeaderError:
print(error_message)
sys.exit(1)

if config.has_section("MASTER") and config.get("MASTER", "ignore"):
self.ignore_folders += config.get("MASTER", "ignore").split(",")

def _is_using_default_rcfile(self):
return self.rcfile == os.getcwd() + "/" + self.DEFAULT_RCFILE
config = linter.cfgfile_parser
if config.has_option("MASTER", "ignore"):
self.ignore_folders += [
word.strip()
for word
in config.get("MASTER", "ignore").split(",")
if word.strip()
]

def _print_line(self, line):
""" Print output only with verbose flag. """
Expand Down Expand Up @@ -187,7 +182,7 @@ def run(self, output=None, error=None):
self._print_line("- " + pylint_file)
self._print_line("----")

if not self._is_using_default_rcfile():
if self.rcfile is not None:
# insert this at the front so it's not after any potential
# positional arguments
self.args.insert(0, "--rcfile={}".format(self.rcfile))
Expand Down
2 changes: 0 additions & 2 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ def _assert_list_equals(list1, list2):
def test_passthrough_args():
the_runner = runner.Runner(args=['-d', 'C0103', '-d', 'E0602'])
expected = [
'--rcfile=tests/tests/good_rc_file',
'--rcfile=.pylintrc',
'--reports=n',
'--output-format=colorized',
'-d',
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/good_rc_file
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
ignore=migrations

[FORMAT]
max-line-length=15
max-line-length=100