diff --git a/grafana_dashboards/builder.py b/grafana_dashboards/builder.py index c6a9b5d..be5c62d 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 delete_dashboard(self, path): self.load_files(path) diff --git a/grafana_dashboards/cmd.py b/grafana_dashboards/cmd.py index 22681da..6d2ecdc 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__) @@ -77,7 +76,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')