# Path: crumbs/utils/file_utils.py
# def wrap_in_buffered_reader(fhand, force_wrap=False,
#                             buffering=DEF_FILE_BUFFER):
#     '''It wraps the given file in a peekable BufferedReader.
# 
#     If the file is seekable it doesn't do anything.
#     '''
#     if not force_wrap and fhand_is_seekable(fhand):
#         return fhand
#     else:
#         fhand = io.open(fhand.fileno(), mode='rb',
#                         buffering=buffering)  # with text there's no peek
# 
#     return fhand
# 
# def uncompress_if_required(fhand):
#     'It returns a uncompressed handle if required'
#     magic = peek_chunk_from_file(fhand, 2)
#     if magic == '\037\213':
#         fhand = GzipFile(fileobj=fhand)
#     elif magic == 'BZ':
#         try:
#             fhand = BZ2File(fhand)
#         except NameError:
#             raise OptionalRequirementError(BZIP_ERROR)
#     return fhand
# 
# def compress_fhand(fhand, compression_kind=None):
#     'Compresses the file if required'
#     if compression_kind == BGZF:
#         if fhand_is_seekable(fhand):
#             fhand = BgzfWriter(fileobj=fhand)
#         else:
#             raise RuntimeError('bgzf is only available for seekable files')
#     elif compression_kind == GZIP:
#         fhand = GzipFile(fileobj=fhand)
#     elif compression_kind == BZIP2:
#         mode = 'w' if 'w' in fhand.mode else 'r'
#         try:
#             fhand = BZ2File(fhand, mode=mode)
#         except NameError:
#             raise OptionalRequirementError(BZIP_ERROR)
#     return fhand
# 
# Path: crumbs/utils/tags.py
# OUTFILE = 'output'
# 
# GUESS_FORMAT = 'guess'
# 
# Path: crumbs/seq/utils/file_formats.py
# def get_format(fhand):
#     'It gets the format or it looks in the inventory'
#     id_ = _get_fhand_id(fhand)
#     try:
#         file_format = FILEFORMAT_INVENTORY[id_]
#     except KeyError:
#         file_format = None
# 
#     if file_format is None:
#         file_format = _guess_format(fhand, force_file_as_non_seek=False)
#         FILEFORMAT_INVENTORY[id_] = file_format
# 
#     return file_format
# 
# def set_format(fhand, file_format):
#     'It sets the file format in the global inventory variable'
#     id_ = _get_fhand_id(fhand)
#     if id_ in FILEFORMAT_INVENTORY:
#         msg = 'The given instance already setted its file format'
#         raise RuntimeError(msg)
#     FILEFORMAT_INVENTORY[id_] = file_format
# 
# Path: crumbs/utils/bin_utils.py
# def build_version_msg():
#     'It creates a message with the version.'
#     bin_name = os.path.split(sys.argv[0])[-1]
#     version_msg = bin_name + ' from ngs_crumbs version: ' + version
#     return version_msg
# 
# def get_requested_compression(parsed_args):
#     'It looks in the selected options and return the selected compression kind'
#     comp_kind = None
#     bgzf = getattr(parsed_args, 'bgzf', False)
#     gzip = getattr(parsed_args, 'gzip', False)
#     bzip2 = getattr(parsed_args, 'bzip2', False)
#     if bgzf:
#         comp_kind = BGZF
#     elif gzip:
#         comp_kind = GZIP
#     elif bzip2:
#         comp_kind = BZIP2
#     return comp_kind


import sys
import argparse
from crumbs.utils.file_utils import (wrap_in_buffered_reader,
                                     uncompress_if_required, compress_fhand)
from crumbs.utils.tags import OUTFILE, GUESS_FORMAT
from crumbs.seq.utils.file_formats import get_format, set_format
from crumbs.utils.bin_utils import build_version_msg, get_requested_compression
    for fhand in in_fhands:
        fhand = wrap_in_buffered_reader(fhand)
        fhand = uncompress_if_required(fhand)
        wrapped_fhands.append(fhand)

    # We have to add the one_line to the fastq files in order to get the
    # speed improvements of the seqitems
    in_format = parsed_args.in_format
    if in_format == GUESS_FORMAT:
        for wrapped_fhand in wrapped_fhands:
            get_format(wrapped_fhand)
    else:
        for wrapped_fhand in wrapped_fhands:
            set_format(wrapped_fhand, in_format)

    out_fhand = getattr(parsed_args, OUTFILE)

    comp_kind = get_requested_compression(parsed_args)
    if isinstance(out_fhand, list):
        new_out_fhands = []
        for out_f in out_fhand:
            try:
                out_f = compress_fhand(out_f, compression_kind=comp_kind)
            except RuntimeError, error:
                parser.error(error)

            new_out_fhands.append(out_f)
        out_fhand = new_out_fhands
    else:
        try:

