from isort import SortImports
import mothermayi.colors
import mothermayi.errors
import mothermayi.files

def plugin():
    return {
        'name'          : 'isort',
        'pre-commit'    : pre_commit,
    }

def do_sort(filename):
    results = SortImports(filename)
    return getattr(results, 'in_lines', None) and results.in_lines != results.out_lines

def get_status(had_changes):
    return mothermayi.colors.red('unsorted') if had_changes else mothermayi.colors.green('sorted')

def pre_commit(config, staged):
    python_files = list(mothermayi.files.python_source(staged))
    if not python_files:
        return

    changes = [do_sort(filename) for filename in python_files]
    messages = [get_status(had_change) for had_change in changes]
    lines = ["  {0:<30} ... {1:<10}".format(filename, message) for filename, message in zip(python_files, messages)]
    result = "\n".join(lines)
    if any(changes):
        raise mothermayi.errors.FailHook(result)
    return result

from isort import SortImports
import mothermayi.colors
import mothermayi.errors
import mothermayi.files

def plugin():
    return {
        'name'          : 'isort',
        'pre-commit'    : pre_commit,
    }

def do_sort(filename):
    results = SortImports(filename, check=True)
    return results.incorrectly_sorted

def get_status(had_changes):
    return mothermayi.colors.red('unsorted') if had_changes else mothermayi.colors.green('sorted')

def pre_commit(config, staged):
    python_files = list(mothermayi.files.python_source(staged))
    if not python_files:
        return

    changes = [do_sort(filename) for filename in python_files]
    messages = [get_status(had_change) for had_change in changes]
    lines = ["  {0:<30} ... {1:<10}".format(filename, message) for filename, message in zip(python_files, messages)]
    result = "\n".join(lines)
    if any(changes):
        raise mothermayi.errors.FailHook(result)
    return result

Leverage isort's check mode to make our logic simpler
Leverage isort's check mode to make our logic simpler

This avoids having to check for in_lines or compare against the
out_lines by just asking for a check and using the results

  from isort import SortImports
  import mothermayi.colors
  import mothermayi.errors
  import mothermayi.files
  
  def plugin():
      return {
          'name'          : 'isort',
          'pre-commit'    : pre_commit,
      }
  
  def do_sort(filename):
-     results = SortImports(filename)
+     results = SortImports(filename, check=True)
-     return getattr(results, 'in_lines', None) and results.in_lines != results.out_lines
+     return results.incorrectly_sorted
  
  def get_status(had_changes):
      return mothermayi.colors.red('unsorted') if had_changes else mothermayi.colors.green('sorted')
  
  def pre_commit(config, staged):
      python_files = list(mothermayi.files.python_source(staged))
      if not python_files:
          return
  
      changes = [do_sort(filename) for filename in python_files]
      messages = [get_status(had_change) for had_change in changes]
      lines = ["  {0:<30} ... {1:<10}".format(filename, message) for filename, message in zip(python_files, messages)]
      result = "\n".join(lines)
      if any(changes):
          raise mothermayi.errors.FailHook(result)
      return result
  
Leverage isort's check mode to make our logic simpler
## Code Before:
from isort import SortImports
import mothermayi.colors
import mothermayi.errors
import mothermayi.files

def plugin():
    return {
        'name'          : 'isort',
        'pre-commit'    : pre_commit,
    }

def do_sort(filename):
    results = SortImports(filename)
    return getattr(results, 'in_lines', None) and results.in_lines != results.out_lines

def get_status(had_changes):
    return mothermayi.colors.red('unsorted') if had_changes else mothermayi.colors.green('sorted')

def pre_commit(config, staged):
    python_files = list(mothermayi.files.python_source(staged))
    if not python_files:
        return

    changes = [do_sort(filename) for filename in python_files]
    messages = [get_status(had_change) for had_change in changes]
    lines = ["  {0:<30} ... {1:<10}".format(filename, message) for filename, message in zip(python_files, messages)]
    result = "\n".join(lines)
    if any(changes):
        raise mothermayi.errors.FailHook(result)
    return result

## Instruction:
Leverage isort's check mode to make our logic simpler
## Code After:
from isort import SortImports
import mothermayi.colors
import mothermayi.errors
import mothermayi.files

def plugin():
    return {
        'name'          : 'isort',
        'pre-commit'    : pre_commit,
    }

def do_sort(filename):
    results = SortImports(filename, check=True)
    return results.incorrectly_sorted

def get_status(had_changes):
    return mothermayi.colors.red('unsorted') if had_changes else mothermayi.colors.green('sorted')

def pre_commit(config, staged):
    python_files = list(mothermayi.files.python_source(staged))
    if not python_files:
        return

    changes = [do_sort(filename) for filename in python_files]
    messages = [get_status(had_change) for had_change in changes]
    lines = ["  {0:<30} ... {1:<10}".format(filename, message) for filename, message in zip(python_files, messages)]
    result = "\n".join(lines)
    if any(changes):
        raise mothermayi.errors.FailHook(result)
    return result
