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

lol I have no idea how to use asyncio

parent 2ff450f2
from configparser import ConfigParser
from os.path import expanduser
config = ConfigParser()
config.read(['emily.cfg', expanduser('~/emily.cfg')])
\ No newline at end of file
"""The core of Emily."""
import asyncio
in_plugins = []
in_protocols = set()
in_proto_insts = []
out_plugins = []
event_loop = asyncio.get_event_loop()
def register_in(plugin):
for protocol in plugin.PROTOCOLS:
in_protocols.add(protocol)
in_plugins.append(plugin())
def register_out(plugin):
out_plugins.append(plugin(event_loop))
def run():
for proto in in_protocols:
in_proto_insts.append(proto(event_loop))
event_loop.run_forever()
from emily.protocols import http
import json
class GitLab:
""" Processes information from GitLab Web Hooks. """
PROTOCOLS = (http,)
def __init__(self):
pass
def http_can_handle(headers, body):
return 'X-Gitlab-Event' in headers
def http_handle(headers, body):
pass
from collections import defaultdict
from PyIRC.extensions import bot_recommended
from PyIRC.io.asyncio import IRCProtocol
from PyIRC.signal import event
from emily import config
class IRCNetwork(IRCProtocol):
"""The basic IRC network representation. Each network has an instance."""
def __init__(self, *args, **kwargs):
super(IRCNetwork, self).__init__(*args, **kwargs)
@event('commands', 'NOTICE')
def on_message(self, caller, line):
if line.params[0] == self.nick and line.params[-1].upper() == 'REHASH':
config.reload()
class IRC:
"""The IRC output plugin.
Config:
networks = comma separated list of networks. For each network:
network_server = IP or hostname
network_port = IRC port
network_ssl = True/False
network_nick = Nickname to use
network_gecos = GECOS
network_username = Username to auth with (or not present)
network_password = Password to auth with (or not present)
network_channels = comma separated list of channels. For each:
network_channel_projects = comma separated list of projects.
"""
def __init__(self, loop):
myconf = config['irc']
self.networks = [network.strip()
for network in myconf['networks'].split(',')]
self.network_instances = {}
self.projects = defaultdict(list)
for network in self.networks:
args = {
'serverport': (myconf[network + '_server'],
int(myconf[network + '_port'])),
'ssl': myconf[network + '_ssl'].lower() == 'true',
'nick': myconf[network + '_nick'],
'gecos': myconf[network + '_gecos'],
'extensions': bot_recommended,
'join': [],
}
args['username'] = myconf.get(network + '_username',
myconf[network + '_nick'])
if network + '_username' in myconf:
args['sasl_username'] = myconf[network + '_username']
args['sasl_password'] = myconf[network + '_password']
for channel in [ch.strip()
for ch in myconf[network + '_channels'].split(',')]:
args['join'].append('#' + channel)
proj_setting = network + '_' + channel + '_projects'
for project in [proj.strip()
for proj in myconf[proj_setting]]:
self.projects[project].append('{}:{}'.format(network,
'#' + channel))
netinst = IRCNetwork(**args)
self.network_instances[network] = netinst
coro = netinst.connect()
loop.call_soon(coro)
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 aiohttp import web
from emily import config
class HTTPProtocol():
def __init__(self, loop):
if 'http' not in config:
config['http'] = {'port': 8085, 'ssl': False}
if config['http'].get('ssl', False):
# handle SSL
pass
# 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.server = loop.create_server(self.app.make_handler(),
'0.0.0.0', int(config['http']['port']))
loop.call_soon(self.server)
def get_handler(self, request):
"""Handle GET."""
print(request)
def post_handler(self, request):
"""Handle POST."""
print(request)
from emily.inp.gitlab import GitLab
from emily.out.irc import IRC
import emily.core
emily.core.register_in(GitLab)
emily.core.register_out(IRC)
emily.core.run()
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