def mp_legacy_publisher(req, possible_module, possible_handler):
    """
    mod_python legacy publisher minimum implementation.
    """
    the_module = open(possible_module).read()
    module_globals = {}
    exec(the_module, module_globals)
    if possible_handler in module_globals and callable(module_globals[possible_handler]):
        from invenio.webinterface_handler import _check_result
        ## req is the required first parameter of any handler
        expected_args = list(inspect.getargspec(module_globals[possible_handler])[0])
        if not expected_args or 'req' != expected_args[0]:
            ## req was not the first argument. Too bad!
            raise SERVER_RETURN, HTTP_NOT_FOUND
        ## the req.form must be casted to dict because of Python 2.4 and earlier
        ## otherwise any object exposing the mapping interface can be
        ## used with the magic **
        form = dict(req.form)
        for key, value in form.items():
            ## FIXME: this is a backward compatibility workaround
            ## because most of the old administration web handler
            ## expect parameters to be of type str.
            ## When legacy publisher will be removed all this
            ## pain will go away anyway :-)
            if isinstance(value, str):
                form[key] = str(value)
            else:
                ## NOTE: this is a workaround for e.g. legacy webupload
                ## that is still using legacy publisher and expect to
                ## have a file (Field) instance instead of a string.
                form[key] = value

        try:
            return _check_result(req, module_globals[possible_handler](req, **form))
        except TypeError, err:
            if ("%s() got an unexpected keyword argument" % possible_handler) in str(err) or ('%s() takes at least' % possible_handler) in str(err):
                inspected_args = inspect.getargspec(module_globals[possible_handler])
                expected_args = list(inspected_args[0])
                expected_defaults = list(inspected_args[3])
                expected_args.reverse()
                expected_defaults.reverse()
                register_exception(req=req, prefix="Wrong GET parameter set in calling a legacy publisher handler for %s: expected_args=%s, found_args=%s" % (possible_handler, repr(expected_args), repr(req.form.keys())), alert_admin=CFG_DEVEL_SITE)
                cleaned_form = {}
                for index, arg in enumerate(expected_args):
                    if arg == 'req':
                        continue
                    if index < len(expected_defaults):
                        cleaned_form[arg] = form.get(arg, expected_defaults[index])
                    else:
                        cleaned_form[arg] = form.get(arg, None)
                return _check_result(req, module_globals[possible_handler](req, **cleaned_form))
            else:
                raise
def mp_legacy_publisher(req, possible_module, possible_handler):
    """
    mod_python legacy publisher minimum implementation.
    """
    the_module = open(possible_module).read()
    module_globals = {}
    exec(the_module, module_globals)
    if possible_handler in module_globals and callable(module_globals[possible_handler]):
        from invenio.webinterface_handler import _check_result
        ## req is the required first parameter of any handler
        expected_args = list(inspect.getargspec(module_globals[possible_handler])[0])
        if not expected_args or 'req' != expected_args[0]:
            ## req was not the first argument. Too bad!
            raise SERVER_RETURN, HTTP_NOT_FOUND
        ## the req.form must be casted to dict because of Python 2.4 and earlier
        ## otherwise any object exposing the mapping interface can be
        ## used with the magic **
        form = dict(req.form)
        for key, value in form.items():
            ## FIXME: this is a backward compatibility workaround
            ## because most of the old administration web handler
            ## expect parameters to be of type str.
            ## When legacy publisher will be removed all this
            ## pain will go away anyway :-)
            if isinstance(value, str):
                form[key] = str(value)
            else:
                ## NOTE: this is a workaround for e.g. legacy webupload
                ## that is still using legacy publisher and expect to
                ## have a file (Field) instance instead of a string.
                form[key] = value

        if (CFG_FULL_HTTPS or CFG_HAS_HTTPS_SUPPORT and get_session(req).need_https) and not req.is_https():
            from invenio.urlutils import redirect_to_url
            # We need to isolate the part of the URI that is after
            # CFG_SITE_URL, and append that to our CFG_SITE_SECURE_URL.
            original_parts = urlparse(req.unparsed_uri)
            plain_prefix_parts = urlparse(CFG_SITE_URL)
            secure_prefix_parts = urlparse(CFG_SITE_SECURE_URL)

            # Compute the new path
            plain_path = original_parts[2]
            plain_path = secure_prefix_parts[2] + \
                         plain_path[len(plain_prefix_parts[2]):]

            # ...and recompose the complete URL
            final_parts = list(secure_prefix_parts)
            final_parts[2] = plain_path
            final_parts[-3:] = original_parts[-3:]

            target = urlunparse(final_parts)
            redirect_to_url(req, target)

        try:
            return _check_result(req, module_globals[possible_handler](req, **form))
        except TypeError, err:
            if ("%s() got an unexpected keyword argument" % possible_handler) in str(err) or ('%s() takes at least' % possible_handler) in str(err):
                inspected_args = inspect.getargspec(module_globals[possible_handler])
                expected_args = list(inspected_args[0])
                expected_defaults = list(inspected_args[3])
                expected_args.reverse()
                expected_defaults.reverse()
                register_exception(req=req, prefix="Wrong GET parameter set in calling a legacy publisher handler for %s: expected_args=%s, found_args=%s" % (possible_handler, repr(expected_args), repr(req.form.keys())), alert_admin=CFG_DEVEL_SITE)
                cleaned_form = {}
                for index, arg in enumerate(expected_args):
                    if arg == 'req':
                        continue
                    if index < len(expected_defaults):
                        cleaned_form[arg] = form.get(arg, expected_defaults[index])
                    else:
                        cleaned_form[arg] = form.get(arg, None)
                return _check_result(req, module_globals[possible_handler](req, **cleaned_form))
            else:
                raise
