Example #1
0
	def __del__(self):
		""" Closes the file when there is no further reference """
		try:
			self.close()
		except:
			logger.warn('Error closing file %s'%self.uri.get_readable())
			utils.format_error()
Example #2
0
    def error_handler(self, e, request, meth, em_format):
        """
        Override this method to add handling of errors customized for your
        needs
        """
        if isinstance(e, FormValidationError):
            return self.form_validation_response(e, self.determine_emitter(request))

        elif isinstance(e, TypeError):
            result = rc.BAD_REQUEST

            msg = "Method signature does not match.\n\n"

            try:
                hm = HandlerMethod(meth)
                sig = hm.signature

            except TypeError:
                msg += "Signature could not be determined"

            else:
                if sig:
                    msg += "Signature should be: %s" % sig
                else:
                    msg += "Resource does not expect any parameters."

            if self.display_errors:
                msg += "\n\nException was: %s" % str(e)

            result.content = format_error(msg)
            return result
        elif isinstance(e, Http404):
            return rc.NOT_FOUND

        elif isinstance(e, HttpStatusCode):
            return e.response

        else:
            """
            On errors (like code errors), we'd like to be able to
            give crash reports to both admins and also the calling
            user. There's two setting parameters for this:

            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback
               to the caller, so he can tell you what error they got.

            If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will
            receive a basic "500 Internal Server Error" message.
            """
            exc_type, exc_value, tb = sys.exc_info()
            rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)
            if self.email_errors:
                self.email_exception(rep)
            if self.display_errors:
                return HttpResponseServerError(format_error("\n".join(rep.format_exception())))
            else:
                raise
Example #3
0
    def msg(self, to, *args):
        """ Sends a message to other nodes
		- to The identifier of the point of the ring that gets the
		message. Note that may or not be the same that the identifier of
		the node that manages that segment of the ring.
		- *args A list of arguments to pass to the other node ('the
		message'). Binary arguments must be wrapped in a xmlprclib.Binary
		object, and decoded in the destinity """
        logger.info('%s: new application message' % self.id)
        if self.manage(to):
            # it is for me
            if not self.listener: return None
            try:
                return self.listener.message(to, *args)
            except:
                return "ERROR: " % utils.format_error()
        else:
            # it is not for me: inform to the listeners
            # and answers if there is a response
            r = None
            try:
                if self.listener:
                    r = self.listener.routing(to, *args)
            except:
                logger.warn('Routing app: ' + utils.format_error())
            if r:
                return r
            else:
                return self.__next(to).msg(to, *args)
Example #4
0
File: ring.py Project: Juanvvc/scfs
	def msg(self, to, *args):
		""" Sends a message to other nodes
		- to The identifier of the point of the ring that gets the
		message. Note that may or not be the same that the identifier of
		the node that manages that segment of the ring.
		- *args A list of arguments to pass to the other node ('the
		message'). Binary arguments must be wrapped in a xmlprclib.Binary
		object, and decoded in the destinity """	
		logger.info('%s: new application message'%self.id)
		if self.manage(to):
			# it is for me
			if not self.listener: return None
			try:
				return self.listener.message(to, *args)
			except:
				return "ERROR: "%utils.format_error()
		else:
			# it is not for me: inform to the listeners
			# and answers if there is a response
			r=None
			try:
				if self.listener:
					r=self.listener.routing(to, *args)
			except:
				logger.warn('Routing app: ' + utils.format_error())
			if r:
				return r
			else:
				return self.__next(to).msg(to, *args)
Example #5
0
    def error_handler(self, e, request, meth, em_format):
        """
        Override this method to add handling of errors customized for your
        needs
        """
        if isinstance(e, FormValidationError):
            return self.form_validation_response(e)

        elif isinstance(e, TypeError):
            result = rc.BAD_REQUEST
            hm = HandlerMethod(meth)
            sig = hm.signature

            msg = 'Method signature does not match.\n\n'

            if sig:
                msg += 'Signature should be: %s' % sig
            else:
                msg += 'Resource does not expect any parameters.'

            if self.display_errors:
                msg += '\n\nException was: %s' % str(e)

            result.content = format_error(msg)
            return result
        elif isinstance(e, Http404):
            return rc.NOT_FOUND

        elif isinstance(e, HttpStatusCode):
            return e.response

        else:
            """
            On errors (like code errors), we'd like to be able to
            give crash reports to both admins and also the calling
            user. There's two setting parameters for this:

            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback
               to the caller, so he can tell you what error they got.

            If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will
            receive a basic "500 Internal Server Error" message.
            """
            exc_type, exc_value, tb = sys.exc_info()
            rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)
            if self.email_errors:
                self.email_exception(rep)
            if self.display_errors:
                return HttpResponseServerError(
                    format_error('\n'.join(rep.format_exception())))
            else:
                raise
Example #6
0
File: ring.py Project: Juanvvc/scfs
	def leave(self):
		""" Leaves the network """
		try:
			logger.debug('%s: leaving the network'%self.id)
			if self.joined and self.next:
				na = self.contacted[self.next]
				if self.prev: self.__next(self.prev).leave_msg(self.id, self.next, na[0], na[1])
				if self.listener: self.listener.left(None)
				self.joined = False
				logger.info('%s: left the network'%self.id)
			else:
				logger.info('%s: I was alone in the network'%self.id)
		except:
			utils.format_error()
		self.server.shutdown()
Example #7
0
def draw_toyMC(hist,title,xlabel="",ylabel="",exponent=None, box="tl"):
    fit = root.TF1("gauss","gaus")
    textbox = r"\begin{align*}\mu&=%s\\\sigma&=%s\end{align*}"
    fig, a = utils.get_plotaxes((4,3.2))
    hist.Fit(fit,"LQ")
    r2mpl.plot(hist,axes=a, errors=True, color="k", zorder=1)
    r2mpl.plot(fit,axes=a, color="r", zorder=0)
    utils.text_box(a, box, textbox % (
        utils.format_error(fit.GetParameter(1), fit.GetParError(1), exponent=exponent),
        utils.format_error(fit.GetParameter(2), fit.GetParError(2), exponent=exponent)))

    a.set_title(title)
    a.set_xlabel(xlabel)
    a.set_ylabel(ylabel)
    return a, (fit.GetParameter(1), fit.GetParError(1)), (fit.GetParameter(2), fit.GetParError(2))