# Path: crumbs/utils/file_utils.py
# def wrap_in_buffered_reader(fhand, force_wrap=False,
#                             buffering=DEF_FILE_BUFFER):
#     '''It wraps the given file in a peekable BufferedReader.
# 
#     If the file is seekable it doesn't do anything.
#     '''
#     if not force_wrap and fhand_is_seekable(fhand):
#         return fhand
#     else:
#         fhand = io.open(fhand.fileno(), mode='rb',
#                         buffering=buffering)  # with text there's no peek
# 
#     return fhand
# 
# def uncompress_if_required(fhand):
#     'It returns a uncompressed handle if required'
#     magic = peek_chunk_from_file(fhand, 2)
#     if magic == '\037\213':
#         fhand = GzipFile(fileobj=fhand)
#     elif magic == 'BZ':
#         try:
#             fhand = BZ2File(fhand)
#         except NameError:
#             raise OptionalRequirementError(BZIP_ERROR)
#     return fhand
# 
# def compress_fhand(fhand, compression_kind=None):
#     'Compresses the file if required'
#     if compression_kind == BGZF:
#         if fhand_is_seekable(fhand):
#             fhand = BgzfWriter(fileobj=fhand)
#         else:
#             raise RuntimeError('bgzf is only available for seekable files')
#     elif compression_kind == GZIP:
#         fhand = GzipFile(fileobj=fhand)
#     elif compression_kind == BZIP2:
#         mode = 'w' if 'w' in fhand.mode else 'r'
#         try:
#             fhand = BZ2File(fhand, mode=mode)
#         except NameError:
#             raise OptionalRequirementError(BZIP_ERROR)
#     return fhand
# 
# Path: crumbs/utils/tags.py
# OUTFILE = 'output'
# 
# GUESS_FORMAT = 'guess'
# 
# Path: crumbs/seq/utils/file_formats.py
# def get_format(fhand):
#     'It gets the format or it looks in the inventory'
#     id_ = _get_fhand_id(fhand)
#     try:
#         file_format = FILEFORMAT_INVENTORY[id_]
#     except KeyError:
#         file_format = None
# 
#     if file_format is None:
#         file_format = _guess_format(fhand, force_file_as_non_seek=False)
#         FILEFORMAT_INVENTORY[id_] = file_format
# 
#     return file_format
# 
# def set_format(fhand, file_format):
#     'It sets the file format in the global inventory variable'
#     id_ = _get_fhand_id(fhand)
#     if id_ in FILEFORMAT_INVENTORY:
#         msg = 'The given instance already setted its file format'
#         raise RuntimeError(msg)
#     FILEFORMAT_INVENTORY[id_] = file_format
# 
# Path: crumbs/utils/bin_utils.py
# def build_version_msg():
#     'It creates a message with the version.'
#     bin_name = os.path.split(sys.argv[0])[-1]
#     version_msg = bin_name + ' from ngs_crumbs version: ' + version
#     return version_msg
# 
# def get_requested_compression(parsed_args):
#     'It looks in the selected options and return the selected compression kind'
#     comp_kind = None
#     bgzf = getattr(parsed_args, 'bgzf', False)
#     gzip = getattr(parsed_args, 'gzip', False)
#     bzip2 = getattr(parsed_args, 'bzip2', False)
#     if bgzf:
#         comp_kind = BGZF
#     elif gzip:
#         comp_kind = GZIP
#     elif bzip2:
#         comp_kind = BZIP2
#     return comp_kind


# Path: crumbs/seq/utils/bin_utils.py
import sys
import argparse
from crumbs.utils.file_utils import (wrap_in_buffered_reader,
                                     uncompress_if_required, compress_fhand)
from crumbs.utils.tags import OUTFILE, GUESS_FORMAT
from crumbs.seq.utils.file_formats import get_format, set_format
from crumbs.utils.bin_utils import build_version_msg, get_requested_compression

    for fhand in in_fhands:
        fhand = wrap_in_buffered_reader(fhand)
        fhand = uncompress_if_required(fhand)
        wrapped_fhands.append(fhand)

    # We have to add the one_line to the fastq files in order to get the
    # speed improvements of the seqitems
    in_format = parsed_args.in_format
    if in_format == GUESS_FORMAT:
        for wrapped_fhand in wrapped_fhands:
            get_format(wrapped_fhand)
    else:
        for wrapped_fhand in wrapped_fhands:
            set_format(wrapped_fhand, in_format)

    out_fhand = getattr(parsed_args, OUTFILE)

    comp_kind = get_requested_compression(parsed_args)
    if isinstance(out_fhand, list):
        new_out_fhands = []
        for out_f in out_fhand:
            try:
                out_f = compress_fhand(out_f, compression_kind=comp_kind)
            except RuntimeError, error:
                parser.error(error)

            new_out_fhands.append(out_f)
        out_fhand = new_out_fhands
    else:
        try:

            out_fhand = compress_fhand(out_fhand, compression_kind=comp_kind)