From f2dc92a68d40ec950a792c74f20a7703d7911e54 Mon Sep 17 00:00:00 2001 From: Sergey Lukjanov Date: Tue, 30 Jul 2013 00:02:21 +0400 Subject: [PATCH] Extract configs-related code to the configs helper * two helpers has been added for getting single and multiple values; * several simple tests has been added to check helpers. Change-Id: Icaebf1737660a1cd7c24d16dcbff900e79904584 --- hacking/config.py | 48 ++++++++++++++++++++++++++++++ hacking/core.py | 35 +++++++++++----------- hacking/tests/test_config.py | 57 ++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 4 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 hacking/config.py create mode 100644 hacking/tests/test_config.py diff --git a/hacking/config.py b/hacking/config.py new file mode 100644 index 0000000..79e3503 --- /dev/null +++ b/hacking/config.py @@ -0,0 +1,48 @@ +# 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 import moves + + +class Config(object): + def __init__(self, default_section=None, tox_file='tox.ini'): + conf = moves.configparser.RawConfigParser() + conf.read(tox_file) + + self.conf = conf + self.default_section = default_section + + def get(self, option, section=None, default=None): + section = section or self.default_section + + if not self.conf.has_section(section): + return default + + if self.conf.has_option(section, option): + return self.conf.get(section, option).strip() + + return default + + def get_multiple(self, option, section=None, default=None): + section = section or self.default_section + + values = self.get(option, section) + if not values: + return default + + values = [v.strip() for v in values.split('\n') if v.strip()] + result = [] + for vals in values: + result.extend([v.strip() for v in vals.split(',') if v.strip()]) + + return result diff --git a/hacking/core.py b/hacking/core.py index 38eec84..8ad2c9e 100755 --- a/hacking/core.py +++ b/hacking/core.py @@ -21,7 +21,6 @@ Built as a sets of pep8 checks using flake8. """ -import ConfigParser import gettext import imp import logging @@ -35,6 +34,8 @@ import traceback import d2to1.util import pep8 +from hacking import config + # Don't need this for testing logging.disable('LOG') @@ -60,6 +61,10 @@ def flake8ext(f): #H8xx git commit messages #H9xx other + +CONF = config.Config('hacking') + + IMPORT_EXCEPTIONS = ['sqlalchemy', 'migrate', 'nova.db.sqlalchemy.session', 'nova.db.sqlalchemy.migration.versioning_api'] # Paste is missing a __init__ in top level directory @@ -887,25 +892,19 @@ class ProxyChecks(GlobalCheck): @classmethod def add_options(cls, parser): - # Abusing this method because of when it gets called - if not os.path.exists('tox.ini'): - return - tox_ini = ConfigParser.RawConfigParser() - tox_ini.read('tox.ini') - if not tox_ini.has_section('hacking'): - return - # We're looking for local checks, so we need to include the local # dir in the search path sys.path.append('.') - if tox_ini.has_option('hacking', 'local-check'): - for check_path in set( - tox_ini.get('hacking', 'local-check').split(",")): - if check_path.strip(): - checker = d2to1.util.resolve_name(check_path) - pep8.register_check(checker) - if tox_ini.has_option('hacking', 'local-check-factory'): - factory = d2to1.util.resolve_name( - tox_ini.get('hacking', 'local-check-factory')) + + local_check = CONF.get_multiple('local-check', default=[]) + for check_path in set(local_check): + if check_path.strip(): + checker = d2to1.util.resolve_name(check_path) + pep8.register_check(checker) + + local_check_fact = CONF.get('local-check-factory') + if local_check_fact: + factory = d2to1.util.resolve_name(local_check_fact) factory(pep8.register_check) + sys.path.pop() diff --git a/hacking/tests/test_config.py b/hacking/tests/test_config.py new file mode 100644 index 0000000..e4b2e6a --- /dev/null +++ b/hacking/tests/test_config.py @@ -0,0 +1,57 @@ +# 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. + +import fixtures +import os + +from hacking import config +from hacking import tests + + +TEST_TOX_INI = """[hacking] +option_1 = val_1 +option_2 = + val_2 +option_3 = + val_1,val_2, val_3, + val_4 , val_5 , val_6, + val_7 + , + val_8 + val_9 +""" + + +class ConfigTest(tests.TestCase): + def setUp(self): + tox_ini_path = os.path.join(self.useFixture(fixtures.TempDir()).path, + 'tox.ini') + + with open(tox_ini_path, 'w') as tox_ini: + tox_ini.write(TEST_TOX_INI) + self.conf = config.Config('hacking', tox_ini_path) + super(ConfigTest, self).setUp() + + def test_get(self): + self.assertEqual('val_1', self.conf.get('option_1')) + self.assertEqual('val_2', self.conf.get('option_2')) + self.assertEqual('val_3', self.conf.get('option_4', default='val_3')) + + def test_get_multiple(self): + self.assertEqual(['val_1', 'val_2', 'val_3', 'val_4', 'val_5', 'val_6', + 'val_7', 'val_8', 'val_9'], + self.conf.get_multiple('option_3')) + + self.assertEqual(['val_1', 'val_2'], + self.conf.get_multiple('option_4', + default=['val_1', 'val_2'])) diff --git a/requirements.txt b/requirements.txt index 3b29528..ee4834d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ pbr>=0.5.10,<0.6 pep8==1.4.5 pyflakes==0.7.2 flake8==2.0 + +six