Example #1
0
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    def __init__(self, request, client_address, server):
        self.router = Router(self)
        for key, value in ROUTES.items():
            self.router.add(key, value)
        BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request,
                                                       client_address, server)

    def do_HEAD(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        if self.path.startswith('/'):
            self.path = self.path[1:]

        routing_info = self.router.route(self.path)
        if routing_info:
            func, regex_match = routing_info
            content = func(**regex_match)

            self.do_HEAD()
            self.wfile.write(content)

        else:
            self.send_error(404)

    def do_POST(self):
        form = cgi.FieldStorage(fp=self.rfile,
                                headers=self.headers,
                                environ={
                                    'REQUEST_METHOD': 'POST',
                                    'CONTENT_TYPE':
                                    self.headers['Content-Type'],
                                })

        # Begin the response
        self.send_response(200)
        self.end_headers()
        self.wfile.write('Client: %s\n' % str(self.client_address))
        self.wfile.write('User-agent: %s\n' % str(self.headers['user-agent']))
        self.wfile.write('Path: %s\n' % self.path)
        self.wfile.write('Form data:\n')

        # Echo back information about what was posted in the form
        for field in form.keys():
            field_item = form[field]
            if field_item.filename:
                # The field contains an uploaded file
                file_data = field_item.file.read()
                file_len = len(file_data)
                del file_data
                self.wfile.write('\tUploaded %s as "%s" (%d bytes)\n' % \
                                (field, field_item.filename, file_len))
            else:
                # Regular form value
                self.wfile.write('\t%s=%s\n' % (field, form[field].value))
        return
Example #2
0
File: server.py Project: vhf/glass
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    def __init__(self, request, client_address, server):
        self.router = Router(self)
        for key, value in ROUTES.items():
            self.router.add(key, value)
        BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server)

    def do_HEAD(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        if self.path.startswith('/'):
            self.path = self.path[1:]

        routing_info = self.router.route(self.path)
        if routing_info:
            func, regex_match = routing_info
            content = func(**regex_match)

            self.do_HEAD()
            self.wfile.write(content)

        else:
            self.send_error(404)

    def do_POST(self):
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST',
                     'CONTENT_TYPE':self.headers['Content-Type'],
                     })

        # Begin the response
        self.send_response(200)
        self.end_headers()
        self.wfile.write('Client: %s\n' % str(self.client_address))
        self.wfile.write('User-agent: %s\n' % str(self.headers['user-agent']))
        self.wfile.write('Path: %s\n' % self.path)
        self.wfile.write('Form data:\n')

        # Echo back information about what was posted in the form
        for field in form.keys():
            field_item = form[field]
            if field_item.filename:
                # The field contains an uploaded file
                file_data = field_item.file.read()
                file_len = len(file_data)
                del file_data
                self.wfile.write('\tUploaded %s as "%s" (%d bytes)\n' % \
                                (field, field_item.filename, file_len))
            else:
                # Regular form value
                self.wfile.write('\t%s=%s\n' % (field, form[field].value))
        return
