Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Move to baseplate configuration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
spladug committed Apr 3, 2020
1 parent 1872591 commit b5e8435
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 101 deletions.
27 changes: 18 additions & 9 deletions harold/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
def make_application(ini_file):
import itertools
import itertools

from ConfigParser import RawConfigParser

from twisted.application.service import Application

from twisted.application.service import Application

from harold.conf import HaroldConfiguration
def make_application(ini_file):
from harold.plugins import database
from harold.plugins import deploy
from harold.plugins import github
Expand All @@ -14,14 +16,21 @@ def make_application(ini_file):

application = Application("Harold")

config = HaroldConfiguration(ini_file)
http_plugin = http.make_plugin(config)
db_plugin = database.make_plugin(config)
slack_plugin = slack.make_plugin(config, http_plugin)
parser = RawConfigParser()
with open(ini_file) as fp:
parser.readfp(fp)

# TODO: ditch the separate config sections next
def plugin_config(name):
return dict(parser.items("harold:plugin:" + name))

http_plugin = http.make_plugin(plugin_config("http"))
db_plugin = database.make_plugin(plugin_config("database"))
slack_plugin = slack.make_plugin(plugin_config("slack"))
salons_plugin = salons.make_plugin(db_plugin)

github.make_plugin(http_plugin, slack_plugin, salons_plugin, db_plugin)
deploy.make_plugin(config, http_plugin, slack_plugin, salons_plugin)
deploy.make_plugin(plugin_config("deploy"), http_plugin, slack_plugin, salons_plugin)
httpchat.make_plugin(http_plugin, slack_plugin)

services = itertools.chain(
Expand Down
50 changes: 0 additions & 50 deletions harold/conf.py

This file was deleted.

22 changes: 8 additions & 14 deletions harold/plugins/database.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
from baseplate import config
from sqlalchemy.engine import url
from twisted.enterprise.adbapi import ConnectionPool

from harold.conf import PluginConfig, Option
from harold.plugin import Plugin


class DatabaseConfig(PluginConfig):
connection_string = Option(str)

def get_module_and_params(self):
"""Parse the SQLAlchemy connection string and return DBAPI info."""
sa_url = url.make_url(self.connection_string)
dialect_cls = sa_url.get_dialect()
return dialect_cls.dbapi(), sa_url.translate_connect_args()


class DatabasePlugin(ConnectionPool, Plugin):
"""A plugin that provides a simple twisted ADBAPI interface to a DB."""

def __init__(self, db_config):
Plugin.__init__(self)
self.module, kwargs = db_config.get_module_and_params()

self.module = db_config.connection_string.get_dialect().dbapi()
kwargs = db_config.connection_string.translate_connect_args()
ConnectionPool.__init__(self, self.module.__name__, **kwargs)


def make_plugin(config):
db_config = DatabaseConfig(config)
def make_plugin(app_config):
db_config = config.parse_config(app_config, {
"connection_string": url.make_url,
})
return DatabasePlugin(db_config)
22 changes: 10 additions & 12 deletions harold/plugins/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

from enum import Enum

from baseplate import config
from twisted.web import resource, server
from twisted.internet import reactor, task
from twisted.internet.defer import inlineCallbacks, returnValue

from harold.conf import PluginConfig, Option, tup
from harold.plugins.http import ProtectedResource
from harold.plugins.salons import WouldOrphanRepositoriesError
from harold.utils import (
Expand All @@ -41,15 +41,6 @@
CONCH_GRACE = 60*5


class DeployConfig(PluginConfig):
organizations = Option(tup)
default_hours_start = Option(parse_time)
default_hours_end = Option(parse_time)
default_tz = Option(pytz.timezone)
blackout_hours_start = Option(parse_time)
blackout_hours_end = Option(parse_time)


class DeployHoldType(Enum):
code_freeze = 'Code Freeze'
manual = 'Manual'
Expand Down Expand Up @@ -1109,8 +1100,15 @@ def announce(self, irc, sender, channel, *message):
sender, message))


def make_plugin(config, http, irc, salons):
deploy_config = DeployConfig(config)
def make_plugin(app_config, http, irc, salons):
deploy_config = config.parse_config(app_config, {
"organizations": config.TupleOf(config.String),
"default_hours_start": parse_time,
"default_hours_end": parse_time,
"default_tz": pytz.timezone,
"blackout_hours_start": parse_time,
"blackout_hours_end": parse_time,
})
monitor = DeployMonitor(deploy_config, irc, salons)

# set up http api
Expand Down
15 changes: 6 additions & 9 deletions harold/plugins/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
import hmac
import urlparse

from baseplate import config
from twisted.web import resource, server
from twisted.application import internet
from twisted.internet import reactor
from twisted.internet.endpoints import serverFromString

from harold.plugin import Plugin
from harold.conf import PluginConfig, Option
from harold.utils import constant_time_compare


class HttpConfig(PluginConfig):
endpoint = Option(str)
hmac_secret = Option(str, default=None)
public_root = Option(str, default="")


class AuthenticationError(Exception):
pass

Expand Down Expand Up @@ -66,8 +60,11 @@ def render_POST(self, request):
return response or ""


def make_plugin(config):
http_config = HttpConfig(config)
def make_plugin(app_config):
http_config = config.parse_config(app_config, {
"endpoint": config.String,
"hmac_secret": config.String,
})

root = resource.Resource()
harold = resource.Resource()
Expand Down
12 changes: 5 additions & 7 deletions harold/plugins/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
WebSocketClientFactory,
WebSocketClientProtocol,
)
from baseplate import config
from twisted.application.internet import ClientService
from twisted.internet import reactor
from twisted.internet.defer import succeed, inlineCallbacks, returnValue, Deferred
Expand All @@ -17,15 +18,10 @@
from twisted.web.http_headers import Headers
from zope.interface import implementer

from harold.conf import PluginConfig, Option
from harold.handlers import Handlers, NoHandlerError
from harold.plugin import Plugin


class SlackConfig(PluginConfig):
token = Option(str)


class FormEncodedBodyProducer(object):
def __init__(self, data):
encoded = urllib.urlencode(
Expand Down Expand Up @@ -445,8 +441,10 @@ def _onChat(self, payload):
traceback.print_exc()


def make_plugin(config, http=None):
slack_config = SlackConfig(config)
def make_plugin(app_config):
slack_config = config.parse_config(app_config, {
"token": config.String,
})

api_client = SlackWebClient(slack_config.token)
endpoint = SlackEndpoint(api_client)
Expand Down

0 comments on commit b5e8435

Please sign in to comment.