Example #8
0
 def leave(self):
     """ Leaves the network """
     try:
         logger.debug('%s: leaving the network' % self.id)
         if self.joined and self.next:
             na = self.contacted[self.next]
             if self.prev:
                 self.__next(self.prev).leave_msg(self.id, self.next, na[0],
                                                  na[1])
             if self.listener: self.listener.left(None)
             self.joined = False
             logger.info('%s: left the network' % self.id)
         else:
             logger.info('%s: I was alone in the network' % self.id)
     except:
         utils.format_error()
     self.server.shutdown()
Example #9
0
def calc_eff(efficiency, errors, n=7, scale=1.0):
    total_eff=0
    total_err=0
    for i in range(n):
        for j in range(n):
            n1 = all_channels[i]
            n2 = all_channels[j]
            total_eff += br_Ds[n1] * br_Ds[n2] * br[n1] * br[n2] * efficiency[i,j]
            total_err += (br_Ds[n1] * br_Ds[n2] * br[n1] * br[n2] * errors[i,j])**2
    return utils.format_error(total_eff*scale,total_err**.5*scale,exponent=-5)
Example #10
0
 def message(self, to, *args):
     """ Receives a message from the ring """
     try:
         if args[0] == 'GET':
             return self.__get(to, args[1])
         elif args[0] == 'PUT':
             return self.__put(to, args[1], args[2])
         else:
             return 'No such method: %s' % args[0]
     except:
         return utils.format_error()
Example #11
0
File: DHT.py Project: Juanvvc/scfs
	def message(self,to,*args):
		""" Receives a message from the ring """
		try:
			if args[0]=='GET':
				return self.__get(to,args[1])
			elif args[0]=='PUT':
				return self.__put(to,args[1],args[2])
			else:
				return 'No such method: %s'%args[0]
		except:
			return utils.format_error()
Example #12
0
def parse_options():
    _root = os.path.join(os.path.dirname(os.path.dirname(__file__)),"")
    _settings = os.path.join(_root, "settings.py")

    try:
        parse_config_file(_settings)
        logging.info("Using settings.py as default settings.")
        print "Using settings.py as default settings."
    except Exception as e:
        import traceback
        print (utils.format_error())
        logging.error("No any default settings, are you sure? Exception: %s" % e)

    parse_command_line()
Example #13
0
	def _complete_read(self):
		""" Reads the contents of the file."""
		s = []
		try:
			# read and return the whole file
			for p in self.parts:
				logger.info('Reading part ' + p)
				uri = uri_from_string(p)
				d = dfs.dht.get(uri.get_hd(),uri.nick)
				# TODO: do not decrypt now, but in the actual read
				if self.crypter:	d = self.crypter.decrypt(d)
				s.append(d)
			s=''.join(s)
			self.eof = True
			# TODO: check the file hashing before returning
			return s[0:self.filelength]
		except:
			raise IOError('Cannot read: %s'%utils.format_error())
Example #14
0
 def __next(self, to):
     """ route a message to the point 'to' """
     try:
         if type(to) == tuple:
             p = xmlrpclib.ServerProxy("http://%s:%d" % to)
         else:
             # TODO: improve the routing. This one just circle the
             # message in the ring
             if self.contacted.has_key(to):
                 na = self.contacted[to]
             else:
                 na = self.contacted[self.next]
             p = xmlrpclib.ServerProxy('http://%s:%d' % (na[0], na[1]))
         return p
     except:
         if type(to) == str:
             st = to
         else:
             st = '%s:%d' % (to[0], to[1])
         logger.warn("%s: error routing message to %s" % (self.id, st))
         raise IOError(utils.format_error())
Example #15
0
File: ring.py Project: Juanvvc/scfs
	def __next(self,to):
		""" route a message to the point 'to' """
		try:
			if type(to)==tuple:
				p=xmlrpclib.ServerProxy("http://%s:%d"%to)
			else:
				# TODO: improve the routing. This one just circle the
				# message in the ring
				if self.contacted.has_key(to):
					na=self.contacted[to]
				else:
					na=self.contacted[self.next]	
				p=xmlrpclib.ServerProxy('http://%s:%d'%(na[0],na[1]))
			return p
		except:
			if type(to)==str:
				st=to
			else:
				st='%s:%d'%(to[0],to[1])
			logger.warn("%s: error routing message to %s"%(self.id,st))
			raise IOError(utils.format_error())
Example #16
0
    def error_handler(self, e, request, meth, em_format):
        """
        Override this method to add handling of errors customized for your
        needs
        """
        if isinstance(e, FormValidationError):
            return self.form_validation_response(e)

        elif isinstance(e, Http404):
            return rc.NOT_FOUND

        elif isinstance(e, HttpStatusCode):
            return e.response

        else:
            """
            On errors (like code errors), we'd like to be able to
            give crash reports to both admins and also the calling
            user. There's two setting parameters for this:

            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback
               to the caller, so he can tell you what error they got.

            If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will
            receive a basic "500 Internal Server Error" message.
            """

            exc_type, exc_value, tb = sys.exc_info()
            rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)

            if self.email_errors:
                self.email_exception(rep)
            if self.display_errors:
                return HttpResponseServerError(
                    format_error('\n'.join(rep.format_exception())))
            else:
                raise
Example #17
0
    def error_handler(self, e, request, meth, em_format):
        """
        Override this method to add handling of errors customized for your
        needs
        """
        if isinstance(e, FormValidationError):
            return self.form_validation_response(e)

        elif isinstance(e, Http404):
            return rc.NOT_FOUND

        elif isinstance(e, HttpStatusCode):
            return e.response

        else:
            """
            On errors (like code errors), we'd like to be able to
            give crash reports to both admins and also the calling
            user. There's two setting parameters for this:

            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback
               to the caller, so he can tell you what error they got.

            If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will
            receive a basic "500 Internal Server Error" message.
            """
            exc_type, exc_value, tb = sys.exc_info()
            rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)
            if self.email_errors:
                self.email_exception(rep)
            if self.display_errors:
                return HttpResponseServerError(
                    format_error("\n".join(rep.format_exception())))
            else:
                raise