Example #3
0
class Brick(object):
    def __init__(self, catchall=True, autojson=True, config=None):
        self.routes = Router()
        self._logger = None
        self.mounts = {}
        self.error_handler = {}
        self.catchall = catchall
        self.config = config or {}
        self.serve = True
        self.castfilter = []
        if autojson and dumps:
            self.add_filter(dict, dumps)

    def optimize(self, *a, **ka):
        depr("Brick.optimize() is obsolete.")

    def mount(self, app, script_path):
        if not isinstance(app, Brick):
            raise TypeError('Only Brick instances are supported for now.')
        script_path = '/'.join(filter(None, script_path.split('/')))
        path_depth = script_path.count('/') + 1
        if not script_path:
            raise TypeError('Empty script_path. Perhaps you want a merge()?')
        for other in self.mounts:
            if other.startswith(script_path):
                raise TypeError('Conflict with existing mount: %s' % other)

        @self.route('/%s/:#.*#' % script_path, method="ANY")
        def mountpoint():
            request.path_shift(path_depth)
            return app.handle(request.path, request.method)

        self.mounts[script_path] = app

    def add_filter(self, ftype, func):
        if not isinstance(ftype, type):
            raise TypeError("Expected type object, got %s" % type(ftype))
        self.castfilter = [(t, f) for (t, f) in self.castfilter if t != ftype]
        self.castfilter.append((ftype, func))
        self.castfilter.sort()

    def match_url(self, path, method='GET'):
        path, method = path.strip().lstrip('/'), method.upper()
        callbacks, args = self.routes.match(path)
        if not callbacks:
            raise HTTPError(404, "Not found: " + path)
        if method in callbacks:
            return callbacks[method], args
        if method == 'HEAD' and 'GET' in callbacks:
            return callbacks['GET'], args
        if 'ANY' in callbacks:
            return callbacks['ANY'], args
        allow = [m for m in callbacks if m != 'ANY']
        if 'GET' in allow and 'HEAD' not in allow:
            allow.append('HEAD')
        raise HTTPError(405,
                        "Method not allowed.",
                        header=[('Allow', ",".join(allow))])

    def get_url(self, routename, **kargs):
        scriptname = request.environ.get('SCRIPT_NAME', '').strip('/') + '/'
        location = self.routes.build(routename, **kargs).lstrip('/')
        return urljoin(urljoin('/', scriptname), location)

    def route(self, path=None, method='GET', **kargs):
        def wrapper(callback):
            routes = [path] if path else yieldroutes(callback)
            methods = method.split(';') if isinstance(method, str) else method
            for r in routes:
                for m in methods:
                    r, m = r.strip().lstrip('/'), m.strip().upper()
                    old = self.routes.get_route(r, **kargs)
                    if old:
                        old.target[m] = callback
                    else:
                        self.routes.add(r, {m: callback}, **kargs)
                        self.routes.compile()
            return callback

        return wrapper

    def get(self, path=None, method='GET', **kargs):
        return self.route(path, method, **kargs)

    def post(self, path=None, method='POST', **kargs):
        return self.route(path, method, **kargs)

    def put(self, path=None, method='PUT', **kargs):
        return self.route(path, method, **kargs)

    def delete(self, path=None, method='DELETE', **kargs):
        return self.route(path, method, **kargs)

    def error(self, code=500):
        def wrapper(handler):
            self.error_handler[int(code)] = handler
            return handler

        return wrapper

    def handle(self, url, method):
        if not self.serve:
            return HTTPError(503, "Server stopped")
        try:
            handler, args = self.match_url(url, method)
            return handler(**args)
        except HTTPResponse, e:
            return e
        except Exception, e:
            if isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError))\
            or not self.catchall:
                raise
            print e
            return HTTPError(500, 'Unhandled exception', e, format_exc(10))
