#!/usr/bin/env python
# encoding: utf-8

import logging, urlparse

class ReqHBase(object):
    @classmethod
    def dispatch(cls, req):
        result = -1
        targetf = 'do_%s' % urlparse.urlsplit(req)[2].split('/')[-1]

        try: handler = getattr(cls, targetf)
        except AttributeError:
            logging.error("no '%s' function in '%s'" % (targetf, str(cls)))
        else: # is it a function?
            if callable(handler):
                try: result = handler(req)
                except Exception, e: logging.exception(str(e))
            else:
                logging.error("'%s' not callable in '%s'" % (targetf, str(cls)))
        return(result)
