# Path: tldp/ldpcollection.py
# class LDPDocumentCollection(collections.MutableMapping):
#     '''a dict-like container for DocumentCollection objects
# 
#     Intended to be subclassed.
# 
#     Implements all the usual dictionary stuff, but also provides sorted
#     lists of documents in the collection.
#     '''
#     def __repr__(self):
#         return '<%s:(%s docs)>' % (self.__class__.__name__, len(self))
# 
#     def __delitem__(self, key):
#         del self.__dict__[key]
# 
#     def __getitem__(self, key):
#         return self.__dict__[key]
# 
#     def __setitem__(self, key, value):
#         self.__dict__[key] = value
# 
#     def __iter__(self):
#         return iter(self.__dict__)
# 
#     def __len__(self):
#         return len(self.__dict__)
# 
#     def iterkeys(self):
#         return iter(self.keys)
# 
#     def itervalues(self):
#         for key in sorted(self, key=lambda x: x.lower()):
#             yield self[key]
# 
#     def iteritems(self):
#         for key in self.keys:
#             yield (key, self[key])
# 
#     def keys(self):
#         return sorted(self, key=lambda x: x.lower())
# 
#     def items(self):
#         return [(key, self[key]) for key in self.keys()]
# 
#     def values(self):
#         return [self[key] for key in self.keys()]
# 
# Path: tldp/utils.py
# def md5files(name, relative=None):
#     '''get all of the MD5s for files from here downtree'''
#     return fileinfo(name, relative=relative, func=md5file)
# 
# def stem_and_ext(name):
#     '''return (stem, ext) for any relative or absolute filename'''
#     return os.path.splitext(os.path.basename(os.path.normpath(name)))
# 
# Path: tldp/typeguesser.py
# def getDoctypeMembers(membertype):
# def getDoctypeClasses():
# def guess(fname):


import os
import sys
import errno
import logging
from tldp.ldpcollection import LDPDocumentCollection
from tldp.utils import md5files, stem_and_ext
from tldp.typeguesser import guess, knownextensions
    if os.path.isfile(filename):
        if os.path.basename(filename) in IGNORABLE_SOURCE:
            return None
        return filename
    elif os.path.isdir(filename):
        return sourcedoc_fromdir(filename)
    return None


def sourcedoc_fromdir(name):
    candidates = list()
    if not os.path.isdir(name):
        return None
    stem = os.path.basename(name)
    for ext in knownextensions:
        possible = os.path.join(name, stem + ext)
        if os.path.isfile(possible):
            candidates.append(possible)
    if len(candidates) > 1:
        logger.warning("%s multiple document choices in dir %s, bailing....",
                       stem, name)
        raise Exception("multiple document choices in " + name)
    elif len(candidates) == 0:
        return None
    else:
        doc = candidates.pop()
        logger.debug("%s identified main document %s.", stem, doc)
        return doc



# Path: tldp/ldpcollection.py
# class LDPDocumentCollection(collections.MutableMapping):
#     '''a dict-like container for DocumentCollection objects
# 
#     Intended to be subclassed.
# 
#     Implements all the usual dictionary stuff, but also provides sorted
#     lists of documents in the collection.
#     '''
#     def __repr__(self):
#         return '<%s:(%s docs)>' % (self.__class__.__name__, len(self))
# 
#     def __delitem__(self, key):
#         del self.__dict__[key]
# 
#     def __getitem__(self, key):
#         return self.__dict__[key]
# 
#     def __setitem__(self, key, value):
#         self.__dict__[key] = value
# 
#     def __iter__(self):
#         return iter(self.__dict__)
# 
#     def __len__(self):
#         return len(self.__dict__)
# 
#     def iterkeys(self):
#         return iter(self.keys)
# 
#     def itervalues(self):
#         for key in sorted(self, key=lambda x: x.lower()):
#             yield self[key]
# 
#     def iteritems(self):
#         for key in self.keys:
#             yield (key, self[key])
# 
#     def keys(self):
#         return sorted(self, key=lambda x: x.lower())
# 
#     def items(self):
#         return [(key, self[key]) for key in self.keys()]
# 
#     def values(self):
#         return [self[key] for key in self.keys()]
# 
# Path: tldp/utils.py
# def md5files(name, relative=None):
#     '''get all of the MD5s for files from here downtree'''
#     return fileinfo(name, relative=relative, func=md5file)
# 
# def stem_and_ext(name):
#     '''return (stem, ext) for any relative or absolute filename'''
#     return os.path.splitext(os.path.basename(os.path.normpath(name)))
# 
# Path: tldp/typeguesser.py
# def getDoctypeMembers(membertype):
# def getDoctypeClasses():
# def guess(fname):


# Path: tldp/sources.py
import os
import sys
import errno
import logging
from tldp.ldpcollection import LDPDocumentCollection
from tldp.utils import md5files, stem_and_ext
from tldp.typeguesser import guess, knownextensions

    if os.path.isfile(filename):
        if os.path.basename(filename) in IGNORABLE_SOURCE:
            return None
        return filename
    elif os.path.isdir(filename):
        return sourcedoc_fromdir(filename)
    return None


def sourcedoc_fromdir(name):
    candidates = list()
    if not os.path.isdir(name):
        return None
    stem = os.path.basename(name)
    for ext in knownextensions:
        possible = os.path.join(name, stem + ext)
        if os.path.isfile(possible):
            candidates.append(possible)
    if len(candidates) > 1:
        logger.warning("%s multiple document choices in dir %s, bailing....",
                       stem, name)
        raise Exception("multiple document choices in " + name)
    elif len(candidates) == 0:
        return None
    else:
        doc = candidates.pop()
        logger.debug("%s identified main document %s.", stem, doc)
        return doc



class SourceCollection(LDPDocumentCollection):