Example #18
0
    ],
    "SVD2": [
        ("signal_corr_svd2_Mbc_mean",  "\Delta\mu(\mbc)"),
        ("signal_corr_svd2_Mbc_sigma", "\delta\sigma(\mbc)"),
        ("signal_corr_svd2_dE_mean",   "\Delta\mu(\de)"),
        ("signal_corr_svd2_dE_sigma",  "\delta\sigma(\de)"),
        ("signal_corr_svd2_rbin1",     "\delta f_Y(rbin0)"),
        ("signal_corr_svd2_rbin2",     "\delta f_Y(rbin1)"),
        ("signal_corr_svd2_rbin3",     "\delta f_Y(rbin2)"),
        ("signal_corr_svd2_rbin4",     "\delta f_Y(rbin3)"),
        ("signal_corr_svd2_rbin5",     "\delta f_Y(rbin4)"),
        ("signal_corr_svd2_rbin6",     "\delta f_Y(rbin5)"),
    ]
}

params = dspdsmks.Parameters()
params.load(sys.argv[1])


for svd, names in sorted(fields.items()):
    print r"""\begin{tabular}{LRCL}
    \toprule
    Name&Value\\
    \midrule"""
    for p,t in names:
        print r"    {0} & {1}\\" .format(t, utils.format_error(params(p).value, params(p).error, align=True))
    print r"""    \bottomrule
\end{tabular}"""


Example #19
0
class Resource(object):
    """
    Resource. Create one for your URL mappings, just
    like you would with Django. Takes one argument,
    the handler. The second argument is optional, and
    is an authentication handler. If not specified,
    `NoAuthentication` will be used by default.
    """
    callmap = {
        'GET': 'read',
        'POST': 'create',
        'PUT': 'update',
        'DELETE': 'delete'
    }

    range_re = re.compile("^items=(\d*)-(\d*)$")

    def __init__(self, handler, authentication=None):
        if not callable(handler):
            raise AttributeError, "Handler not callable."

        self.handler = handler()
        self.csrf_exempt = getattr(self.handler, 'csrf_exempt', True)

        if not authentication:
            self.authentication = (NoAuthentication(), )
        elif isinstance(authentication, (list, tuple)):
            self.authentication = authentication
        else:
            self.authentication = (authentication, )

        # Erroring
        self.email_errors = getattr(settings, 'PISTON_EMAIL_ERRORS', True)
        self.display_errors = getattr(settings, 'PISTON_DISPLAY_ERRORS', True)
        self.stream = getattr(settings, 'PISTON_STREAM_OUTPUT', False)

        # Paging
        paging_params = getattr(settings, 'PISTON_PAGINATION_PARAMS',
                                ('offset', 'limit'))
        self.paging_offset = paging_params[0]
        self.paging_limit = paging_params[1]

    def determine_emitter(self, request, *args, **kwargs):
        """
        Function for determening which emitter to use
        for output. It lives here so you can easily subclass
        `Resource` in order to change how emission is detected.

        You could also check for the `Accept` HTTP header here,
        since that pretty much makes sense. Refer to `Mimer` for
        that as well.
        """
        em = kwargs.pop('emitter_format', None)

        if not em:
            em = request.GET.get('format', 'json')

        return em

    def form_validation_response(self, e):
        """
        Method to return form validation error information. 
        You will probably want to override this in your own
        `Resource` subclass.
        """
        resp = rc.BAD_REQUEST
        resp.write(' ' + str(e.form.errors))
        return resp

    @property
    def anonymous(self):
        """
        Gets the anonymous handler. Also tries to grab a class
        if the `anonymous` value is a string, so that we can define
        anonymous handlers that aren't defined yet (like, when
        you're subclassing your basehandler into an anonymous one.)
        """
        if hasattr(self.handler, 'anonymous'):
            anon = self.handler.anonymous

            if callable(anon):
                return anon

            for klass in typemapper.keys():
                if anon == klass.__name__:
                    return klass

        return None

    def authenticate(self, request, rm):
        actor, anonymous = False, True

        for authenticator in self.authentication:
            if not authenticator.is_authenticated(request):
                if self.anonymous and \
                    rm in self.anonymous.allowed_methods:

                    actor, anonymous = self.anonymous(), True
                else:
                    actor, anonymous = authenticator.challenge, CHALLENGE
            else:
                return self.handler, self.handler.is_anonymous

        return actor, anonymous

    @vary_on_headers('Authorization')
    def __call__(self, request, *args, **kwargs):
        """
        NB: Sends a `Vary` header so we don't cache requests
        that are different (OAuth stuff in `Authorization` header.)
        """
        rm = request.method.upper()

        # Django's internal mechanism doesn't pick up
        # PUT request, so we trick it a little here.
        if rm == "PUT":
            coerce_put_post(request)

        actor, anonymous = self.authenticate(request, rm)

        if anonymous is CHALLENGE:
            return actor()
        else:
            handler = actor

        # Translate nested datastructs into `request.data` here.
        if rm in ('POST', 'PUT'):
            try:
                translate_mime(request)
            except MimerDataException:
                return rc.BAD_REQUEST
            if not hasattr(request, 'data'):
                if rm == 'POST':
                    request.data = request.POST
                else:
                    request.data = request.PUT

        if not rm in handler.allowed_methods:
            return HttpResponseNotAllowed(handler.allowed_methods)

        meth = getattr(handler, self.callmap.get(rm, ''), None)
        if not meth:
            raise Http404

        # Support emitter both through (?P<emitter_format>) and ?format=emitter.
        em_format = self.determine_emitter(request, *args, **kwargs)

        kwargs.pop('emitter_format', None)

        # Clean up the request object a bit, since we might
        # very well have `oauth_`-headers in there, and we
        # don't want to pass these along to the handler.
        request = self.cleanup_request(request)

        try:
            result = meth(request, *args, **kwargs)
        except Exception, e:
            result = self.error_handler(e, request, meth, em_format)

        try:
            emitter, ct = Emitter.get(em_format)
            fields = handler.fields

            if hasattr(handler, 'list_fields') and isinstance(
                    result, (list, tuple, QuerySet)):
                fields = handler.list_fields
        except ValueError:
            result = rc.BAD_REQUEST
            result.content = "Invalid output format specified '%s'." % em_format
            return result

        status_code = 200

        # If we're looking at a response object which contains non-string
        # content, then assume we should use the emitter to format that
        # content
        if isinstance(result, HttpResponse) and not result._is_string:
            status_code = result.status_code
            # Note: We can't use result.content here because that method attempts
            # to convert the content into a string which we don't want.
            # when _is_string is False _container is the raw data
            result = result._container
            if sig:
                msg += 'Signature should be: %s' % sig

        srl = emitter(result, typemapper, handler, fields, anonymous)

        try:
            """
            Decide whether or not we want a generator here,
            or we just want to buffer up the entire result
            before sending it to the client. Won't matter for
            smaller datasets, but larger will have an impact.
            """
            if self.stream: stream = srl.stream_render(request)
            else: stream = srl.render(request)

            if not isinstance(stream, HttpResponse):
                resp = HttpResponse(stream, mimetype=ct, status=status_code)
            else:
                resp = stream

            if self.display_errors:
                msg += '\n\nException was: %s' % str(e)
            resp.streaming = self.stream
            result.content = format_error(msg)
        except Http404:
            return rc.NOT_FOUND
        except HttpStatusCode, e:
            return e.response
Example #20
0
            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback
               to the caller, so he can tell you what error they got.

            If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will
            receive a basic "500 Internal Server Error" message.
            """
            exc_type, exc_value, tb = sys.exc_info()
            rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)
            if self.email_errors:
                self.email_exception(rep)
            if self.display_errors:
                return HttpResponseServerError(
                    format_error('\n'.join(rep.format_exception())))
            else:
                raise

        content_range = None
        if isinstance(result, QuerySet):
            """
            Limit results based on requested items. This is a based on
            HTTP 1.1 Partial GET, RFC 2616 sec 14.35, but is intended to
            operate on the record level rather than the byte level.  We
            will still respond with code 206 and a range header.
            """

            request_range = None

            if 'HTTP_RANGE' in request.META:
Example #21
0
    cpv_result = [jc_result, js1_result, js2_result, bl_result, bbar_svd1_result, bbar_svd2_result]
    cpv_pull = [root.TH1D(e.GetName()+"_pull","",100,-5,5) for e in cpv_result]
    cpv_params = ["signal_dt_Jc", "signal_dt_Js1", "signal_dt_Js2", "signal_dt_blifetime", "yield_bbar_svd1", "yield_bbar_svd2"]
    cpv_input = [params(e).value for e in cpv_params]

    for parfile in run_jobs(False, True):
        if not os.path.exists(parfile): continue
        params.load(parfile)
        br = params("yield_signal_br").value
        br_error = params("yield_signal_br").error
        br_result.Fill(br)
        br_pull.Fill((br-utils.br)/br_error)

        for i,p in enumerate([params(e) for e in cpv_params]):
            cpv_result[i].Fill(p.value)
            cpv_pull[i].Fill((p.value-cpv_input[i])/p.error)

    a,m,s = draw_toyMC(br_result, r"fit results, input=$%s$" % utils.format_error(utils.br, precision=1), "$\mathcal{B}(\ddk)$", exponent=-3, ylabel=r"Entries / \num{%s}" % br_result.GetBinWidth(1))
    a.set_ylim(0, br_result.GetMaximum()*1.5)
    draw_toyMC(br_pull,"pull distribution", xlabel=r"Pull($\mathcal{B}(\ddk)$)", ylabel="Entries / %s" % br_pull.GetBinWidth(1))

    names = [r"$J_C/J_0$", r"$(2J_{s1}/J_0) \sin(2\phi_1)$", r"$(2J_{s2}/J_0) \cos(2\phi_1)$",r"$\tau / ps$","m1","m2"]

    for i,name in enumerate(names):
        hist = cpv_result[i]
        pull = cpv_pull[i]
        draw_toyMC(hist, r"fit results, input=$%.3g$" % cpv_input[i], xlabel=name, ylabel="Entries / %s" % hist.GetBinWidth(1))
        draw_toyMC(pull, r"pull distribution", xlabel="Pull(%s)" % name, ylabel="Entries / %s" % pull.GetBinWidth(1))

    r2mpl.save_all("toymc/toymc-%s" % toyname, png=False, single_pdf=True)
            lin_result.SetBinError(x,fit.GetParError(1))
            one_pull.Fit(fit,"LQ")
            lin_pull_mean.SetBinContent(x,fit.GetParameter(1))
            lin_pull_mean.SetBinError(x,fit.GetParError(1))
            lin_pull_sigma.SetBinContent(x,fit.GetParameter(2))
            lin_pull_sigma.SetBinError(x,fit.GetParError(2))

        fit = root.TF1("line","pol1",-0.8,0.8)
        for a,h in (a1,lin_result),(a2,lin_pull_mean),(a3,lin_pull_sigma):
            if par is not "yield_signal_br":
                h.Fit(fit,"Q")
            else:
                h.Fit(fit,"Q")
            r2mpl.plot(h, axes=a, errors=True, color=colors[par])
            r2mpl.plot(fit, axes=a, color=colors[par])
            utils.text_box(a, "tl", r"\begin{align*}m&=%s\\t&=%s\end{align*}" % (utils.format_error(fit.GetParameter(1), fit.GetParError(1)), utils.format_error(fit.GetParameter(0), fit.GetParError(0))))

        lin_res = lin_result.Clone("tmp")
        for i in range(1,lin_res.GetNbinsX()+1):
            lin_res.SetBinContent(i,lin_res.GetBinContent(i)-lin_res.GetBinCenter(i))
        r2mpl.plot(lin_res, axes=a1, errors=True, color="k")

        if par is not "yield_signal_br":
            #ymin = result.GetYaxis().GetXmin()
            #ymax = result.GetYaxis().GetXmax()
            a1.set_ylim(-1.2,1.2)
        a2.set_ylim(-1,1)
        a3.set_ylim(0,2)

    elif isinstance(result,root.TH1D):
        toymc.draw_toyMC(result,title)
Example #23
0
        error email to people in `settings.ADMINS`.
      - `FULCRUM_DISPLAY_ERRORS`: Will return a simple traceback
        to the caller, so he can tell you what error they got.
        
     If `FULCRUM_DISPLAY_ERRORS` is not enabled, the caller will
     receive a basic "500 Internal Server Error" message.
     """
     
     exc_type, exc_value, tb = sys.exc_info()
     rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)
     
     if self.email_errors:
         self.email_exception(rep)
     if self.display_errors:
         return HttpResponseServerError(
             format_error('\n'.join(rep.format_exception())))
     else:
         raise
 
 # Return serialized data
 emitter, ct = Emitter.get(em_format)
 srl = emitter(result, recurse_level, typemapper, handler, handler.fields, anonymous)
 
 try:
     """
     Decide whether or not we want a generator here,
     or we just want to buffer up the entire result
     before sending it to the client. Won't matter for
     smaller datasets, but larger will have an impact.
     """
     if self.stream: stream = srl.stream_render(request)
