Example #1
0
    def test_add_duplicate_parameters_resolve_conflict_handler(self):
        '''Parameters(conflict_handler = resolve).add_parameter()—with duplicate'''

        self.p = Parameters(conflict_handler = 'resolve')

        self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))
        self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))
Example #2
0
    def test_add_duplicate_parameters(self):
        '''Parameters().add_parameter()—with duplicate'''

        self.p = Parameters()

        self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))

        with self.assertRaises(argparse.ArgumentError):
            self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))
Example #3
0
    def setUp(self):
        self.original_argv0 = sys.argv[0]

        def _():
            sys.argv[0] = self.original_argv0
        self.addCleanup(_)

        sys.argv[0] = 'crumbs'

        self.p = Parameters()
Example #4
0
    def test_add_configuration_file_with_inotify(self):
        '''Parameters(inotify = True).add_configuration_file()'''

        self.p = Parameters(inotify = True)

        self._assert_configuration_readable()

        with open(self.file_name, 'a') as fh:
            fh.write('bar = foo')

        time.sleep(1)

        self.assertEqual('bar', self.p['default.foo'])
        self.assertEqual('foo', self.p['default.bar'])
Example #5
0
    def test_add_configuration_file_with_explicit_read(self):
        '''Parameters().add_configuration_file()—re-read'''

        self.p = Parameters()

        self._assert_configuration_readable()

        with open(self.file_name, 'a') as fh:
            fh.write('bar = foo')

        self.p.read_configuration_files()

        self.assertEqual('bar', self.p['default.foo'])
        self.assertEqual('foo', self.p['default.bar'])
Example #6
0
class ParametersParseTest(unittest.TestCase):
    def setUp(self):
        self.p = Parameters()

    def test_parse(self):
        '''Parameters().parse()'''

        self.p.parse()

        self.assertTrue(self.p.parsed)

    def test_parse_with_help(self):
        '''Parameters().parse()—with --help'''

        sys.argv.append('--help')
        self.addCleanup(functools.partial(sys.argv.remove, '--help'))

        with self.assertRaises(SystemExit):
            self.p.parse()

    def test_parse_only_known(self):
        '''Parameters().parse(only_known = True)'''

        self.p.parse(only_known = True)

        self.assertFalse(self.p.parsed)

    def test_parse_only_known_with_help(self):
        '''Parameters().parse(only_known = True)—with --help'''

        sys.argv.append('--help')
        self.addCleanup(functools.partial(sys.argv.remove, '--help'))

        self.p.parse(only_known = True)

        self.assertFalse(self.p.parsed)
Example #7
0
    def test_add_configuration_file(self):
        '''Parameters().add_configuration_file()'''

        self.p = Parameters()

        self._assert_configuration_readable()
