Create default configuration

Here we are creating a simple wrapper object to ConfigParser for the
purpose of simplifying our configuration. Now, we have a single
location to control our configuration defaults.

Change-Id: Ie49938b1eec183055bb8d462d70bcdfb733b360b
Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
Paul Belanger 2015-10-22 17:38:19 -04:00
parent 81b07b548e
commit 1bb25348e9
5 changed files with 71 additions and 32 deletions

View File

@ -25,34 +25,11 @@ LOG = logging.getLogger(__name__)
class Builder(object):
def __init__(self, config):
self.grafana = self._setup_grafana(config)
self.grafana = Grafana(
config.get('grafana', 'apikey'), config.get('grafana', 'url'))
self.parser = YamlParser()
self.cache = self._setup_cache(config)
def _setup_cache(self, config):
if config.has_option('cache', 'enabled'):
self.cache_enabled = config.getboolean('cache', 'enabled')
else:
self.cache_enabled = True
if config.has_option('cache', 'cachedir'):
cachedir = config.get('cache', 'cachedir')
else:
cachedir = '~/.cache/grafyaml'
return Cache(cachedir)
def _setup_grafana(self, config):
if config.has_option('grafana', 'apikey'):
key = config.get('grafana', 'apikey')
else:
key = None
if config.has_option('grafana', 'url'):
url = config.get('grafana', 'url')
else:
url = 'http://localhost:8080'
return Grafana(url, key)
self.cache_enabled = config.getboolean('cache', 'enabled')
self.cache = Cache(config.get('cache', 'cachedir'))
def load_files(self, path):
files_to_process = []

View File

@ -17,9 +17,8 @@ import logging
import os
import sys
from six.moves import configparser as ConfigParser
from grafana_dashboards.builder import Builder
from grafana_dashboards.config import Config
from grafana_dashboards import version
LOG = logging.getLogger(__name__)
@ -66,7 +65,7 @@ class Client(object):
self.args = parser.parse_args()
def read_config(self):
self.config = ConfigParser.ConfigParser()
self.config = Config()
if self.args.config:
fp = self.args.config
else:

View File

@ -0,0 +1,29 @@
# Copyright 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from six.moves import configparser as ConfigParser
class Config(ConfigParser.ConfigParser):
def __init__(self):
ConfigParser.ConfigParser.__init__(self)
# Add [cache] section
self.add_section('cache')
self.set('cache', 'cachedir', '~/.cache/grafyaml')
self.set('cache', 'enabled', 'true')
# Add [grafana] section
self.add_section('grafana')
self.set('grafana', 'apikey', '')
self.set('grafana', 'url', 'http://localhost:8080')

View File

@ -23,9 +23,10 @@ import shutil
import tempfile
import fixtures
from six.moves import configparser as ConfigParser
import testtools
from grafana_dashboards.config import Config
FIXTURE_DIR = os.path.join(
os.path.dirname(__file__), 'fixtures')
@ -65,7 +66,7 @@ class TestCase(testtools.TestCase):
self.addCleanup(self.cleanup_cachedir)
def setup_config(self):
self.config = ConfigParser.ConfigParser()
self.config = Config()
self.config.read(os.path.join(FIXTURE_DIR, 'grafyaml.conf'))
def cleanup_cachedir(self):

33
tests/test_config.py Normal file
View File

@ -0,0 +1,33 @@
# Copyright 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from grafana_dashboards.config import Config
from tests.base import TestCase
class TestCaseConfig(TestCase):
def setUp(self):
super(TestCaseConfig, self).setUp()
self.config = Config()
def test_defaults(self):
self.assertTrue(
self.config.getboolean('cache', 'enabled'))
self.assertEqual(
self.config.get('cache', 'cachedir'), '~/.cache/grafyaml')
self.assertEqual(
self.config.get('grafana', 'apikey'), '')
self.assertEqual(
self.config.get('grafana', 'url'), 'http://localhost:8080')