Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config loader from config file #146

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.env
venv/
*.pyc
config.py
./config.py
*.config.py
limbo.sqlite3
limbo.egg-info
Expand All @@ -17,3 +17,4 @@ slask.egg-info/
.vagrant
.python-version
.cache
.idea
7 changes: 7 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[DEFAULT]

SLACK_TOKEN = token

[BOT]

logfile = logfile.log
55 changes: 55 additions & 0 deletions limbo/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import logging
import os

import configparser as confparser


class Config(object):

def __init__(self):
self.env_config = {}
self.file_config = None # type: confparser.ConfigParser
self.load_env_config()
self.load_config_file()

def load_config_file(self):
config_file = self.env_config.get("config_location", "config.ini")
# check if config exists
if not os.path.exists(config_file):
logger = logging.getLogger(__name__)
logger.warning("File for configuration not found")
else:
self.file_config = confparser.ConfigParser()
self.file_config.read(config_file)

def load_env_config(self):
config = {}
self._getif(config, "token", "SLACK_TOKEN")
self._getif(config, "loglevel", "LIMBO_LOGLEVEL")
self._getif(config, "logfile", "LIMBO_LOGFILE")
self._getif(config, "logformat", "LIMBO_LOGFORMAT")
self._getif(config, "plugins", "LIMBO_PLUGINS")
self._getif(config, "config_location", "LIMBO_CONFIG_LOCATION")
self.env_config = config

def _getif(self, config, name, envvar):
if envvar in os.environ:
config[name] = os.environ.get(envvar)

def get_by_section(self, plugin_name, item_name):
if plugin_name and plugin_name + "_" + str(item_name) in self.env_config.keys():
return self.env_config[plugin_name + "_" + item_name]
if not self.file_config:
return None
try:
return self.file_config.get(plugin_name, item_name)
except confparser.Error:
return None # in case of not finding it anywhere

def get(self, *args):
if len(args) > 1:
return self.get_by_section(args[0], args[1])
return self.get_by_section("BOT", args[0])

def __getitem__(self, item):
return self.get("BOT", item)
18 changes: 2 additions & 16 deletions limbo/limbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .slack import SlackClient, SlackConnectionError, SlackLoginError
from .server import LimboServer
from .fakeserver import FakeServer
from .config import Config

CURDIR = os.path.abspath(os.path.dirname(__file__))
DIR = functools.partial(os.path.join, CURDIR)
Expand Down Expand Up @@ -170,21 +171,6 @@ def handle_event(event, server):
return handler(event, server)


def getif(config, name, envvar):
if envvar in os.environ:
config[name] = os.environ.get(envvar)


def init_config():
config = {}
getif(config, "token", "SLACK_TOKEN")
getif(config, "loglevel", "LIMBO_LOGLEVEL")
getif(config, "logfile", "LIMBO_LOGFILE")
getif(config, "logformat", "LIMBO_LOGFORMAT")
getif(config, "plugins", "LIMBO_PLUGINS")

return config


def loop(server, test_loop=None):
"""Run the main loop
Expand Down Expand Up @@ -308,7 +294,7 @@ def encode(str_, codec='utf8'):


def main(args):
config = init_config()
config = Config()
if args.test:
init_log(config)
db = init_db(args.database_name)
Expand Down
2 changes: 2 additions & 0 deletions test/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

DIR = os.path.dirname(os.path.realpath(__file__))
TESTPLUGINS = os.path.join(DIR, "plugins")
os.environ["LIMBO_CONFIG_LOCATION"] = "config.ini"


# http://stackoverflow.com/a/13160748/42559
def sh(cmd):
Expand Down
24 changes: 24 additions & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: UTF-8 -*-
import os

DIR = os.path.dirname(os.path.realpath(__file__))
PARENT = os.path.split(DIR)[0]

from limbo.config import Config


def test_config():
# test wrong config location
os.environ["LIMBO_CONFIG_LOCATION"] = "config_non_existent.ini"
config = Config()
assert config.file_config is None

# know a correct config location
os.environ["LIMBO_CONFIG_LOCATION"] = "config.ini"
config = Config()
assert config["SLACK_TOKEN"] == "token"

# test non existing setting
result = config.get("weather", "weather_token")
assert not result