#! /usr/bin/env python

"""Python shell for SymPy.

   This is just a normal Python shell (IPython shell if you have the
   IPython package installed),  that executes the following commands
   for the user:

       >>> from __future__ import division
       >>> from sympy import *
       >>> x, y, z, t = symbols('x y z t')
       >>> k, m, n = symbols('k m n', integer=True)
       >>> f, g, h = symbols('f g h', cls=Function)

   So starting 'isympy' is equivalent to starting Python (or IPython)
   and executing the above commands by hand.  It is intended for easy
   and quick experimentation with SymPy.

   COMMAND LINE OPTIONS
   --------------------

   -c CONSOLE, --console=CONSOLE

     Use the specified Python or IPython shell as console backend instead
     of the default one (IPython if present or Python otherwise), e.g.:

        isympy -c python

   -p PRETTY, --pretty PRETTY

     Setup pretty printing in SymPy. By default the most pretty,  Unicode
     printing is enabled. User can use less pretty ASCII printing instead
     or no pretty printing at all, e.g.:

   -q, --quiet

     Print only Python's and SymPy's versions to stdout at startup.

   -- IPython's options

     Additionally you can pass command line options directly to IPython
     interpreter (standard Python shell is not supported).  However you
     need to add '--' separator between two types of options. To run
     SymPy without startup banner and colors, for example, issue:

        isympy -q -- -colors NoColor

"""

import os, sys

# hook in-tree SymPy into Python path, if possible

isympy_dir = os.path.dirname(__file__)         # bin/isympy
sympy_top  = os.path.split(isympy_dir)[0]      # ../
sympy_dir  = os.path.join(sympy_top, 'sympy')  # ../sympy/

if os.path.isdir(sympy_dir):
    sys.path.insert(0, sympy_top)

def main():
    from optparse import OptionParser

    usage = 'usage: isympy [options] -- [ipython options]'
    parser = OptionParser(usage)

    parser.add_option(
        '-c', '--console',
        dest='console',
        action='store',
        default=None,
        choices=['ipython', 'python'],
        help='select type of interactive session: ipython | python')

    parser.add_option(
        '-p', '--pretty',
        dest='pretty',
        action='store',
        default=None,
        choices=['unicode', 'ascii', 'no'],
        help='setup pretty printing: unicode | ascii | no')

    parser.add_option(
        '-t', '--types',
        dest='types',
        action='store',
        default=None,
        choices=['gmpy', 'python', 'sympy'],
        help='setup ground types: gmpy | python | sympy')

    parser.add_option(
        '-o', '--order',
        dest='order',
        action='store',
        default=None,
        choices=['lex', 'grlex', 'grevlex', 'rev-lex', 'rev-grlex', 'rev-grevlex', 'old'],
        help='setup ordering of terms: [rev-]lex | [rev-]grlex | [rev-]grevlex | old')

    parser.add_option(
        '-q', '--quiet',
        dest='quiet',
        action='store_true',
        default=False,
        help='print only version information at startup')

    parser.add_option(
        '-d', '--doctest',
        dest='doctest',
        action='store_true',
        default=False,
        help='use the doctest format for output (you can just copy and paste it)')

    parser.add_option(
        '-C', '--no-cache',
        dest='cache',
        action='store_false',
        default=True,
        help='disable caching mechanism')

    (options, ipy_args) = parser.parse_args()

    if not options.cache:
        os.environ['SYMPY_USE_CACHE'] = 'no'

    if options.types:
        os.environ['SYMPY_GROUND_TYPES'] = options.types

    if options.doctest:
        options.pretty = 'no'
        options.console = 'python'

    session = options.console

    if session is not None:
        ipython = session == 'ipython'
    else:
        ipython = None

    args = {
        'pretty_print' : True,
        'use_unicode'  : None,
        'order'        : None,
        'argv'         : ipy_args,
    }

    if options.pretty == 'unicode':
        args['use_unicode'] = True
    elif options.pretty == 'ascii':
        args['use_unicode'] = False
    elif options.pretty == 'no':
        args['pretty_print'] = False

    if options.order is not None:
        args['order'] = options.order

    args['quiet'] = options.quiet

    from sympy.interactive import init_session
    init_session(ipython, **args)

if __name__ == "__main__":
    main()
