from __future__ import print_function, absolute_import
import os, sys, re, pydoc

import numpy as np # pyflakes:ignore (API import)

_cwd = os.path.abspath(os.path.split(__file__)[0])
sys.path.insert(0, os.path.join(_cwd, '..', 'param'))

import param

__version__ = param.Version(release=(1,3,2), fpath=__file__,
                            commit="$Format:%h$", reponame='holoviews')

from .core import archive                               # pyflakes:ignore (API import)
from .core.dimension import OrderedDict, Dimension      # pyflakes:ignore (API import)
from .core.boundingregion import BoundingBox            # pyflakes:ignore (API import)
from .core.options import Options, Store, StoreOptions  # pyflakes:ignore (API import)
from .core.layout import *                              # pyflakes:ignore (API import)
from .core.element import *                             # pyflakes:ignore (API import)
from .core.overlay import *                             # pyflakes:ignore (API import)
from .core.tree import *                                # pyflakes:ignore (API import)

from .interface import *                                             # pyflakes:ignore (API import)
from .operation import ElementOperation, MapOperation, TreeOperation # pyflakes:ignore (API import)
from .element import *                                               # pyflakes:ignore (API import)

# A single holoviews.rc file may be executed if found.
for rcfile in [os.environ.get("HOLOVIEWSRC", ''),
               "~/.holoviews.rc",
               "~/.config/holoviews/holoviews.rc"]:
    try:
        filename = os.path.expanduser(rcfile)
        with open(filename) as f:
            code = compile(f.read(), filename, 'exec')
            try:
                exec(code)
            except Exception as e:
                print("Warning: Could not load %r [%r]" % (filename, str(e)))
        break
    except IOError:
        pass


def help(obj, visualization=True, ansi=True, backend='matplotlib'):
    """
    Extended version of the built-in help that supports parameterized
    functions and objects. If ansi is set to False, all ANSI color
    codes are stripped out.
    """
    ansi_escape = re.compile(r'\x1b[^m]*m')
    parameterized_object = isinstance(obj, param.Parameterized)
    parameterized_class = (isinstance(obj,type)
                           and  issubclass(obj,param.Parameterized))

    if parameterized_object or parameterized_class:
        if Store.registry[backend].get(obj if parameterized_class else type(obj), False):
            if visualization is False:
                print("\nTo view the visualization options applicable to this object or class, use:\n\n"
                      "   holoviews.help(obj, visualization=True)\n")
            else:
                Store.info(obj, ansi=ansi, backend=backend)
                return
        info = param.ipython.ParamPager()(obj)
        if ansi is False:
            info = ansi_escape.sub('', info)
        print(info)
    else:
        pydoc.help(obj)
