# Path: crumbs/utils/optional_modules.py
# MSG = 'A python package to run this executable is required,'
# BIO = 'biopython'
# BIO_BGZF = 'biopython with Bgzf support'
#     NCBIXML = create_fake_class(MSG + BIO)
#     NCBIWWW = create_fake_class(MSG + BIO)
# def create_fake_class(msg):
#         def __init__(self, *args, **kwargs):
# def create_fake_funct(msg):
#     def FakeRequiredfunct(*args, **kwargs):
#     class FakePythonRequiredClass(object):
# 
# Path: crumbs/utils/tags.py
# BGZF = 'bgzf'
# 
# GZIP = 'gzip'
# 
# BZIP2 = 'bzip2'
# 
# Path: crumbs/exceptions.py
# class OptionalRequirementError(Exception):
#     'An optional module is not present'
#     pass


import tempfile
import shutil
import io
import os.path
from gzip import GzipFile
from subprocess import check_call, Popen, PIPE
from crumbs.utils.optional_modules import BgzfWriter
from crumbs.utils.tags import BGZF, GZIP, BZIP2
    from crumbs.utils import BZ2File
from crumbs.exceptions import OptionalRequirementError
BZIP_ERROR += 'bzip2 files'


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:

# Path: crumbs/utils/optional_modules.py
# MSG = 'A python package to run this executable is required,'
# BIO = 'biopython'
# BIO_BGZF = 'biopython with Bgzf support'
#     NCBIXML = create_fake_class(MSG + BIO)
#     NCBIWWW = create_fake_class(MSG + BIO)
# def create_fake_class(msg):
#         def __init__(self, *args, **kwargs):
# def create_fake_funct(msg):
#     def FakeRequiredfunct(*args, **kwargs):
#     class FakePythonRequiredClass(object):
# 
# Path: crumbs/utils/tags.py
# BGZF = 'bgzf'
# 
# GZIP = 'gzip'
# 
# BZIP2 = 'bzip2'
# 
# Path: crumbs/exceptions.py
# class OptionalRequirementError(Exception):
#     'An optional module is not present'
#     pass


# Path: crumbs/utils/file_utils.py
import tempfile
import shutil
import io
import os.path
from gzip import GzipFile
from subprocess import check_call, Popen, PIPE
from crumbs.utils.optional_modules import BgzfWriter
from crumbs.utils.tags import BGZF, GZIP, BZIP2
    from crumbs.utils import BZ2File
from crumbs.exceptions import OptionalRequirementError

BZIP_ERROR += 'bzip2 files'


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)