# Path: tldp/typeguesser.py
# def guess(fname):
#     '''return a tldp.doctype class which is a best guess for document type
# 
#     :parama fname: A filename.
# 
#     The guess function will try to guess the document type (doctype) from the
#     file extension.  If extension matching produces multiple possible doctype
#     matches (e.g. .xml or .sgml), the guess function will then use signature
#     matching to find the earliest match in the file for a signature.
# 
#     If there are multiple signature matches, it will choose the signature
#     matching at the earliest position in the file.
# 
#     Bugs/shortcomings:
# 
#       * This is only a guesser.
#       * When signature matching, it reports first signature it discovers in
#         any input file.
#       * It could/should read more than 1024 bytes (cf. SignatureChecker)
#         especially if it cannot return any result.
#       * It could/should use heuristics or something richer than signatures.
#     '''
#     try:
#         stem, ext = os.path.splitext(fname)
#     except (AttributeError, TypeError):
#         return None
# 
#     if not ext:
#         logger.debug("%s no file extension, skipping %s.", stem, ext)
#         return None
# 
#     possible = [t for t in knowndoctypes if ext in t.extensions]
#     logger.debug("Possible:  %r", possible)
#     if not possible:
#         logger.debug("%s unknown extension %s.", stem, ext)
#         return None
# 
#     if len(possible) == 1:
#         doctype = possible.pop()
#         return doctype
# 
#     # -- for this extension, multiple document types, probably SGML, XML
#     #
#     logger.debug("%s multiple possible doctypes for extension %s on file %s.",
#                  stem, ext, fname)
#     for doctype in possible:
#         logger.debug("%s extension %s could be %s.", stem, ext, doctype)
# 
#     try:
#         with codecs.open(fname, encoding='utf-8') as f:
#             buf = f.read(1024)
#     except UnicodeDecodeError:
#         # -- a wee bit ugly, but many SGML docs used iso-8859-1, so fall back
#         with codecs.open(fname, encoding='iso-8859-1') as f:
#             buf = f.read(1024)
# 
#     guesses = list()
#     for doctype in possible:
#         sindex = doctype.signatureLocation(buf, fname)
#         if sindex is not None:
#             guesses.append((sindex, doctype))
# 
#     if not guesses:
#         logger.warning("%s no matching signature found for %s.",
#                        stem, fname)
#         return None
#     if len(guesses) == 1:
#         _, doctype = guesses.pop()
#         return doctype
# 
#     # -- OK, this is unusual; we still found multiple document type
#     #    signatures.  Seems rare but unlikely, so we should choose the
#     #    first signature in the file as the more likely document type.
#     #
#     guesses.sort()
#     logger.info("%s multiple doctype guesses for file %s", stem, fname)
#     for sindex, doctype in guesses:
#         logger.info("%s could be %s (sig at pos %s)", stem, doctype, sindex)
#     logger.info("%s going to guess %s for %s", stem, doctype, fname)
#     _, doctype = guesses.pop(0)
#     return doctype
# 
# Path: tldp/doctypes/common.py
# class SignatureChecker(object):
# 
#     @classmethod
#     def signatureLocation(cls, buf, fname):
#         for sig in cls.signatures:
#             try:
#                 sindex = buf.index(sig)
#                 logger.debug("YES FOUND signature %r in %s at %s; doctype %s.",
#                              sig, fname, sindex, cls)
#                 return sindex
#             except ValueError:
#                 logger.debug("not found signature %r in %s for type %s",
#                              sig, fname, cls.__name__)
#         return None


import os
import codecs
import unittest
import example
from tempfile import NamedTemporaryFile as ntf
from tldp.typeguesser import guess
from tldp.doctypes.common import SignatureChecker
opd = os.path.dirname
opa = os.path.abspath

sampledocs = opj(opd(__file__), 'sample-documents')


def genericGuessTest(content, ext):
        tf = ntf(prefix='tldp-guesser-test-', suffix=ext, delete=False)
        tf.close()
        with codecs.open(tf.name, 'w', encoding='utf-8') as f:
            f.write(content)
        dt = guess(tf.name)
        os.unlink(tf.name)
        return dt


class TestDoctypes(unittest.TestCase):

    def testISO_8859_1(self):
        dt = guess(opj(sampledocs, 'ISO-8859-1.sgml'))
        self.assertIsNotNone(dt)

    def testDetectionBySignature(self):
        for ex in example.sources:
            if isinstance(ex.doctype, SignatureChecker):
                dt = genericGuessTest(ex.content, ex['ext'])
                self.assertEqual(ex.doctype, dt)

    def testDetectionByExtension(self):
        for ex in example.sources:

# Path: tldp/typeguesser.py
# def guess(fname):
#     '''return a tldp.doctype class which is a best guess for document type
# 
#     :parama fname: A filename.
# 
#     The guess function will try to guess the document type (doctype) from the
#     file extension.  If extension matching produces multiple possible doctype
#     matches (e.g. .xml or .sgml), the guess function will then use signature
#     matching to find the earliest match in the file for a signature.
# 
#     If there are multiple signature matches, it will choose the signature
#     matching at the earliest position in the file.
# 
#     Bugs/shortcomings:
# 
#       * This is only a guesser.
#       * When signature matching, it reports first signature it discovers in
#         any input file.
#       * It could/should read more than 1024 bytes (cf. SignatureChecker)
#         especially if it cannot return any result.
#       * It could/should use heuristics or something richer than signatures.
#     '''
#     try:
#         stem, ext = os.path.splitext(fname)
#     except (AttributeError, TypeError):
#         return None
# 
#     if not ext:
#         logger.debug("%s no file extension, skipping %s.", stem, ext)
#         return None
# 
#     possible = [t for t in knowndoctypes if ext in t.extensions]
#     logger.debug("Possible:  %r", possible)
#     if not possible:
#         logger.debug("%s unknown extension %s.", stem, ext)
#         return None
# 
#     if len(possible) == 1:
#         doctype = possible.pop()
#         return doctype
# 
#     # -- for this extension, multiple document types, probably SGML, XML
#     #
#     logger.debug("%s multiple possible doctypes for extension %s on file %s.",
#                  stem, ext, fname)
#     for doctype in possible:
#         logger.debug("%s extension %s could be %s.", stem, ext, doctype)
# 
#     try:
#         with codecs.open(fname, encoding='utf-8') as f:
#             buf = f.read(1024)
#     except UnicodeDecodeError:
#         # -- a wee bit ugly, but many SGML docs used iso-8859-1, so fall back
#         with codecs.open(fname, encoding='iso-8859-1') as f:
#             buf = f.read(1024)
# 
#     guesses = list()
#     for doctype in possible:
#         sindex = doctype.signatureLocation(buf, fname)
#         if sindex is not None:
#             guesses.append((sindex, doctype))
# 
#     if not guesses:
#         logger.warning("%s no matching signature found for %s.",
#                        stem, fname)
#         return None
#     if len(guesses) == 1:
#         _, doctype = guesses.pop()
#         return doctype
# 
#     # -- OK, this is unusual; we still found multiple document type
#     #    signatures.  Seems rare but unlikely, so we should choose the
#     #    first signature in the file as the more likely document type.
#     #
#     guesses.sort()
#     logger.info("%s multiple doctype guesses for file %s", stem, fname)
#     for sindex, doctype in guesses:
#         logger.info("%s could be %s (sig at pos %s)", stem, doctype, sindex)
#     logger.info("%s going to guess %s for %s", stem, doctype, fname)
#     _, doctype = guesses.pop(0)
#     return doctype
# 
# Path: tldp/doctypes/common.py
# class SignatureChecker(object):
# 
#     @classmethod
#     def signatureLocation(cls, buf, fname):
#         for sig in cls.signatures:
#             try:
#                 sindex = buf.index(sig)
#                 logger.debug("YES FOUND signature %r in %s at %s; doctype %s.",
#                              sig, fname, sindex, cls)
#                 return sindex
#             except ValueError:
#                 logger.debug("not found signature %r in %s for type %s",
#                              sig, fname, cls.__name__)
#         return None


# Path: tests/test_typeguesser.py
import os
import codecs
import unittest
import example
from tempfile import NamedTemporaryFile as ntf
from tldp.typeguesser import guess
from tldp.doctypes.common import SignatureChecker

opd = os.path.dirname
opa = os.path.abspath

sampledocs = opj(opd(__file__), 'sample-documents')


def genericGuessTest(content, ext):
        tf = ntf(prefix='tldp-guesser-test-', suffix=ext, delete=False)
        tf.close()
        with codecs.open(tf.name, 'w', encoding='utf-8') as f:
            f.write(content)
        dt = guess(tf.name)
        os.unlink(tf.name)
        return dt


class TestDoctypes(unittest.TestCase):

    def testISO_8859_1(self):
        dt = guess(opj(sampledocs, 'ISO-8859-1.sgml'))
        self.assertIsNotNone(dt)

    def testDetectionBySignature(self):
        for ex in example.sources:
            if isinstance(ex.doctype, SignatureChecker):
                dt = genericGuessTest(ex.content, ex['ext'])
                self.assertEqual(ex.doctype, dt)

    def testDetectionByExtension(self):
        for ex in example.sources:

            if not isinstance(ex.doctype, SignatureChecker):