Skip to content

Commit

Permalink
support variadic file args and stdin
Browse files Browse the repository at this point in the history
instead of running the demo when no args are given, use --demo
  • Loading branch information
Jonathan Stewmon committed Mar 7, 2016
1 parent ed88951 commit c74ccb8
Showing 1 changed file with 57 additions and 55 deletions.
112 changes: 57 additions & 55 deletions proselint/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .tools import line_and_column, is_quoted
from . import checks as pl
import pkgutil
import codecs
import subprocess
import ntpath
import re
Expand All @@ -25,6 +24,7 @@

base_url = "proselint.com/"
proselint_path = os.path.dirname(os.path.realpath(__file__))
demo_file = os.path.join(proselint_path, "demo.md")


def log_error(filename, line, column, error_code, msg):
Expand All @@ -49,10 +49,9 @@ def run_initialization():
pass


def lint(path, debug=False):
def lint(input_file, debug=False):
"""Run the linter on the file with the given path."""
# Load the options.
proselint_path = os.path.dirname(os.path.realpath(__file__))
options = js.load(open(os.path.join(proselint_path, '.proselintrc')))

# Extract the checks.
Expand All @@ -68,28 +67,26 @@ def lint(path, debug=False):
checks.append(getattr(module, d))

# Apply all the checks.
text = input_file.read()
errors = []
with codecs.open(path, "r", encoding='utf-8') as f:
text = f.read()
errors = []
for check in checks:
if debug:
click.echo(check.__module__ + "." + check.__name__)
for check in checks:
if debug:
click.echo(check.__module__ + "." + check.__name__)

result = check(text)
result = check(text)

for error in result:
(start, end, check, message) = error
(line, column) = line_and_column(text, start)
if not is_quoted(start, text):
errors += [(check, message, line, column, start, end,
end - start, "warning", None)]
for error in result:
(start, end, check, message) = error
(line, column) = line_and_column(text, start)
if not is_quoted(start, text):
errors += [(check, message, line, column, start, end,
end - start, "warning", None)]

if len(errors) > options["max_errors"]:
break
if len(errors) > options["max_errors"]:
break

# Sort the errors by line and column number.
errors = sorted(errors[:options["max_errors"]])
# Sort the errors by line and column number.
errors = sorted(errors[:options["max_errors"]])

return errors

Expand Down Expand Up @@ -125,16 +122,48 @@ def clear_cache():
shell=True)


def show_errors(filename, errors, json=False):
"""Print the errors, resulting from lint, for filename"""
if json:
out = []
for e in errors:
out.append({
"check": e[0],
"message": e[1],
"line": e[2],
"column": e[3],
"start": e[4],
"end": e[5],
"extent": e[6],
"severity": e[7],
"replacements": e[8],
})
result = dict(
status="success",
data={"errors": out})

click.echo(js.dumps(result))

else:
for error in errors:

(check, message, line, column, start, end,
extent, severity, replacements) = error

log_error(filename, line, column, check, message)


@click.command()
@click.option('--version/--whatever', default=None)
@click.option('--initialize/--i', default=None)
@click.option('--debug/--d', default=False)
@click.option('--score/--s', default=False)
@click.option('--json/--j', default=False)
@click.option('--time/--t', default=False)
@click.argument('file', default=False)
def proselint(file=None, version=None, initialize=None,
debug=None, score=None, json=None, time=None):
@click.option('--demo', default=False, is_flag=True)
@click.argument('files', nargs=-1, type=click.File(encoding='utf8'))
def proselint(files=None, version=None, initialize=None,
debug=None, score=None, json=None, time=None, demo=None):
"""Define the linter command line API."""
# Return the version number.
if version:
Expand All @@ -159,39 +188,12 @@ def proselint(file=None, version=None, initialize=None,
clear_cache()

# Use the demo file by default.
if not file:
file = os.path.join(proselint_path, "demo.md")

errors = lint(file, debug=debug)

# Display the errors.
if json:
out = []
for e in errors:
out.append({
"check": e[0],
"message": e[1],
"line": e[2],
"column": e[3],
"start": e[4],
"end": e[5],
"extent": e[6],
"severity": e[7],
"replacements": e[8],
})
result = dict(
status="success",
data={"errors": out})

click.echo(js.dumps(result))

else:
for error in errors:

(check, message, line, column, start, end,
extent, severity, replacements) = error
if demo:
files = [click.open_file(demo_file, encoding='utf8')]

log_error(file, line, column, check, message)
for f in files:
errors = lint(f, debug=debug)
show_errors(click.format_filename(f.name), errors, json)

if __name__ == '__main__':
proselint()

0 comments on commit c74ccb8

Please sign in to comment.