Example #8
0
class ParametersReadTest(unittest.TestCase):
    def setUp(self):
        self.original_argv0 = sys.argv[0]

        def _():
            sys.argv[0] = self.original_argv0
        self.addCleanup(_)

        sys.argv[0] = 'crumbs'

        self.p = Parameters()

    def populateMulti(self, group = False):
        self.p.add_parameter(options = [ '--multi', ])

    def populateEnvironment(self):
        os.environ['CRUMBS_ENVIRONMENT_ONLY'] = 'environment_only'
        os.environ['CRUMBS_MULTI'] = 'environment_multi'

        self.addCleanup(functools.partial(os.unsetenv, 'CRUMBS_ENVIRONMENT_ONLY'))
        self.addCleanup(functools.partial(os.unsetenv, 'CRUMBS_MULTI'))

        self.p.add_parameter(options = ( '--environment-only', ), only = ( 'environment', ))

    def populateArgumentVector(self):
        sys.argv.extend([ '--argument-only', 'argument_only' ])
        sys.argv.extend([ '--multi', 'argument_multi' ])

        self.addCleanup(functools.partial(sys.argv.remove, '--argument-only'))
        self.addCleanup(functools.partial(sys.argv.remove, 'argument_only'))
        self.addCleanup(functools.partial(sys.argv.remove, '--multi'))
        self.addCleanup(functools.partial(sys.argv.remove, 'argument_multi'))

        self.p.add_parameter(options = [ '--argument-only', ], only = ( 'argument', ))

    def populateConfiguration(self):
        tmp_fh = tempfile.NamedTemporaryFile(mode = 'w')
        tmp_fh.write(
            '[default]\n'
            'configuration_only = configuration_only\n'
            'multi = configuration_multi\n'
            '\n'
            'type_int = 15\n'
        )

        tmp_fh.seek(0)

        self.addCleanup(tmp_fh.close)

        self.p.add_parameter(options = ( '--configuration-only', ), only = ( 'configuration', ))
        self.p.add_configuration_file(tmp_fh.name)

    def populateTypes(self):
        sys.argv.extend([ '--type-int', '15' ])

        self.addCleanup(functools.partial(sys.argv.remove, '--type-int'))
        self.addCleanup(functools.partial(sys.argv.remove, '15'))

        self.p.add_parameter(options = [ '--type-int', ], type = int, default = 0)

    def test_read_implicit_default_group(self):
        '''Parameters()[key]—implicit default group'''

        self.populateEnvironment()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('environment_only', self.p['default.environment_only'])

    def test_read_underscores_or_hyphens(self):
        '''Parameters()[key]—indistinguishable characters: -, _'''

        self.populateEnvironment()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment-only'])
        self.assertEqual('environment_only', self.p['environment_only'])

    def test_read_types(self):
        '''Parameters()[key]—type cast'''

        self.populateTypes()

        self.p.parse()

        self.assertIsInstance(self.p['type_int'], int)
        self.assertEqual(15, self.p['type_int'])

    def test_read_environment(self):
        '''Parameters()[key]—environment'''

        self.populateEnvironment()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('environment_multi', self.p['multi'])

    def test_read_configuration(self):
        '''Parameters()[key]—configuration'''

        self.populateConfiguration()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('configuration_multi', self.p['multi'])

    def test_read_argument(self):
        '''Parameters()[key]—argument'''

        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])

    def test_read_environment_configuration(self):
        '''Parameters()[key]—environment,configuration'''

        self.populateEnvironment()
        self.populateConfiguration()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('configuration_multi', self.p['multi'])

    def test_read_environment_argument(self):
        '''Parameters()[key]—environment,argument'''

        self.populateEnvironment()
        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])

    def test_read_configuration_argument(self):
        '''Parameters()[key]—configuration,argument'''

        self.populateConfiguration()
        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])

    def test_read_environment_configuration_argument(self):
        '''Parameters()[key]—environment,configuration,argument'''

        self.populateEnvironment()
        self.populateConfiguration()
        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])
Example #9
0
# Copyright (C) 2013 by Alex Brandt <*****@*****.**>
#
# pmort is freely distributable under the terms of an MIT-style license.
# See COPYING or http://www.opensource.org/licenses/mit-license.php.

import os
import sys

from crumbs import Parameters

CONFIGURATION_DIRECTORY = os.path.join(os.path.sep, 'etc', 'pmort')

PARAMETERS = Parameters(conflict_handler = 'resolve')

PARAMETERS.add_parameter(
        group = 'pmort',
        options = [ '--configuration-file-path', '-c' ],
        metavar = 'FILE',
        default = os.path.join(CONFIGURATION_DIRECTORY, 'pmort.ini'),
        help = \
                'Location of the configuration file to search for pmort ' \
                'parameters.  Default: %(default)s'
        )

