Commit d109b936 authored by A. Wilcox's avatar A. Wilcox 🦊
Browse files

Trying my best to make this not suck so bad

parent 2cc35ce9
"""The core of Emily."""
import asyncio
from collections import defaultdict
in_plugins = []
in_proto_plugins = defaultdict(list)
in_protocols = set()
in_proto_insts = []
out_plugins = []
......@@ -12,9 +14,13 @@ event_loop = asyncio.get_event_loop()
def register_in(plugin):
inst = plugin()
for protocol in plugin.PROTOCOLS:
in_protocols.add(protocol)
in_plugins.append(plugin())
in_proto_insts.append(protocol(event_loop))
in_plugins.append(inst)
def register_out(plugin):
......@@ -22,14 +28,11 @@ def register_out(plugin):
def run():
for proto in in_protocols:
in_proto_insts.append(proto(event_loop))
coros = []
for proto in in_proto_insts:
coros += proto.coros()
for out in out_plugins:
coros += out.coros()
asyncio.gather(coros, loop=event_loop)
asyncio.gather(*coros, loop=event_loop)
event_loop.run_forever()
from emily.protocols import http
from emily.protocols.http import http_handle, HTTPProtocol
import json
class GitLab:
""" Processes information from GitLab Web Hooks. """
PROTOCOLS = (http,)
PROTOCOLS = (HTTPProtocol,)
def __init__(self):
pass
http_handle.add(self.http_handle)
def http_can_handle(headers, body):
return 'X-Gitlab-Event' in headers
def http_handle(self, request):
if 'X-Gitlab-Event' not in request.headers:
return False
def http_handle(headers, body):
pass
return True
......@@ -70,7 +70,7 @@ class IRC:
self.network_instances[network] = netinst
def coros(self):
return [inst.connect for inst in self.network_instances.values()]
return [inst.connect() for inst in self.network_instances.values()]
def push(self, project, push):
pass
\ No newline at end of file
"""Basic protocol handling for Emily."""
from emily.protocols.http import HTTPProtocol as http
\ No newline at end of file
from emily.protocols.http import HTTPProtocol
__all__ = [HTTPProtocol]
from aiohttp import web
import asyncio
from taillight import Signal
from emily import config
http_handle = Signal(('http', 'handle'))
class HTTPProtocol():
def __init__(self, loop):
if 'http' not in config:
......@@ -13,10 +17,10 @@ class HTTPProtocol():
# listen on port
self.app = web.Application(loop=loop)
self.app.router.add_route('GET', '/{project}', self.get_handler)
self.app.router.add_route('POST', '/{project}', self.post_handler)
self.app.router.add_route('GET', '/{grp}/{project}', self.get_handler)
self.app.router.add_route('POST', '/{grp}/{project}', self.post_handler)
self.app.router.add_route('GET', '/{project}', self.handler)
self.app.router.add_route('POST', '/{project}', self.handler)
self.app.router.add_route('GET', '/{grp}/{project}', self.handler)
self.app.router.add_route('POST', '/{grp}/{project}', self.handler)
self.server = loop.create_server(self.app.make_handler(),
'0.0.0.0', int(config['http']['port']))
......@@ -24,10 +28,11 @@ class HTTPProtocol():
def coros(self):
return [self.server]
def get_handler(self, request):
"""Handle GET."""
print(request)
@asyncio.coroutine
def handler(self, request):
"""Handle requests."""
responses = yield from http_handle.call_async(request)
def post_handler(self, request):
"""Handle POST."""
print(request)
if any(responses):
return web.Response(body=b'Okay')
return web.Response(body=b'Not handled')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment