# 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/settings.py
# def get_setting(key):
#     'It returns the value for one setting'
#     return _settings[key]
# 
# Path: crumbs/utils/file_utils.py
# def fhand_is_seekable(fhand):
#     'It returns True if the fhand is seekable'
#     try:
#         try:
#             # The stdin stream in some instances has seek, has no seekable
#             fhand.tell()
#         except IOError:
#             return False
#         try:
#             if fhand.seekable():
#                 return True
#             else:
#                 return False
#         except AttributeError:
#             return True
#     except AttributeError:
#         return False
# 
# def peek_chunk_from_file(fhand, chunk_size):
#     'It returns the beginning of a file without moving the pointer'
#     if fhand_is_seekable(fhand):
#         fhand.seek(0)
#         chunk = fhand.read(chunk_size)
#         fhand.seek(0)
#     else:
#         chunk = fhand.peek(chunk_size)
#     return chunk
# 
# Path: crumbs/exceptions.py
# class UnknownFormatError(Exception):
#     'Raised when the format of a sequence file cannot be guessed'
#     pass
# 
# class UndecidedFastqVersionError(Exception):
#     'The file is Fastq, but the version is difficult to guess'
#     pass
# 
# class FileIsEmptyError(Exception):
#     pass


import cStringIO
import hashlib
from array import array
from crumbs.utils.optional_modules import FastqGeneralIterator
from crumbs.settings import get_setting
from crumbs.utils.file_utils import fhand_is_seekable, peek_chunk_from_file
from crumbs.exceptions import (UnknownFormatError, UndecidedFastqVersionError,
                               FileIsEmptyError)
    '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


def _guess_format(fhand, force_file_as_non_seek):
    '''It guesses the format of the sequence file.

    This function is just for testing forcing the fhand as non-seekable.
    It does ignore the solexa fastq version.
    '''
    chunk_size = 2048

# 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/settings.py
# def get_setting(key):
#     'It returns the value for one setting'
#     return _settings[key]
# 
# Path: crumbs/utils/file_utils.py
# def fhand_is_seekable(fhand):
#     'It returns True if the fhand is seekable'
#     try:
#         try:
#             # The stdin stream in some instances has seek, has no seekable
#             fhand.tell()
#         except IOError:
#             return False
#         try:
#             if fhand.seekable():
#                 return True
#             else:
#                 return False
#         except AttributeError:
#             return True
#     except AttributeError:
#         return False
# 
# def peek_chunk_from_file(fhand, chunk_size):
#     'It returns the beginning of a file without moving the pointer'
#     if fhand_is_seekable(fhand):
#         fhand.seek(0)
#         chunk = fhand.read(chunk_size)
#         fhand.seek(0)
#     else:
#         chunk = fhand.peek(chunk_size)
#     return chunk
# 
# Path: crumbs/exceptions.py
# class UnknownFormatError(Exception):
#     'Raised when the format of a sequence file cannot be guessed'
#     pass
# 
# class UndecidedFastqVersionError(Exception):
#     'The file is Fastq, but the version is difficult to guess'
#     pass
# 
# class FileIsEmptyError(Exception):
#     pass


# Path: crumbs/seq/utils/file_formats.py
import cStringIO
import hashlib
from array import array
from crumbs.utils.optional_modules import FastqGeneralIterator
from crumbs.settings import get_setting
from crumbs.utils.file_utils import fhand_is_seekable, peek_chunk_from_file
from crumbs.exceptions import (UnknownFormatError, UndecidedFastqVersionError,
                               FileIsEmptyError)

    '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


def _guess_format(fhand, force_file_as_non_seek):
    '''It guesses the format of the sequence file.

    This function is just for testing forcing the fhand as non-seekable.
    It does ignore the solexa fastq version.
    '''
    chunk_size = 2048

    chunk = peek_chunk_from_file(fhand, chunk_size)