"""$URL: svn+ssh://svn/repos/trunk/dulcinea/lib/traversal.py $
$Id: traversal.py 26248 2005-02-25 14:56:37Z dbinger $
"""

from quixote import enable_ptl
enable_ptl()
from sets import Set as set
from quixote import get_session
from dulcinea.site_util import get_root_directory
from dulcinea.ui.crumbs import get_exports
from dulcinea.ui.directory import DynamicExportingDirectory
from dulcinea.ui.publisher import DulcineaPublisher
from quixote.directory import Directory
from quixote.http_request import HTTPRequest
from dulcinea import local


def check_exports():
    db = local.open_database()
    publisher = DulcineaPublisher()
    publisher._set_request(HTTPRequest(None, dict(SCRIPT_NAME='',
                                                  PATH_INFO='/')))
    admin_user = local.get_user_db().get_admin().get_direct_grantees(
        'staff')[0]
    publisher.start_request()
    get_session().user = admin_user
    todo = set([get_root_directory()])
    seen = set()
    while todo:
        directory = todo.pop()
        seen.add(directory)
        if isinstance(directory, DynamicExportingDirectory):
            if not hasattr(directory, 'get_exports'):
                print directory, 'is missing get_exports()'
        else:
            if not hasattr(directory, '_q_exports'):
                print directory, 'is missing _q_exports'
        if (getattr(directory, '_q_exports', None) and
            getattr(directory, 'get_exports', None)):
            print directory, 'has both _q_exports and get_exports()'
        unused_q_index = hasattr(directory, '_q_index')
        for component, export, crumb, title in get_exports(directory):
            if component == "" and export == '_q_index':
                unused_q_index = False
            if not hasattr(directory, export):
                if hasattr(directory, '_q_resolve'):
                    print directory, '_q_resolve must provide', export
                else:
                    print directory, 'is missing', export
            else:
                if isinstance(getattr(directory, export, None),
                              Directory):
                    todo.add(getattr(directory, export, None))
        if unused_q_index:
            print directory, 'has an unused _q_index'
    db.close()
    import quixote.publish
    quixote.publish._publisher = None
