# Path: abkhazia/kaldi/path.py
# def kaldi_path():
#     """Return an environment for running the abkhazia recipes"""
#     env = os.environ
# 
#     env['LC_ALL'] = 'C'  # for expected sorting and joining behaviour
# 
#     kaldiroot = config.get('kaldi', 'kaldi-directory')
#     kaldisrc = os.path.join(kaldiroot, 'src')
# 
#     targets = ('bin', 'featbin', 'fgmmbin', 'fstbin', 'gmmbin',
#                'latbin', 'nnetbin', 'nnet2bin', 'sgmmbin', 'lmbin',
#                'kwsbin', 'ivectorbin', 'online2bin', 'sgmm2bin')
#     kaldibin = ':'.join((os.path.join(kaldisrc, target)
#                         for target in targets))
# 
#     fstbin = os.path.join(kaldiroot, 'tools', 'openfst', 'bin')
# 
#     platform = 'macosx' if os.uname()[0] == 'Darwin' else 'i686-m64'
#     lmbin = ':'.join([
#         os.path.join(kaldiroot, 'tools', 'irstlm', 'bin'),
#         os.path.join(kaldiroot, 'tools', 'srilm', 'bin'),
#         os.path.join(kaldiroot, 'tools', 'srilm', 'bin', platform),
#         os.path.join(kaldiroot, 'tools', 'sctk', 'bin')])
# 
#     if 'PATH' not in env:
#         # PATH isn't in the environment, should not occur
#         env['PATH'] = ':'.join([kaldibin, fstbin, lmbin])
#     else:
#         path = ':'.join([kaldibin, fstbin, lmbin])
#         if path not in env['PATH']:
#             env['PATH'] += ':' + path
# 
#     env['IRSTLM'] = os.path.join(kaldiroot, 'tools', 'irstlm')
# 
#     # was a bug -> error while loading shared libraries: libfstscript.so.1
#     fstlib = os.path.join(kaldiroot, 'tools', 'openfst', 'lib')
#     libfstscript = os.path.join(fstlib, 'libfstscript.so.1')
#     assert os.path.exists(libfstscript)
# 
#     if 'LD_LIBRARY_PATH' not in env:
#         env['LD_LIBRARY_PATH'] = fstlib
#     else:
#         if fstlib not in env['LD_LIBRARY_PATH']:
#             env['LD_LIBRARY_PATH'] += ':' + fstlib
# 
#     return env


import os
import re
import struct
import tempfile
import numpy as np
import h5features as h5f
import h5py
import abkhazia.utils as utils
from abkhazia.kaldi import kaldi_path
def dict_to_ark(arkfile, data, format='text'):
    """Write a data dictionary to a Kaldi ark file

    TODO for now time information from h5f is lost in ark

    Parameters:
    -----------

    arkfile (str): path to the ark file to write

    data (dict): dictionary of numpy arrays to write

    format (str): must be 'text' or 'binary' to write a text or a
        binary ark file respectively, default is 'text'

    Raise:
    ------

    RuntimeError if format is not 'text' or 'binary'

    """
    if format == 'text':
        _dict_to_txt_ark(arkfile, data)
    elif format == 'binary':
        with tempfile.NamedTemporaryFile(
                dir=utils.config.get('abkhazia', 'tmp-directory')) as tmp:
            _dict_to_txt_ark(tmp.name, data)

            utils.jobs.run(
                'copy-feats ark,t:{0} ark:{1}'.format(tmp.name, arkfile),

# Path: abkhazia/kaldi/path.py
# def kaldi_path():
#     """Return an environment for running the abkhazia recipes"""
#     env = os.environ
# 
#     env['LC_ALL'] = 'C'  # for expected sorting and joining behaviour
# 
#     kaldiroot = config.get('kaldi', 'kaldi-directory')
#     kaldisrc = os.path.join(kaldiroot, 'src')
# 
#     targets = ('bin', 'featbin', 'fgmmbin', 'fstbin', 'gmmbin',
#                'latbin', 'nnetbin', 'nnet2bin', 'sgmmbin', 'lmbin',
#                'kwsbin', 'ivectorbin', 'online2bin', 'sgmm2bin')
#     kaldibin = ':'.join((os.path.join(kaldisrc, target)
#                         for target in targets))
# 
#     fstbin = os.path.join(kaldiroot, 'tools', 'openfst', 'bin')
# 
#     platform = 'macosx' if os.uname()[0] == 'Darwin' else 'i686-m64'
#     lmbin = ':'.join([
#         os.path.join(kaldiroot, 'tools', 'irstlm', 'bin'),
#         os.path.join(kaldiroot, 'tools', 'srilm', 'bin'),
#         os.path.join(kaldiroot, 'tools', 'srilm', 'bin', platform),
#         os.path.join(kaldiroot, 'tools', 'sctk', 'bin')])
# 
#     if 'PATH' not in env:
#         # PATH isn't in the environment, should not occur
#         env['PATH'] = ':'.join([kaldibin, fstbin, lmbin])
#     else:
#         path = ':'.join([kaldibin, fstbin, lmbin])
#         if path not in env['PATH']:
#             env['PATH'] += ':' + path
# 
#     env['IRSTLM'] = os.path.join(kaldiroot, 'tools', 'irstlm')
# 
#     # was a bug -> error while loading shared libraries: libfstscript.so.1
#     fstlib = os.path.join(kaldiroot, 'tools', 'openfst', 'lib')
#     libfstscript = os.path.join(fstlib, 'libfstscript.so.1')
#     assert os.path.exists(libfstscript)
# 
#     if 'LD_LIBRARY_PATH' not in env:
#         env['LD_LIBRARY_PATH'] = fstlib
#     else:
#         if fstlib not in env['LD_LIBRARY_PATH']:
#             env['LD_LIBRARY_PATH'] += ':' + fstlib
# 
#     return env


# Path: abkhazia/kaldi/ark.py
import os
import re
import struct
import tempfile
import numpy as np
import h5features as h5f
import h5py
import abkhazia.utils as utils
from abkhazia.kaldi import kaldi_path

def dict_to_ark(arkfile, data, format='text'):
    """Write a data dictionary to a Kaldi ark file

    TODO for now time information from h5f is lost in ark

    Parameters:
    -----------

    arkfile (str): path to the ark file to write

    data (dict): dictionary of numpy arrays to write

    format (str): must be 'text' or 'binary' to write a text or a
        binary ark file respectively, default is 'text'

    Raise:
    ------

    RuntimeError if format is not 'text' or 'binary'

    """
    if format == 'text':
        _dict_to_txt_ark(arkfile, data)
    elif format == 'binary':
        with tempfile.NamedTemporaryFile(
                dir=utils.config.get('abkhazia', 'tmp-directory')) as tmp:
            _dict_to_txt_ark(tmp.name, data)

            utils.jobs.run(
                'copy-feats ark,t:{0} ark:{1}'.format(tmp.name, arkfile),

                env=kaldi_path(),