1# This file is part of h5py, a Python interface to the HDF5 library.
2#
3# http://www.h5py.org
4#
5# Copyright 2008-2013 Andrew Collette and contributors
6#
7# License:  Standard 3-clause BSD; see "license.txt" for full license terms
8#           and contributor agreement.
9
10"""
11    This is the h5py package, a Python interface to the HDF5
12    scientific data format.
13"""
14
15from warnings import warn as _warn
16import atexit
17
18
19# --- Library setup -----------------------------------------------------------
20
21# When importing from the root of the unpacked tarball or git checkout,
22# Python sees the "h5py" source directory and tries to load it, which fails.
23# We tried working around this by using "package_dir" but that breaks Cython.
24try:
25    from . import _errors
26except ImportError:
27    import os.path as _op
28    if _op.exists(_op.join(_op.dirname(__file__), '..', 'setup.py')):
29        raise ImportError("You cannot import h5py from inside the install directory.\nChange to another directory first.")
30    else:
31        raise
32
33from . import version
34
35if version.hdf5_version_tuple != version.hdf5_built_version_tuple:
36    _warn(("h5py is running against HDF5 {0} when it was built against {1}, "
37           "this may cause problems").format(
38            '{0}.{1}.{2}'.format(*version.hdf5_version_tuple),
39            '{0}.{1}.{2}'.format(*version.hdf5_built_version_tuple)
40    ))
41
42
43_errors.silence_errors()
44
45from ._conv import register_converters as _register_converters, \
46                   unregister_converters as _unregister_converters
47_register_converters()
48atexit.register(_unregister_converters)
49
50from .h5z import _register_lzf
51_register_lzf()
52
53
54# --- Public API --------------------------------------------------------------
55
56from . import h5a, h5d, h5ds, h5f, h5fd, h5g, h5r, h5s, h5t, h5p, h5z, h5pl
57
58from ._hl import filters
59from ._hl.base import is_hdf5, HLObject, Empty
60from ._hl.files import (
61    File,
62    register_driver,
63    unregister_driver,
64    registered_drivers,
65)
66from ._hl.group import Group, SoftLink, ExternalLink, HardLink
67from ._hl.dataset import Dataset
68from ._hl.datatype import Datatype
69from ._hl.attrs import AttributeManager
70
71from ._selector import MultiBlockSlice
72from .h5 import get_config
73from .h5r import Reference, RegionReference
74from .h5t import (special_dtype, check_dtype,
75    vlen_dtype, string_dtype, enum_dtype, ref_dtype, regionref_dtype,
76    opaque_dtype,
77    check_vlen_dtype, check_string_dtype, check_enum_dtype, check_ref_dtype,
78    check_opaque_dtype,
79)
80from .h5s import UNLIMITED
81
82from .version import version as __version__
83
84
85if version.hdf5_version_tuple[:3] >= get_config().vds_min_hdf5_version:
86    from ._hl.vds import VirtualSource, VirtualLayout
87
88
89def run_tests(args=''):
90    """Run tests with pytest and returns the exit status as an int.
91    """
92    # Lazy-loading of tests package to avoid strong dependency on test
93    # requirements, e.g. pytest
94    from .tests import run_tests
95    return run_tests(args)
96
97
98def enable_ipython_completer():
99    """ Call this from an interactive IPython session to enable tab-completion
100    of group and attribute names.
101    """
102    import sys
103    if 'IPython' in sys.modules:
104        ip_running = False
105        try:
106            from IPython.core.interactiveshell import InteractiveShell
107            ip_running = InteractiveShell.initialized()
108        except ImportError:
109            # support <ipython-0.11
110            from IPython import ipapi as _ipapi
111            ip_running = _ipapi.get() is not None
112        except Exception:
113            pass
114        if ip_running:
115            from . import ipy_completer
116            return ipy_completer.load_ipython_extension()
117
118    raise RuntimeError('Completer must be enabled in active ipython session')
119