from pylons.controllers.util import abort, redirect
from pylons.controllers import WSGIController
from pylons.templating import render_mako as render

import re, base64, urllib, uuid, inspect
from core import Auth, SwordError, AuthException, DepositRequest, DeleteRequest
from negotiator import ContentNegotiator, AcceptParameters, ContentType
from spec import Errors, HttpHeaders, ValidationException


import logging
ssslog = logging.getLogger(__name__)

# create the global configuration and import the implementation classes
from sss import Configuration
config = Configuration()
Authenticator = config.get_authenticator_implementation()
SwordServer = config.get_server_implementation()
WebInterface = config.get_webui_implementation()

__controller__ = "SwordController"

HEADER_MAP = {
    HttpHeaders.in_progress : "HTTP_IN_PROGRESS",
    HttpHeaders.metadata_relevant : "HTTP_METADATA_RELEVANT",
    HttpHeaders.on_behalf_of : "HTTP_ON_BEHALF_OF"
}

class SwordController(WSGIController):

    # WSGI Controller Stuff
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect
from pylons.controllers import WSGIController
from pylons.templating import render_mako as render

import re, base64, urllib, uuid, inspect
from core import Auth, SwordError, AuthException, DepositRequest, DeleteRequest
from negotiator import ContentNegotiator, AcceptParameters, ContentType
from spec import Errors, HttpHeaders, ValidationException

import logging
ssslog = logging.getLogger(__name__)

# create the global configuration and import the implementation classes
from sss import Configuration
config = Configuration()
Authenticator = config.get_authenticator_implementation()
SwordServer = config.get_server_implementation()
WebInterface = config.get_webui_implementation()

__controller__ = "SwordController"

HEADER_MAP = {
    HttpHeaders.in_progress: "HTTP_IN_PROGRESS",
    HttpHeaders.metadata_relevant: "HTTP_METADATA_RELEVANT",
    HttpHeaders.on_behalf_of: "HTTP_ON_BEHALF_OF"
}


class SwordController(WSGIController):
 def test_01_init(self):
     # before beginning, remove any existing configuration file
     if os.path.isfile(SSS_CONFIG_FILE):
         os.remove(SSS_CONFIG_FILE)
 
     c = Configuration()
 
     # check that the config file specified exists/has been created
     assert os.path.isfile(c.SSS_CONFIG_FILE)
     
     # check that the configuration has been loaded successfully
     assert c.cfg is not None
     
     # check that __getattr__ is behaving correctly
     assert c.ajklsdhfoiqajriojwelkasfljw is None
     
     # check that we can set and retrieve ingesters and disseminators
     c.package_disseminators = { "(& (application/zip))" : "sss.ingesters_disseminators.DefaultDisseminator" }
     c.package_ingesters = { "http://whatever/" :  "sss.ingesters_disseminators.BinaryIngester"}
     
     # should be able to get back an class handle for DefaultDisseminator
     klazz = c.get_package_disseminator("(& (application/zip))")
     assert klazz.__name__ == "DefaultDisseminator"
     
     # and one for DefaultIngester
     clazz = c.get_package_ingester("http://whatever/")
     assert clazz.__name__ == "BinaryIngester"
     
     # should also get None when we ask for something else
     assert c.get_package_disseminator("other") is None
     assert c.get_package_ingester("other") is None
     
     # check that we can get accept parameters for the media formats
     c.media_resource_formats = [
         {"content_type" : "application/zip", "packaging" : "http://purl.org/net/sword/package/SimpleZip"},
         {"content_type" : "application/zip"},
         {"content_type" : "application/atom+xml;type=feed"},
         {"content_type" : "text/html"}
     ]
     c.media_resource_default = {
         "content_type" : "application/zip"
     }
     
     default, acceptable = c.get_media_resource_formats()
     
     assert isinstance(default, AcceptParameters)
     assert len(acceptable) == 4
     
     assert default.content_type.mimetype() == "application/zip"
     
     for acc in acceptable:
         assert isinstance(acc, AcceptParameters)
         assert acc.content_type.mimetype() == "application/zip" or acc.content_type.mimetype() == "application/atom+xml;type=feed" or acc.content_type.mimetype() == "text/html"
         if acc.packaging is not None:
             assert acc.packaging == "http://purl.org/net/sword/package/SimpleZip"