Beispiel #1
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'])
Beispiel #2
0
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'),
    help = 'Location of the configuration file containing logging parameters. '
           ' Default %(default)s'
)

PARAMETERS.parse(only_known = True)

PARAMETERS.add_configuration_file(PARAMETERS['margarine.configuration_file_path'])

if os.access(PARAMETERS['logging.configuration_file_path'], os.R_OK):
    logging.config.fileConfig(PARAMETERS['logging.configuration_file_path'])
Beispiel #3
0
        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 ' \
                'is collected into.'
        )

PARAMETERS.add_parameter(
        options = [ '--cache-directory' ],
        group = 'pmort',
        metavar = 'DIR',
        default = os.path.join(os.path.sep, 'var', 'cache', sys.argv[0]),
        help = \
                'Specifies the output directory for any learned ' \
                'information.  Default: %(default)s'
        )

PARAMETERS.add_parameter(
        options = [ '--configuration-file-path' ],
        group = 'logging',
        metavar = 'FILE',
        default = os.path.join(CONFIGURATION_DIRECTORY, 'logging.ini'),
        help = \
                'Specifies the file path for the logging configuration file. ' \
        )

PARAMETERS.parse(only_known = True)

PARAMETERS.add_configuration_file(PARAMETERS['pmort.configuration_file_path'])
Beispiel #4
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'])