Example #24
0
class Resource(object):
    """
    Resource. Create one for your URL mappings, just
    like you would with Django. Takes one argument,
    the handler. The second argument is optional, and
    is an authentication handler. If not specified,
    `NoAuthentication` will be used by default.
    """
    callmap = { 'GET': 'read', 'POST': 'create', 
                'PUT': 'update', 'DELETE': 'delete' }
    
    def __init__(self, handler, site, name=None, authentication=None, group=None):
        #if not callable(handler):
        #    raise AttributeError, "Handler not callable."
        
        self.handler = handler
        self.site = site
        self.model = self.handler.model
        self.easymodel = EasyModel(self, self.model)
        self.group = group
        
        if name:
            self.name = name.lower()
            self.verbose_name = get_verbose_name(self.name)
            self.verbose_name_plural = string_concat(self.verbose_name, 's')
        else:
            self.name = self.model._meta.model_name
            self.verbose_name = self.model._meta.verbose_name
            self.verbose_name_plural = self.model._meta.verbose_name_plural
        
        self.authentication = authentication
        self.arbitrary = False
        # Erroring
        self.email_errors = getattr(settings, 'FULCRUM_EMAIL_ERRORS', True)
        self.display_errors = getattr(settings, 'FULCRUM_DISPLAY_ERRORS', True)
        self.stream = getattr(settings, 'FULCRUM_STREAM_OUTPUT', False)

    def determine_emitter(self, request, *args, **kwargs):
        """
        Function for determening which emitter to use
        for output. It lives here so you can easily subclass
        `Resource` in order to change how emission is detected.

        You could also check for the `Accept` HTTP header here,
        since that pretty much makes sense. Refer to `Mimer` for
        that as well.
        """
        em = kwargs.pop('emitter_format', None)
        
        if not em:
            em = request.GET.get('format', 'json')

        return em
    
    def get_recurse_level(self, request):
        recurse = int(request.GET.get('recurse', 0))
        if recurse in [0, 1]:
            return recurse
        return 0
    
    @vary_on_headers('Authorization')
    def handle(self, request,*args, **kwargs):
        """
        NB: Sends a `Vary` header so we don't cache requests
        that are different (OAuth stuff in `Authorization` header.)
        """
        
        rm = request.method.upper()

        # Django's internal mechanism doesn't pick up
        # PUT request, so we trick it a little here.
        if rm == "PUT":
            coerce_put_post(request)

        if not self.authentication.is_authenticated(request):
            if hasattr(self.handler, 'anonymous') and \
                callable(self.handler.anonymous) and \
                rm in self.handler.anonymous.allowed_methods:

                handler = self.handler.anonymous()
                anonymous = True
            else:
                return self.authentication.challenge()
        else:
            handler = self.handler
            anonymous = handler.is_anonymous
        
        # Translate nested datastructs into `request.data` here.
        if rm in ('POST', 'PUT'):
            try:
                translate_mime(request)
            except MimerDataException:
                return rc.BAD_REQUEST
        
        if not rm in handler.allowed_methods:
            return HttpResponseNotAllowed(handler.allowed_methods)
        
        meth = getattr(handler, self.callmap.get(rm), None)
                
        if not meth:
            raise Http404

        # Support emitter both through (?P<emitter_format>) and ?format=emitter.
        em_format = self.determine_emitter(request, *args, **kwargs)
        kwargs.pop('emitter_format', None)

        # result is just html, handled in template
        # TODO: move this block into sites.py view handler
        if em_format == 'html':
            temp = get_template('fulcrum/resource_detail.html')
            ctxt = RequestContext(request, { 'resource': self, 'handler': self.handler })
            return HttpResponse(temp.render(ctxt))
        
        # Get recursion level
        recurse_level = self.get_recurse_level(request)
        
        # Clean up the request object a bit, since we might
        # very well have `oauth_`-headers in there, and we
        # don't want to pass these along to the handler.
        request = self.cleanup_request(request)
                
        try:
            # result is either a single object or a list of objects
            # something like... [<Blogpost: Sample test post 2>]
            result = meth(request, *args, **kwargs)
        except FormValidationError, e:
            # TODO: Use rc.BAD_REQUEST here
            return HttpResponse("Bad Request: %s" % e.form.errors, status=400)
        except TypeError, e:
                        
            result = rc.BAD_REQUEST
            hm = HandlerMethod(meth)
            sig = hm.get_signature()

            msg = 'Method signature does not match.\n\n'
            
            if sig:
                msg += 'Signature should be: %s' % sig
            else:
                msg += 'Resource does not expect any parameters.'

            if self.display_errors:                
                msg += '\n\nException was: %s' % str(e)
                
            result.content = format_error(msg)