PARAMETERS.add_parameter(
        options = [ '--output-directory', '-o' ],
        group = 'pmort',
        metavar = 'DIR',
        default = os.path.join(os.path.sep, 'var', 'spool', sys.argv[0]),
        help = \
                'Specifies the output directory that the collection output ' \
Example #10
0
    def test_add_parameters_without_group_prefix(self):
        '''Parameters(group_prefix = False).add_parameters()'''

        self.p = Parameters(group_prefix = False)

        self._assert_parameters_add(self.parameters['valid'], group_prefix = False)
Example #11
0
    def test_add_parameters_with_group_prefix(self):
        '''Parameters().add_parameter()'''

        self.p = Parameters()

        self._assert_parameters_add(self.parameters['valid'])
Example #12
0
class ParametersAddParametersTest(BaseParametersTest):
    def _assert_parameters_add(self, parameters, group_prefix = True):
        for inputs in parameters['inputs']:
            self.p.add_parameter(**inputs)

        logger.debug('defaults: %s', self.p.defaults)
        self.assertEqual(parameters['defaults'], self.p.defaults)

        if group_prefix:
            logger.info('adding group prefixes to arguments')

            def _add_group_to_long_options(parameters):
                for _ in parameters:
                    if _['group'] != 'default':
                        long_option = max(_['options'], key = len)

                        _['options'].remove(long_option)
                        _['options'].append(long_option.replace('--', '--' + _['group'].replace('_', '-') + '-'))

            _add_group_to_long_options(parameters['parameters'].values())

            for _ in parameters['grouped_parameters'].values():
                _add_group_to_long_options(_.values())

        logger.debug('parameters.keys(): %s', parameters.keys())

        _, self.maxDiff = self.maxDiff, None

        logger.debug('parameters: %s', self.p.parameters)
        self.assertEqual(parameters['parameters'], self.p.parameters)

        logger.debug('grouped_parameters: %s', self.p.grouped_parameters)
        self.assertEqual(parameters['grouped_parameters'], self.p.grouped_parameters)

        self.maxDiff = _

        logger.debug('groups: %s', self.p.groups)
        self.assertEqual(parameters['groups'], self.p.groups)

        self.assertFalse(self.p.parsed)

    def test_add_parameters_with_group_prefix(self):
        '''Parameters().add_parameter()'''

        self.p = Parameters()

        self._assert_parameters_add(self.parameters['valid'])

    def test_add_parameters_without_group_prefix(self):
        '''Parameters(group_prefix = False).add_parameters()'''

        self.p = Parameters(group_prefix = False)

        self._assert_parameters_add(self.parameters['valid'], group_prefix = False)

    def test_add_parameters_with_group_prefix_resolve_conflict_handler(self):
        '''Parameters(conflict_handler = resolve).add_parameter()'''

        self.p = Parameters(conflict_handler = 'resolve')

        self._assert_parameters_add(self.parameters['valid'])

    def test_add_parameters_without_group_prefix_resolve_conflict_handler(self):
        '''Parameters(group_prefix = False, conflict_handler = resolve).add_parameter()'''

        self.p = Parameters(group_prefix = False, conflict_handler = 'resolve')

        self._assert_parameters_add(self.parameters['valid'], group_prefix = False)

    def test_add_duplicate_parameters(self):
        '''Parameters().add_parameter()—with duplicate'''

        self.p = Parameters()

        self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))

        with self.assertRaises(argparse.ArgumentError):
            self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))

    def test_add_duplicate_parameters_resolve_conflict_handler(self):
        '''Parameters(conflict_handler = resolve).add_parameter()—with duplicate'''

        self.p = Parameters(conflict_handler = 'resolve')

        self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))
        self.p.add_parameter(**copy.deepcopy(self.parameters['valid']['inputs'][0]))
Example #13
0
 def setUp(self):
     self.p = Parameters()