def mp_legacy_publisher(req, possible_module, possible_handler):
    """
    mod_python legacy publisher minimum implementation.
    """
    from invenio.session import get_session
    from invenio.webinterface_handler import CFG_HAS_HTTPS_SUPPORT, CFG_FULL_HTTPS
    the_module = open(possible_module).read()
    module_globals = {}
    exec(the_module, module_globals)
    if possible_handler in module_globals and callable(
            module_globals[possible_handler]):
        from invenio.webinterface_handler import _check_result
        ## req is the required first parameter of any handler
        expected_args = list(
            inspect.getargspec(module_globals[possible_handler])[0])
        if not expected_args or 'req' != expected_args[0]:
            ## req was not the first argument. Too bad!
            raise SERVER_RETURN, HTTP_NOT_FOUND
        ## the req.form must be casted to dict because of Python 2.4 and earlier
        ## otherwise any object exposing the mapping interface can be
        ## used with the magic **
        form = dict()
        for key, value in req.form.items():
            ## FIXME: this is a backward compatibility workaround
            ## because most of the old administration web handler
            ## expect parameters to be of type str.
            ## When legacy publisher will be removed all this
            ## pain will go away anyway :-)
            if isinstance(value, unicode):
                form[key] = value.encode('utf8')
            else:
                ## NOTE: this is a workaround for e.g. legacy webupload
                ## that is still using legacy publisher and expect to
                ## have a file (Field) instance instead of a string.
                form[key] = value

        if (CFG_FULL_HTTPS or CFG_HAS_HTTPS_SUPPORT
                and get_session(req).need_https) and not req.is_https():
            from invenio.urlutils import redirect_to_url
            # We need to isolate the part of the URI that is after
            # CFG_SITE_URL, and append that to our CFG_SITE_SECURE_URL.
            original_parts = urlparse(req.unparsed_uri)
            plain_prefix_parts = urlparse(CFG_SITE_URL)
            secure_prefix_parts = urlparse(CFG_SITE_SECURE_URL)

            # Compute the new path
            plain_path = original_parts[2]
            plain_path = secure_prefix_parts[2] + \
                         plain_path[len(plain_prefix_parts[2]):]

            # ...and recompose the complete URL
            final_parts = list(secure_prefix_parts)
            final_parts[2] = plain_path
            final_parts[-3:] = original_parts[-3:]

            target = urlunparse(final_parts)
            redirect_to_url(req, target)

        try:
            return _check_result(req, module_globals[possible_handler](req,
                                                                       **form))
        except TypeError, err:
            if ("%s() got an unexpected keyword argument" %
                    possible_handler) in str(err) or (
                        '%s() takes at least' % possible_handler) in str(err):
                inspected_args = inspect.getargspec(
                    module_globals[possible_handler])
                expected_args = list(inspected_args[0])
                expected_defaults = list(inspected_args[3])
                expected_args.reverse()
                expected_defaults.reverse()
                register_exception(
                    req=req,
                    prefix=
                    "Wrong GET parameter set in calling a legacy publisher handler for %s: expected_args=%s, found_args=%s"
                    % (possible_handler, repr(expected_args),
                       repr(req.form.keys())),
                    alert_admin=CFG_DEVEL_SITE)
                cleaned_form = {}
                for index, arg in enumerate(expected_args):
                    if arg == 'req':
                        continue
                    if index < len(expected_defaults):
                        cleaned_form[arg] = form.get(arg,
                                                     expected_defaults[index])
                    else:
                        cleaned_form[arg] = form.get(arg, None)
                return _check_result(
                    req, module_globals[possible_handler](req, **cleaned_form))
            else:
                raise