Example #25
0
class Resource(object):
    """
    Resource. Create one for your URL mappings, just
    like you would with Django. Takes one argument,
    the handler. The second argument is optional, and
    is an authentication handler. If not specified,
    `NoAuthentication` will be used by default.
    """
    callmap = { 'GET': 'read', 'POST': 'create', 
                'PUT': 'update', 'DELETE': 'delete' }
    
    def __init__(self, handler, authentication=None):
        if not callable(handler):
            raise AttributeError, "Handler not callable."
        
        self.handler = handler()
        
        if not authentication:
            self.authentication = NoAuthentication()
        else:
            self.authentication = authentication
            
        # Erroring
        self.email_errors = getattr(settings, 'PISTON_EMAIL_ERRORS', True)
        self.display_errors = getattr(settings, 'PISTON_DISPLAY_ERRORS', True)
        self.stream = getattr(settings, 'PISTON_STREAM_OUTPUT', False)

    def determine_emitter(self, request, *args, **kwargs):
        """
        Function for determening which emitter to use
        for output. It lives here so you can easily subclass
        `Resource` in order to change how emission is detected.

        You could also check for the `Accept` HTTP header here,
        since that pretty much makes sense. Refer to `Mimer` for
        that as well.
        """
        em = kwargs.pop('emitter_format', None)
        
        if not em:
            em = request.GET.get('format', 'json')

        return em
    
    @vary_on_headers('Authorization')
    def __call__(self, request, *args, **kwargs):
        """
        NB: Sends a `Vary` header so we don't cache requests
        that are different (OAuth stuff in `Authorization` header.)
        """
        rm = request.method.upper()

        # Django's internal mechanism doesn't pick up
        # PUT request, so we trick it a little here.
        if rm == "PUT":
            coerce_put_post(request)

        if not self.authentication.is_authenticated(request):
            if hasattr(self.handler, 'anonymous') and \
                callable(self.handler.anonymous) and \
                rm in self.handler.anonymous.allowed_methods:

                handler = self.handler.anonymous()
                anonymous = True
            else:
                return self.authentication.challenge()
        else:
            handler = self.handler
            anonymous = handler.is_anonymous
        
        # Translate nested datastructs into `request.data` here.
        if rm in ('POST', 'PUT'):
            try:
                translate_mime(request)
            except MimerDataException:
                return rc.BAD_REQUEST
        
        if not rm in handler.allowed_methods:
            return HttpResponseNotAllowed(handler.allowed_methods)
        
        meth = getattr(handler, self.callmap.get(rm), None)
        
        if not meth:
            raise Http404

        # Support emitter both through (?P<emitter_format>) and ?format=emitter.
        em_format = self.determine_emitter(request, *args, **kwargs)

        kwargs.pop('emitter_format', None)
        
        # Clean up the request object a bit, since we might
        # very well have `oauth_`-headers in there, and we
        # don't want to pass these along to the handler.
        request = self.cleanup_request(request)
        
        try:
            result = meth(request, *args, **kwargs)
        except FormValidationError, e:
            # TODO: Use rc.BAD_REQUEST here
            return HttpResponse("Bad Request: %s" % e.form.errors, status=400)
        except TypeError, e:
            result = rc.BAD_REQUEST
            hm = HandlerMethod(meth)
            sig = hm.get_signature()

            msg = 'Method signature does not match.\n\n'
            
            if sig:
                msg += 'Signature should be: %s' % sig
            else:
                msg += 'Resource does not expect any parameters.'

            if self.display_errors:                
                msg += '\n\nException was: %s' % str(e)
                
            result.content = format_error(msg)
Example #26
0
print r"""
\def\BMultiplicity{%.1f}
\def\FractionCorrectRec{\SI{%.1f}{\%%}}
\def\FractionCorrectBestB{\SI{%.1f}{\%%}}
\def\ReconstructedBR{\num{%.3e}}
\def\RawReconstructionEffSVDOne{%s}
\def\RawReconstructionEffSVDTwo{%s}
\def\ReconstructionEffSVDOne{%s}
\def\ReconstructionEffSVDTwo{%s}
\def\ReconstructionEffSVDOneCorr{%s}
\def\ReconstructionEffSVDTwoCorr{%s}""" % (
    b_mult,
    100*ncorrect[1] / (ncorrect[0]+ncorrect[1]),
    100*bestB[1] / (bestB[0]+bestB[1]),
    eff_DDKs,
    utils.format_error(*raw_eff[0], exponent=-3),
    utils.format_error(*raw_eff[1], exponent=-3),
    utils.format_error(*rec_eff[0], exponent=-5),
    utils.format_error(*rec_eff[1], exponent=-5),
    utils.format_error(*rec_eff_c[0], exponent=-5),
    utils.format_error(*rec_eff_c[1], exponent=-5),
)

print r"""
\def\BMultiplicityCtrl{%.1f}
\def\FractionCorrectRecCtrl{\SI{%.1f}{\%%}}
\def\FractionCorrectBestBCtrl{\SI{%.1f}{\%%}}
\def\ReconstructedBRCtrl{\num{%.3e}}
\def\RawReconstructionEffSVDOneCtrl{%s}
\def\RawReconstructionEffSVDTwoCtrl{%s}
\def\ReconstructionEffSVDOneCtrl{%s}
Example #27
0
        except TypeError, e:
            result = rc.BAD_REQUEST
            hm = HandlerMethod(meth)
            sig = hm.get_signature()

            msg = 'Method signature does not match.\n\n'
            
            if sig:
                msg += 'Signature should be: %s' % sig
            else:
                msg += 'Resource does not expect any parameters.'

            if self.display_errors:                
                msg += '\n\nException was: %s' % str(e)
                
            result.content = format_error(msg)
        except HttpStatusCode, e:
            #result = e ## why is this being passed on and not just dealt with now?
            return e.response
        except Exception, e:
            """
            On errors (like code errors), we'd like to be able to
            give crash reports to both admins and also the calling
            user. There's two setting parameters for this:
            
            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback
               to the caller, so he can tell you what error they got.
               