Example #14
0
class ParametersReadTest(unittest.TestCase):
    def setUp(self):
        self.original_argv0 = sys.argv[0]

        def _():
            sys.argv[0] = self.original_argv0

        self.addCleanup(_)

        sys.argv[0] = 'crumbs'

        self.p = Parameters()

    def populateMulti(self, group=False):
        self.p.add_parameter(options=[
            '--multi',
        ])

    def populateEnvironment(self):
        os.environ['CRUMBS_ENVIRONMENT_ONLY'] = 'environment_only'
        os.environ['CRUMBS_MULTI'] = 'environment_multi'

        self.addCleanup(
            functools.partial(os.unsetenv, 'CRUMBS_ENVIRONMENT_ONLY'))
        self.addCleanup(functools.partial(os.unsetenv, 'CRUMBS_MULTI'))

        self.p.add_parameter(options=('--environment-only', ),
                             only=('environment', ))

    def populateArgumentVector(self):
        sys.argv.extend(['--argument-only', 'argument_only'])
        sys.argv.extend(['--multi', 'argument_multi'])

        self.addCleanup(functools.partial(sys.argv.remove, '--argument-only'))
        self.addCleanup(functools.partial(sys.argv.remove, 'argument_only'))
        self.addCleanup(functools.partial(sys.argv.remove, '--multi'))
        self.addCleanup(functools.partial(sys.argv.remove, 'argument_multi'))

        self.p.add_parameter(options=[
            '--argument-only',
        ],
                             only=('argument', ))

    def populateConfiguration(self):
        tmp_fh = tempfile.NamedTemporaryFile(mode='w')
        tmp_fh.write('[default]\n'
                     'configuration_only = configuration_only\n'
                     'multi = configuration_multi\n'
                     '\n'
                     'type_int = 15\n')

        tmp_fh.seek(0)

        self.addCleanup(tmp_fh.close)

        self.p.add_parameter(options=('--configuration-only', ),
                             only=('configuration', ))
        self.p.add_configuration_file(tmp_fh.name)

    def populateTypes(self):
        sys.argv.extend(['--type-int', '15'])

        self.addCleanup(functools.partial(sys.argv.remove, '--type-int'))
        self.addCleanup(functools.partial(sys.argv.remove, '15'))

        self.p.add_parameter(options=[
            '--type-int',
        ], type=int, default=0)

    def test_read_implicit_default_group(self):
        '''Parameters()[key]—implicit default group'''

        self.populateEnvironment()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('environment_only',
                         self.p['default.environment_only'])

    def test_read_underscores_or_hyphens(self):
        '''Parameters()[key]—indistinguishable characters: -, _'''

        self.populateEnvironment()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment-only'])
        self.assertEqual('environment_only', self.p['environment_only'])

    def test_read_types(self):
        '''Parameters()[key]—type cast'''

        self.populateTypes()

        self.p.parse()

        self.assertIsInstance(self.p['type_int'], int)
        self.assertEqual(15, self.p['type_int'])

    def test_read_custom_environment_prefix(self):
        '''Parameters()[key]—environment_prefix=custom'''

        os.environ['CUSTOM_CUSTOM_ENVIRONMENT'] = 'custom_environment'

        self.addCleanup(
            functools.partial(os.unsetenv, 'CUSTOM_CUSTOM_ENVIRONMENT'))

        self.p.add_parameter(options=('--custom-environment', ),
                             only=('environment', ),
                             environment_prefix='custom')

        self.p.parse()

        self.assertEqual('custom_environment', self.p['custom_environment'])

    def test_read_empty_environment_prefix(self):
        '''Parameters()[key]—environment_prefix=None'''

        os.environ['CUSTOM_ENVIRONMENT'] = 'custom_environment'

        self.addCleanup(functools.partial(os.unsetenv, 'CUSTOM_ENVIRONMENT'))

        self.p.add_parameter(options=('--custom-environment', ),
                             only=('environment', ),
                             environment_prefix=None)

        self.p.parse()

        self.assertEqual('custom_environment', self.p['custom_environment'])

    def test_read_environment_with_expansion(self):
        '''Parameters()[key]—with expansion'''

        os.environ['FOO'] = 'foo'
        self.addCleanup(functools.partial(os.unsetenv, 'FOO'))

        os.environ['EXPAND'] = '${FOO}'
        self.addCleanup(functools.partial(os.unsetenv, 'EXPAND'))

        self.p.add_parameter(options=('--expand', ),
                             only=('environment', ),
                             environment_prefix=None)

        self.p.parse()

        self.assertEqual('foo', self.p['expand'])

    def test_read_environment(self):
        '''Parameters()[key]—environment'''

        self.populateEnvironment()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('environment_multi', self.p['multi'])

    def test_read_configuration(self):
        '''Parameters()[key]—configuration'''

        self.populateConfiguration()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('configuration_multi', self.p['multi'])

    def test_read_argument(self):
        '''Parameters()[key]—argument'''

        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])

    def test_read_environment_configuration(self):
        '''Parameters()[key]—environment,configuration'''

        self.populateEnvironment()
        self.populateConfiguration()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('configuration_multi', self.p['multi'])

    def test_read_environment_argument(self):
        '''Parameters()[key]—environment,argument'''

        self.populateEnvironment()
        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])

    def test_read_configuration_argument(self):
        '''Parameters()[key]—configuration,argument'''

        self.populateConfiguration()
        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])

    def test_read_environment_configuration_argument(self):
        '''Parameters()[key]—environment,configuration,argument'''

        self.populateEnvironment()
        self.populateConfiguration()
        self.populateArgumentVector()
        self.populateMulti()

        self.p.parse()

        self.assertEqual('environment_only', self.p['environment_only'])
        self.assertEqual('configuration_only', self.p['configuration_only'])
        self.assertEqual('argument_only', self.p['argument_only'])
        self.assertEqual('argument_multi', self.p['multi'])
