'Attempt to identify the appropriate LangInfo from the magic number
in the file. This mimics some of the behaviour of GNU file.
@param head_bytes {string} is a string of 8-bit char bytes or a
unicode string from the head of the document.
@param shebang_only {boolean} can be set to true to only process
magic number records for shebang lines (a minor perf
improvement).'

def langinfo_from_magic(self, head_bytes, shebang_only=False):

    if (self._magic_table is None):
        self._build_tables()
    for (magic_number, li, sort_key) in self._magic_table:
        try:
            (start, format, pattern) = magic_number
        except ValueError:
            continue
        if (shebang_only and (format != 'regex')):
            continue
        if (format == 'string'):
            end = (start + len(pattern))
            if (head_bytes[start:end] == pattern):
                return li
        elif (format == 'regex'):
            if pattern.search(head_bytes, start):
                return li
        else:
            try:
                length = struct.calcsize(format)
            except struct.error as ex:
                warnings.warn(('error    in    %s    magic    number    struct    format:    %r' % (li, format)), InvalidLangInfoWarning)
            end = (start + length)
            bytes = head_bytes[start:end]
            if (len(bytes) == length):
                if (struct.unpack(format, bytes)[0] == pattern):
                    return li