Example #28
0
	def __init__(self,uri,mode,config=None,save_metadata=True,keys=None):
		""" Initializes the file object.
		- uri is a URI or str object with the address of the file.
		- mode is the mode of the file. Currently, just 'w' and 'r'
		- keys are a pair (Kf, Kd) to use. If None, use the pair in the
		configuration. Kf is used to crypt/decrypt the file and Kd to
		secure the resource localization
		- config is the configuration to use. In None, use the default 
		configuration
		- keys is an array of identifiers for the keys used in the file: in None,
		it uses KEY_NAMES (see extended documentation)
		- If save_metadata is True, the file descriptor is saved in
		the DHT. You can access to the file descriptor with
		File.metadata """
		
		if not config: config=dfs.default_config
		self.config=config
		if not keys: keys=KEY_NAMES
		self.keys=[]
		for k in keys:
			self.keys.append(self.config.get_key(k))

		if type(uri)==str: uri=uri_from_string(uri,config=config,kd=self.keys[0])

		logger.debug("Accesing file %s (%s)"%(uri, mode))
		
		self.uri=uri
		self.mode = mode
		self.closed = True
		
		self.buffer=[]
		# The maximum block size
		self.BLOCK_SIZE=self.config.getint('File:block',dfs.dht.BLOCK_SIZE)
		# The file descriptor could be bigger than the block size. To
		# prevent this, the metadata is chained in several blocks
		# Use this parameter to control how many part references
		# a block of metadata holds.
		self.DESC_PER_METAPART=self.config.getint('File:descPerMetapart',12)
		# The max length of the internal buffer before an automatic flush()
		self.MAX_BUFFER=self.config.getint('File:maxbuffer',4096)
		self.save_metadata=save_metadata
		
		if mode=='r':
			# crypter used to decrypt the metadata. There is always
			# a crypter to protect against casual atackers, but
			# if there is no Kff the crypter is nearly useless
			if SECURED:
				if self.keys and self.keys[0]:
					mdencrypter=AES.new(self.keys[4],AES.MODE_CBC,self.uri.get_hd())
				else:
					mdencrypter=AES.new(self.uri.get_hd(),AES.MODE_CBC,self.uri.get_hd())
			else:
				mdencrypter=DummyEncrypter()
			# get the metadata from the DHT
			md=dfs.dht.get(uri.get_hd(),uri.nick)
			if not md: raise IOError('No reference to that file: ' + uri.get_static())
			self.metadata=utils.Config()
			md=mdencrypter.decrypt(md)
			try:
				self.metadata.load(md)
			except:
				raise IOError('The reference is not metatada: %s'%utils.format_error())
			# get basic information from the metadata
			self.uri.uid=self.metadata.get('Main:UID')
			self.uri.nick=self.metadata.get('Main:nick')
			np=self.metadata.getint('Main:parts', 0)
			self.filelength=self.metadata.getint('Main:length')
			# get info about each one of the parts
			self.parts=[]
			cmd=self.metadata
			for next_part in range(0,np):
				self.parts.append(cmd.get("Part:%d"%next_part))
				# load chained metadata
				if next_part<np-1 and next_part%self.DESC_PER_METAPART==self.DESC_PER_METAPART-1:
					nuri=uri_from_string(cmd.get('Main:n'))
					md=dfs.dht.get(nuri.get_hd(),nuri.nick)
					if not md: raise IOError('No reference to %s (%d)'%(nuri.get_static(),next_part))
					md=mdencrypter.decrypt(md)
					cmd=utils.Config()
					try:
						cmd.load(md)
					except:
						raise IOError('The reference is not metadata: %s'%utils.format_error())
					
			self.eof=bool(len(self.parts)==0)
		elif mode=='w':
			if not self.uri.uid: self.uri.uid=dfs.default_config.get('Main:UID')
			if not self.uri.nick:
				self.uri.nick=dfs.default_config.get('Main:nick')
				#if not self.uri.nick: self.uri.nick=utils.random_nick()
			self.metadata=utils.Config()
			self.metadata.set('Main:UID',self.uri.uid)
			if self.uri.nick:
				self.metadata.set('Main:nick',self.uri.nick)
			self.parts=[]
			self.filelength=0
		else:
			raise IOError,'Mode not supported: %s'%mode
		
		s='File %s in mode "%s" '%(self.uri.get_readable(),self.mode)
		for k in self.keys:
			if k:
				s+='1'
			else:
				s+='0'
		logger.info(s)
		
		# Create hasher and crypter
		self.hasher=get_new_hasher()
		if self.keys[1] and SECURED:
			# The crypter is AES in CBC mode, with IV=Hd of the file
			self.crypter=AES.new(self.keys[1],AES.MODE_CBC,self.uri.get_hd())
		else:
			self.crypter=None
		
		logger.info('Opening %s in mode=%s'%(self.uri.get_readable(),self.mode))
		self.closed = False
Example #29
0
    def error_handler(self, response, e, request, meth):
        """
        Override this method to add handling of errors customized for your
        needs
        """
        response.status_code = 500
        if isinstance(e, PermissionDenied):
            response.error_message = e.message or "Permission Denied"
            response.status_code = 403
        elif isinstance(
                e, (PistonException, PistonBadRequestException,
                    PistonForbiddenException, PistonMethodException,
                    PistonNotFoundException, PistonUnauthorizedException)):
            response.status_code = e.status_code
            response.error_message = e.message
            response.headers.update(e.headers)
        elif isinstance(e, FormValidationError):
            response.status_code = 400
            response.form_errors = e.form.errors
        elif isinstance(e, TypeError) and meth:
            hm = HandlerMethod(meth)
            sig = hm.signature

            msg = 'Method signature does not match.\n\n'

            if sig:
                msg += 'Signature should be: %s' % sig
            else:
                msg += 'Resource does not expect any parameters.'

            if self.display_errors:
                msg += '\n\nException was: %s' % str(e)

            response.error_message = format_error(msg)
        # TODO: As we start using Piston exceptions, the following 2 errors can be phased out
        elif isinstance(e, Http404):
            response.status_code = 404
            response.error_message = 'Not Found'
        elif isinstance(e, ValidationError):
            response.status_code = 400
            if hasattr(e, "message_dict"):
                response.error_message = "\n".join(
                    "%s: %s" % (k[0], k[1][0])
                    for k in e.message_dict.iteritems())
            else:
                response.error_message = " ".join(e.messages)
        elif isinstance(e, ObjectDoesNotExist):
            response.status_code = 404
            if hasattr(e, "message"):
                response.error_message = e.message
            else:
                response.error_message = 'Not Found'

        elif isinstance(e, HttpStatusCode):
            response.error_message = e.response
        else:
            """
            On errors (like code errors), we'd like to be able to
            give crash reports to both admins and also the calling
            user. There's two setting parameters for this:

            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple message/full traceback
               depending on `PISTON_DISPLAY_TRACEBACK`. Default is simple message
               to the caller, so he can tell you what error they got.

            If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will
            receive a basic "500 Internal Server Error" message.
            """
            exc_type, exc_value, tb = sys.exc_info()
            rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)
            if self.email_errors:
                self.email_exception(rep, e, request)
            if self.display_errors:
                if self.display_traceback:
                    response.error_message = format_error('\n'.join(
                        rep.format_exception()))
                else:
                    response.error_message = str(e)
            else:
                raise