Example #15
0
class ParametersAddConfigurationFileTest(BaseParametersTest):
    def setUp(self):
        super(ParametersAddConfigurationFileTest, self).setUp()

        tmp_fh = tempfile.NamedTemporaryFile(mode = 'w')
        tmp_fh.write(
            '[default]\n'
            'foo = bar\n'
        )

        tmp_fh.seek(0)

        self.addCleanup(tmp_fh.close)

        self.file_name = tmp_fh.name

    def _assert_configuration_readable(self):
        self.p.add_parameter(options = ( '--foo', ))
        self.p.add_parameter(options = ( '--bar', ))

        self.p.add_configuration_file(self.file_name)

        self.assertEqual([ self.file_name ], list(self.p.configuration_files.keys()))
        self.assertIsInstance(self.p.configuration_files[self.file_name], SafeConfigParser)

        self.p.parse()

        self.assertEqual('bar', self.p['default.foo'])
        self.assertIsNone(self.p['default.bar'])

    def test_add_configuration_file(self):
        '''Parameters().add_configuration_file()'''

        self.p = Parameters()

        self._assert_configuration_readable()

    def test_add_configuration_file_with_explicit_read(self):
        '''Parameters().add_configuration_file()—re-read'''

        self.p = Parameters()

        self._assert_configuration_readable()

        with open(self.file_name, 'a') as fh:
            fh.write('bar = foo')

        self.p.read_configuration_files()

        self.assertEqual('bar', self.p['default.foo'])
        self.assertEqual('foo', self.p['default.bar'])

    @unittest.skipUnless(_pyinotify_loaded, 'inotify module not available')
    def test_add_configuration_file_with_inotify(self):
        '''Parameters(inotify = True).add_configuration_file()'''

        self.p = Parameters(inotify = True)

        self._assert_configuration_readable()

        with open(self.file_name, 'a') as fh:
            fh.write('bar = foo')

        time.sleep(1)

        self.assertEqual('bar', self.p['default.foo'])
        self.assertEqual('foo', self.p['default.bar'])
Example #16
0
    def test_add_parameters_without_group_prefix_resolve_conflict_handler(self):
        '''Parameters(group_prefix = False, conflict_handler = resolve).add_parameter()'''

        self.p = Parameters(group_prefix = False, conflict_handler = 'resolve')

        self._assert_parameters_add(self.parameters['valid'], group_prefix = False)
Example #17
0
# Copyright (C) 2014 by Alex Brandt <*****@*****.**>
#
# margarine is freely distributable under the terms of an MIT-style license.
# See COPYING or http://www.opensource.org/licenses/mit-license.php.

import logging
import logging.config
import os

from crumbs import Parameters

logger = logging.getLogger(__name__)

CONFIGURATION_DIRECTORY = os.path.join(os.path.sep, 'etc', 'margarine')

PARAMETERS = Parameters(conflict_handler = 'resolve', inotify = True)

PARAMETERS.add_parameter(
    group = 'margarine',
    options = [ '--configuration-file-path', '-c' ],
    metavar = 'FILE',
    default = os.path.join(CONFIGURATION_DIRECTORY, 'margarine.ini'),
    help = 'Location of the configuration file containing margarine '
           'parameters.  Default %(default)s'
)

PARAMETERS.add_parameter(
    group = 'logging',
    options = [ '--configuration-file-path' ],
    metavar = 'FILE',
    default = os.path.join(CONFIGURATION_DIRECTORY, 'logging.ini'),
Example #18
0
    def test_add_configuration_file(self):
        '''Parameters().add_configuration_file()'''

        self.p = Parameters()

        self._assert_configuration_readable()