#!/usr/bin/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 = symbols("xyz")
       >>> k, m, n = symbols("kmn", integer=True)
       >>> f, g, h = map(Function, 'fgh')

   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)

# some longer messages

long_message = """\
These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z = symbols('xyz')
>>> k, m, n = symbols('kmn', integer=True)
>>> f, g, h = map(Function, 'fgh')

Documentation can be found at http://sympy.org/
"""

no_ipython = """\
Couldn't locate IPython. Having IPython installed is greatly recommended.
See http://ipython.scipy.org for more details.  If you use Debian/Ubuntu,
just install the 'ipython' package and start isympy again.
"""

from sympy.interactive import init_session

def main():
    from sympy import __version__ as sympy_version
    py_version = "%d.%d.%d" % sys.version_info[:3]

    from optparse import OptionParser

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

    parser.add_option(
        '-c', '--console',
        dest='console',
        action='store',
        default=None,
        help='select type of interactive session: IPython | Python')

    parser.add_option(
        '-p', '--pretty',
        dest='pretty',
        action='store',
        default="any",
        help='setup pretty printing: Unicode | ASCII | any | no')

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

    (options, ipy_args) = parser.parse_args()

    session = options.console

    if session is not None:
        session = session.lower()

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

    pretty = options.pretty.lower()

    if pretty != 'any':
        if pretty == 'unicode':
            args['use_unicode'] = True
        else:
            args['use_unicode'] = False

            if pretty != 'ascii':
                if pretty == 'no':
                    args['pretty'] = False
                else:
                    raise ValueError("Unknown pretty" \
                        " printing setup: " + options.pretty)

    if not options.quiet:
        args['message'] = long_message

    try:
        init_session(session, **args)
    except ValueError:
        try:
            init_session('ipython', **args)
        except ImportError:
            print no_ipython
            init_session('python', **args)

if __name__ == "__main__":
    main()