Example #30
0
    def error_handler(self, response, e, request, meth):
        """
        Override this method to add handling of errors customized for your
        needs
        """
        response.status_code = 500
        if isinstance(e, (PistonException, PistonBadRequestException, PistonForbiddenException, PistonMethodException, PistonNotFoundException, PistonUnauthorizedException)):
            response.status_code = e.status_code
            response.error_message = e.message
            response.headers.update(e.headers)
        elif isinstance(e, FormValidationError):
            response.status_code = 400
            response.form_errors = e.form.errors
        elif isinstance(e, TypeError) and meth:
            hm = HandlerMethod(meth)
            sig = hm.signature

            msg = 'Method signature does not match.\n\n'

            if sig:
                msg += 'Signature should be: %s' % sig
            else:
                msg += 'Resource does not expect any parameters.'

            if self.display_errors:
                msg += '\n\nException was: %s' % str(e)

            response.error_message = format_error(msg)
        # TODO: As we start using Piston exceptions, the following 2 errors can be phased out
        elif isinstance(e, Http404):
            response.status_code = 404
            response.error_message = 'Not Found'
        elif isinstance(e, HttpStatusCode):
            response.error_message = e.response
        else:
            """
            On errors (like code errors), we'd like to be able to
            give crash reports to both admins and also the calling
            user. There's two setting parameters for this:

            Parameters::
             - `PISTON_EMAIL_ERRORS`: Will send a Django formatted
               error email to people in `settings.ADMINS`.
             - `PISTON_DISPLAY_ERRORS`: Will return a simple message/full traceback
               depending on `PISTON_DISPLAY_TRACEBACK`. Default is simple message
               to the caller, so he can tell you what error they got.

            If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will
            receive a basic "500 Internal Server Error" message.
            """
            exc_type, exc_value, tb = sys.exc_info()
            rep = ExceptionReporter(request, exc_type, exc_value, tb.tb_next)
            if self.email_errors:
                self.email_exception(rep)
            if self.display_errors:
                if self.display_traceback:
                    response.error_message = format_error('\n'.join(rep.format_exception()))
                else:
                    response.error_message = str(e)
            else:
                raise
Example #31
0
class Resource(object):
    """
    Resource. Create one for your URL mappings, just
    like you would with Django. Takes one argument,
    the handler. The second argument is optional, and
    is an authentication handler. If not specified,
    `NoAuthentication` will be used by default.
    """
    callmap = { 'GET': 'read', 'POST': 'create',
                'PUT': 'update', 'DELETE': 'delete' }

    def __init__(self, handler, authentication=None):
        if not callable(handler):
            raise AttributeError, "Handler not callable."

        self.handler = handler()

        if not authentication:
            self.authentication = (NoAuthentication(),)
        elif isinstance(authentication, (list, tuple)):
            self.authentication = authentication
        else:
            self.authentication = (authentication,)

        # Erroring
        self.email_errors = getattr(settings, 'PISTON_EMAIL_ERRORS', True)
        self.display_errors = getattr(settings, 'PISTON_DISPLAY_ERRORS', True)
        self.stream = getattr(settings, 'PISTON_STREAM_OUTPUT', False)

    def determine_emitter(self, request, *args, **kwargs):
        """
        Function for determening which emitter to use
        for output. It lives here so you can easily subclass
        `Resource` in order to change how emission is detected.

        You could also check for the `Accept` HTTP header here,
        since that pretty much makes sense. Refer to `Mimer` for
        that as well.
        """
        em = kwargs.pop('emitter_format', None)

        if not em:
            em = request.GET.get('format', 'json')

        return em

    def form_validation_response(self, e):
        """
        Method to return form validation error information. 
        You will probably want to override this in your own
        `Resource` subclass.
        """
        resp = rc.BAD_REQUEST
        resp.write(' '+str(e.form.errors))
        return resp

    @property
    def anonymous(self):
        """
        Gets the anonymous handler. Also tries to grab a class
        if the `anonymous` value is a string, so that we can define
        anonymous handlers that aren't defined yet (like, when
        you're subclassing your basehandler into an anonymous one.)
        """
        if hasattr(self.handler, 'anonymous'):
            anon = self.handler.anonymous

            if callable(anon):
                return anon

            for klass in typemapper.keys():
                if anon == klass.__name__:
                    return klass

        return None

    def authenticate(self, request, rm):
        actor, anonymous = False, True

        for authenticator in self.authentication:
            if not authenticator.is_authenticated(request):
                if self.anonymous and \
                    rm in self.anonymous.allowed_methods:

                    actor, anonymous = self.anonymous(), True
                else:
                    actor, anonymous = authenticator.challenge, CHALLENGE
            else:
                return self.handler, self.handler.is_anonymous

        return actor, anonymous

    @vary_on_headers('Authorization')
    def __call__(self, request, *args, **kwargs):
        """
        NB: Sends a `Vary` header so we don't cache requests
        that are different (OAuth stuff in `Authorization` header.)
        """
        rm = request.method.upper()

        # Django's internal mechanism doesn't pick up
        # PUT request, so we trick it a little here.
        if rm == "PUT":
            coerce_put_post(request)

        actor, anonymous = self.authenticate(request, rm)

        if anonymous is CHALLENGE:
            return actor()
        else:
            handler = actor

        # Translate nested datastructs into `request.data` here.
        if rm in ('POST', 'PUT'):
            try:
                translate_mime(request)
            except MimerDataException:
                return rc.BAD_REQUEST
            if not hasattr(request, 'data'):
                if rm == 'POST':
                    request.data = request.POST
                else:
                    request.data = request.PUT

        if not rm in handler.allowed_methods:
            return HttpResponseNotAllowed(handler.allowed_methods)

        meth = getattr(handler, self.callmap.get(rm), None)

        if not meth:
            raise Http404

        # Support emitter both through (?P<emitter_format>) and ?format=emitter.
        em_format = self.determine_emitter(request, *args, **kwargs)

        kwargs.pop('emitter_format', None)

        # Clean up the request object a bit, since we might
        # very well have `oauth_`-headers in there, and we
        # don't want to pass these along to the handler.
        request = self.cleanup_request(request)

        try:
            result = meth(request, *args, **kwargs)
        except FormValidationError, e:
            return self.form_validation_response(e)
        except TypeError, e:
            result = rc.BAD_REQUEST
            hm = HandlerMethod(meth)
            sig = hm.signature

            msg = 'Method signature does not match.\n\n'

            if sig:
                msg += 'Signature should be: %s' % sig
            else:
                msg += 'Resource does not expect any parameters.'

            if self.display_errors:
                msg += '\n\nException was: %s' % str(e)

            result.content = format_error(msg)