import os
import lasso

from quixote import get_publisher, get_response, get_session, redirect, get_session_manager
from quixote.directory import Directory
from quixote.util import StaticDirectory

import admin
import liberty_root
import saml2

import errors
import logger
import misc
import template
from form import *

from users import User

import httplib
from quixote.form import Form, StringWidget, PasswordWidget, \
     RadiobuttonsWidget, SingleSelectWidget, MultipleSelectWidget, \
     CheckboxWidget, FileWidget

class RootDirectory(Directory):
    _q_exports = ['', 'admin', 'liberty', 'token', 'saml']

    def _q_index (self):
        raise errors.TraversalError()

    def _q_traverse(self, path):
        session = get_session()
        if session:
            get_request().user = session.get_user()
        else:
            get_request().user = None

        response = get_response()
        response.filter = {}
#        if not hasattr(response, 'breadcrumb'):
#            response.breadcrumb = [ ('', _('Home')) ]

        try:
            return Directory._q_traverse(self, path)
        except errors.TraversalError:
            pass

        return self._q_index()

    # Make it so that http://proxy_domain/directory gets a trailing slash
    def _q_lookup(self, component):
#        return SiteUI(component)
#        if component == 'themes':
#            dirname = os.path.join(get_publisher().data_dir, 'themes')
#            return StaticDirectory(dirname, follow_symlinks = True)

        # is this a category ?
#        try:
#            category = Category.get_by_urlname(component)
#        except KeyError:
#            pass
#        else:
#            return forms.root.RootDirectory(category)

        # or a form ?
#        return forms.root.RootDirectory()._q_lookup(component)
        return self._q_index()

    admin = admin.RootDirectory()
    saml = saml2.RootDirectory()
    liberty = liberty_root.LibertyRootDirectory()
