From 1bb25348e93c03f085c0367f5ec806cc693720fa Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Thu, 22 Oct 2015 17:38:19 -0400 Subject: [PATCH] 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 --- grafana_dashboards/builder.py | 31 ++++--------------------------- grafana_dashboards/cmd.py | 5 ++--- grafana_dashboards/config.py | 29 +++++++++++++++++++++++++++++ tests/base.py | 5 +++-- tests/test_config.py | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 grafana_dashboards/config.py create mode 100644 tests/test_config.py diff --git a/grafana_dashboards/builder.py b/grafana_dashboards/builder.py index f6333a6..e2b72af 100644 --- a/grafana_dashboards/builder.py +++ b/grafana_dashboards/builder.py @@ -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 = [] diff --git a/grafana_dashboards/cmd.py b/grafana_dashboards/cmd.py index 01380f2..3dfb8c7 100644 --- a/grafana_dashboards/cmd.py +++ b/grafana_dashboards/cmd.py @@ -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: diff --git a/grafana_dashboards/config.py b/grafana_dashboards/config.py new file mode 100644 index 0000000..5ecd4ed --- /dev/null +++ b/grafana_dashboards/config.py @@ -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') diff --git a/tests/base.py b/tests/base.py index e72c240..940bf87 100644 --- a/tests/base.py +++ b/tests/base.py @@ -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): diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..0e1cb9f --- /dev/null +++ b/tests/test_config.py @@ -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')