Skip to content

Commit

Permalink
dynamic generic server name
Browse files Browse the repository at this point in the history
  • Loading branch information
isysd committed Aug 17, 2016
1 parent 13bcf50 commit 1ab7771
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ build:

testenv:
$(call makebase, "")
sudo cp cfg.ini /etc/tapp/test_cfg.ini
sudo cp cfg.ini /etc/tapp/test.ini
sudo cp test/env_cfg.ini /etc/tapp/test_env.ini

install:
$(call makebase, "")
Expand All @@ -23,6 +24,7 @@ purge:
rm -rf build dist tapp_config.egg-info tests/__pycache__ tapp_config/__pycache__
rm -rf tests/*.pyc tapp_config/*.pyc *.egg
rm -f .coverage*
rm -fR /etc/tapp/test_tapp.ini /etc/tapp/test_env_tapp.ini

rst:
pandoc --from=markdown_github --to=rst --output=README.rst README.md
19 changes: 14 additions & 5 deletions tapp_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
as well as logging and redis.
Database configuration is done in sqlalchemy_models, since it has the content to test with.
"""
from os.path import expanduser
from os.path import isfile

try:
from ConfigParser import ConfigParser, NoOptionError
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser, NoOptionError
from configparser import ConfigParser

import logging
import os
Expand All @@ -27,7 +27,16 @@ def get_config(name=__name__):
:return: A config parser matching the given name
"""
cfg = ConfigParser()
path = os.environ.get('%s_CONFIG_FILE' % name.upper(), '/etc/tapp/%s_cfg.ini' % name)
path = os.environ.get('%s_CONFIG_FILE' % name.upper())
if path is None or path == "":
fname = '/etc/tapp/%s.ini' % name
if isfile(fname):
path = fname
elif isfile('cfg.ini'):
path = 'cfg.ini'
else:
raise ValueError("Unable to get configuration for tapp %s" % name)

cfg.read(path)
return cfg

Expand All @@ -43,7 +52,7 @@ def setup_logging(name, cfg=None):
"""
logname = "%s_tapp.log" % name
logfile = cfg.get('log', 'LOGFILE') if cfg is not None and \
cfg.get('log', 'LOGFILE') is not None else 'server.log'
cfg.get('log', 'LOGFILE') is not None else logname
loglevel = cfg.get('log', 'LOGLEVEL') if cfg is not None and \
cfg.get('log', 'LOGLEVEL') is not None else logging.INFO
logging.basicConfig(filename=logfile, level=loglevel)
Expand Down
13 changes: 9 additions & 4 deletions test/test_tapp_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ def tearDown(self):
self.setUp()

def test_get_default_config(self):
cfg = get_config("test")
cfg = get_config("test_not_known")
assert cfg.get('db', 'SA_ENGINE_URI') == 'sqlite:////tmp/test.db'
assert cfg.get('log', 'LOGFILE') == '/tmp/test.log'

def test_get_installed_config(self):
cfg = get_config("test_env")
assert cfg.get('db', 'SA_ENGINE_URI') == 'sqlite:////tmp/env_test.db'
assert cfg.get('log', 'LOGFILE') == '/tmp/env_test.log'

def test_get_environment_config(self):
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'env_cfg.ini')
os.environ["TEST_CONFIG_FILE"] = path
Expand All @@ -42,7 +47,7 @@ def setUp(self):
except OSError:
pass
try:
os.remove("server.log")
os.remove("/var/log/test_tapp.log")
except OSError:
pass

Expand All @@ -55,11 +60,11 @@ def tearDown(self):
self.setUp()

def test_setup_default_logger(self):
logger = setup_logging('test')
logger = setup_logging('test_none')
assert isinstance(logger, logging.getLoggerClass())
message = 'testing 1.2.3.'
logger.exception(message)
assert os.path.isfile('server.log')
assert os.path.isfile("test_none_tapp.log")

def test_setup_logger(self):
cfg = get_config("test")
Expand Down

0 comments on commit 1ab7771

Please sign in to comment.