#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Command line utility for proselint."""

import click
import os
from proselint.tools import line_and_column
import proselint.checks as pl
import pkgutil
import codecs
import subprocess


base_url = "prose.lifelinter.com/"
proselint_path = os.path.dirname(os.path.realpath(__file__))


def log_error(filename, line, column, error_code, msg):
    """Print a message to the command line."""
    click.echo(filename + ":" +
               str(1 + line) + ":" +
               str(1 + column) + ": " +
               error_code + " " +
               msg + " " + base_url + error_code)


@click.command()
@click.option('--version/--whatever', default=None)
@click.option('--initialize/--i', default=None)
@click.option('--debug/--d', default=False)
@click.argument('file', default=False)
def proselint(file=None, version=None, initialize=None, debug=None):
    """Run the linter."""

    # Return the version number.
    if version:
        print "v0.0.1"
        return

    # In debug mode, delete the cache and *.pyc files before running.
    if debug:
        subprocess.call("find . -name '*.pyc' -delete", shell=True)
        subprocess.call("find . -name '*.check' -delete", shell=True)

    if not file:
        file = "demo.md"

    # Extract functions from the checks folder.
    checks = []
    for loader, module_name, is_pkg in pkgutil.walk_packages(pl.__path__):
        module = loader.find_module(module_name).load_module(module_name)

        # Run the initialization.
        if initialize:
            try:
                assert(hasattr(module.initialize, '__call__'))
                module.initialize()
            except Exception, e:
                print e
                pass

        # Add the check to the list of checks.
        try:
            assert(hasattr(module.check, '__call__'))
            checks.append(module.check)
        except Exception:
            pass

    if initialize:
        print "Initialization complete."
        return

    # Apply all the checks.
    with codecs.open(file, "r", encoding='utf-8') as f:
        text = f.read()
        errors = []
        for check in checks:
            errors += check(text)

        # Sort the errors by line and column number.
        errors = sorted(errors)

        # Display the errors.
        for error in errors:
            (start, end, error_code, msg) = error
            (line, column) = line_and_column(text, start)
            log_error(file, line, column, error_code, msg)

if __name__ == '__main__':
    proselint()

@@ -625,352 +625,217 @@
 )%0A%0A%0A
-@click.command()%0A@click.option('--version/--whatev
+def run_initialization():%0A    for load
 er
-'
 , 
-default=None)%0A@click.option('--initialize/--i', default=None)%0A@click.option('--debug/--d', default=False)%0A@click.argument('file', default=False)%0Adef proselint(file=None, version=None, initialize=None, debug=None):
+module_name, is_pkg in pkgutil.walk_packages(pl.__path__):%0A        module = loader.find_module(module_name).load_module(module_name)%0A
 %0A    
-%22%22%22Run the linter.%22%22%22%0A%0A
     # R
-etur
+u
 n the 
-version number.%0A    if version:
+initialization.
 %0A   
@@ -843,769 +843,382 @@
     
-print %22v0.0.1%22%0A        return%0A%0A    # In debug mode, delete the cache and *.pyc files before running.%0A    if debug:%0A        subprocess.call(%22find . -name '*.pyc' -delete%22, shell=True)%0A        subprocess.call(%22find . -name '*.check' -delete%22, shell=True)%0A%0A    if not file:%0A        file = %22demo.md%22%0A%0A    # Extract functions from the checks folder.%0A    checks = %5B%5D%0A    for loader, module_name, is_pkg in pkgutil.walk_packages(pl.__path__):%0A        module = loader.find_module(module_name).load_module(module_name)%0A%0A        # Run the initialization.%0A        if initialize:%0A            try:%0A                assert(hasattr(module.initialize, '__call__'))%0A                module.initialize()%0A            except Exception, e:%0A                print e%0A                pass
+try:%0A            assert(hasattr(module.initialize, '__call__'))%0A            module.initialize()%0A        except Exception:%0A            pass%0A%0A%0Adef lint(filename):%0A%0A    # Extract functions from the checks folder.%0A    checks = %5B%5D%0A    for loader, module_name, is_pkg in pkgutil.walk_packages(pl.__path__):%0A        module = loader.find_module(module_name).load_module(module_name)
 %0A%0A  
@@ -1417,84 +1417,8 @@
 ss%0A%0A
-    if initialize:%0A        print %22Initialization complete.%22%0A        return%0A%0A
     
@@ -1466,16 +1466,20 @@
 pen(file
+name
 , %22r%22, e
@@ -1888,16 +1888,20 @@
 ror(file
+name
 , line, 
@@ -1926,16 +1926,774 @@
 , msg)%0A%0A
+    return errors%0A%0A%0A@click.command()%0A@click.option('--version/--whatever', default=None)%0A@click.option('--initialize/--i', default=None)%0A@click.option('--debug/--d', default=False)%0A@click.argument('file', default=False)%0Adef proselint(file=None, version=None, initialize=None, debug=None):%0A    %22%22%22Run the linter.%22%22%22%0A%0A    # Return the version number.%0A    if version:%0A        print %22v0.0.1%22%0A        return%0A%0A    if initialize:%0A        run_initialization()%0A        return%0A%0A    # In debug mode, delete the cache and *.pyc files before running.%0A    if debug:%0A        subprocess.call(%22find . -name '*.pyc' -delete%22, shell=True)%0A        subprocess.call(%22find . -name '*.check' -delete%22, shell=True)%0A%0A    if not file:%0A        file = %22demo.md%22%0A%0A    return lint(file)%0A%0A%0A
 if __nam

Reorganize command line utility