Example #4
0
class Brick(object):

    def __init__(self, catchall=True, autojson=True, config=None):
        self.routes = Router()
        self._logger=None
        self.mounts = {}
        self.error_handler = {}
        self.catchall = catchall
        self.config = config or {}
        self.serve = True
        self.castfilter = []
        if autojson and dumps:
            self.add_filter(dict, dumps)

    def optimize(self, *a, **ka):
        depr("Brick.optimize() is obsolete.")

    def mount(self, app, script_path):
        if not isinstance(app, Brick):
            raise TypeError('Only Brick instances are supported for now.')
        script_path = '/'.join(filter(None, script_path.split('/')))
        path_depth = script_path.count('/') + 1
        if not script_path:
            raise TypeError('Empty script_path. Perhaps you want a merge()?')
        for other in self.mounts:
            if other.startswith(script_path):
                raise TypeError('Conflict with existing mount: %s' % other)
        @self.route('/%s/:#.*#' % script_path, method="ANY")
        def mountpoint():
            request.path_shift(path_depth)
            return app.handle(request.path, request.method)
        self.mounts[script_path] = app

    def add_filter(self, ftype, func):
        if not isinstance(ftype, type):
            raise TypeError("Expected type object, got %s" % type(ftype))
        self.castfilter = [(t, f) for (t, f) in self.castfilter if t != ftype]
        self.castfilter.append((ftype, func))
        self.castfilter.sort()

    def match_url(self, path, method='GET'):
        path, method = path.strip().lstrip('/'), method.upper()
        callbacks, args = self.routes.match(path)
        if not callbacks:
            raise HTTPError(404, "Not found: " + path)
        if method in callbacks:
            return callbacks[method], args
        if method == 'HEAD' and 'GET' in callbacks:
            return callbacks['GET'], args
        if 'ANY' in callbacks:
            return callbacks['ANY'], args
        allow = [m for m in callbacks if m != 'ANY']
        if 'GET' in allow and 'HEAD' not in allow:
            allow.append('HEAD')
        raise HTTPError(405, "Method not allowed.",
                        header=[('Allow',",".join(allow))])

    def get_url(self, routename, **kargs):
        scriptname = request.environ.get('SCRIPT_NAME', '').strip('/') + '/'
        location = self.routes.build(routename, **kargs).lstrip('/')
        return urljoin(urljoin('/', scriptname), location)

    def route(self, path=None, method='GET', **kargs):
        def wrapper(callback):
            routes = [path] if path else yieldroutes(callback)
            methods = method.split(';') if isinstance(method, str) else method
            for r in routes:
                for m in methods:
                    r, m = r.strip().lstrip('/'), m.strip().upper()
                    old = self.routes.get_route(r, **kargs)
                    if old:
                        old.target[m] = callback
                    else:
                        self.routes.add(r, {m: callback}, **kargs)
                        self.routes.compile()
            return callback
        return wrapper

    def get(self, path=None, method='GET', **kargs):
        return self.route(path, method, **kargs)

    def post(self, path=None, method='POST', **kargs):
        return self.route(path, method, **kargs)

    def put(self, path=None, method='PUT', **kargs):
        return self.route(path, method, **kargs)

    def delete(self, path=None, method='DELETE', **kargs):
        return self.route(path, method, **kargs)

    def error(self, code=500):
        def wrapper(handler):
            self.error_handler[int(code)] = handler
            return handler
        return wrapper

    def handle(self, url, method):
        if not self.serve:
            return HTTPError(503, "Server stopped")
        try:
            handler, args = self.match_url(url, method)
            return handler(**args)
        except HTTPResponse, e:
            return e
        except Exception, e:
            if isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError))\
            or not self.catchall:
                raise
            print e
            return HTTPError(500, 'Unhandled exception', e, format_exc(10))
Example #5
0
from routes import *
from utilities import getDiscordClient, startsWithAny, replaceAnyFront, cleanUpDBConnection
from command import Command
from gpt3 import getResponse

if not isProduction():
    import dotenv
    dotenv.load_dotenv()

BOT_TOKEN = os.environ.get('BOT_TOKEN')
openai.api_key = os.environ.get('OPENAI_API_KEY')

client = getDiscordClient()
router = Router()

router.add(Command('help', commands, "Show help for commands."))
router.add(Command('hello', hello, "It's only fair to reciprocate!"))

router.add(Command('pony', pony, ""))

router.add(Command('i love twilight', iLoveTwilight, ""))

router.add(Command('emergency raritwi', emergencyRaritwi, ""))
router.add(Command('emergency rarity', emergencyRarity, ""))
router.add(Command('emergency twilight', emergencyTwilight, ""))

router.add(Command('what do you think', whatDoYouThink, ""))
router.add(Command('do you agree', whatDoYouThink, ""))
router.add(Command('rate this', rateThis, ""))
router.add(Command('tell me a joke', tellMeAJoke, ""))
router.add(Command('tell me another joke', tellMeAJoke, ""))