__author__ = 'Victor Cabezas'

from past.builtins import basestring
from .Solver import Solver
from .Solver import Beginner
from .Solver import CFOP
from .Solver import Kociemba
from .NaiveCube import NaiveCube
from .Cubie import Cube
from .Printer import TtyPrinter

def _check_valid_cube(cube):
    '''Checks if cube is one of str, NaiveCube or Cubie.Cube and returns
    an instance of Cubie.Cube'''

    if isinstance(cube, basestring):
        c = NaiveCube()
        c.set_cube(cube)
        cube = c

    if isinstance(cube, NaiveCube):
        c = Cube()
        c.from_naive_cube(cube)
        cube = c

    if not isinstance(cube, Cube):
        raise ValueError('Cube is not one of (str, NaiveCube or Cubie.Cube)')

    return cube

def solve(cube, method = Beginner.BeginnerSolver, *args, **kwargs):
    if not issubclass(method, Solver):
        raise ValueError('Method %s is not a valid Solver subclass' %
            method.__class__.__name__
        )

    cube = _check_valid_cube(cube)

    solver = method(cube)

    return solver.solution(*args, **kwargs)

def pprint(cube, color = True):
    cube = _check_valid_cube(cube)
    printer = TtyPrinter(cube, color)
    printer.pprint()

@@ -23,17 +23,70 @@
 abezas'%0A
+import argparse%0Afrom __future__ import print_function
 %0A
-
 from pas
@@ -314,16 +314,141 @@
 rinter%0A%0A
+METHODS = %7B%0A    'Beginner': Beginner.BeginnerSolver,%0A    'CFOP': CFOP.CFOPSolver,%0A    'Kociemba': Kociemba.KociembaSolver%0A%7D%0A%0A
 def _che
@@ -983,16 +983,253 @@
 wargs):%0A
+    if isinstance(method, basestring):%0A        if not method in METHODS:%0A            raise ValueError('Invalid method name, must be one of (%25s)' %25%0A                ', '.join(METHODS.keys())%0A            )%0A        method = METHODS%5Bmethod%5D%0A%0A
     if n
@@ -1591,16 +1591,16 @@
  color)%0A
-
     prin
@@ -1612,8 +1612,654 @@
 print()%0A
+%0Adef main():%0A    arg_parser = argparse.ArgumentParser(description = 'rubik_solver command line tool')%0A    arg_parser.add_argument('-i', '--cube', dest = 'cube', required = True, help = 'Cube definition string')%0A    arg_parser.add_argument('-c', '--color', dest = 'color', action = 'store_false', help = 'Disable use of colors with TtyPrinter')%0A    arg_parser.add_argument('-s', '--solver', dest = 'solver', default = 'Beginner', choices = METHODS.keys(), help = 'Solver method to use')%0A%0A    cube = args.cube.lower()%0A    print (%22Read cube%22, cube)%0A    pprint(cube, args.colors)%0A%0A    print (%22Solution%22, ', '.join(solve(cube, METHODS%5Bargs.solver%5D)))%0A

Add main to utils module