make the sphinxpolicygen extension handle multiple input/output files

Some projects have multiple policy files for different parts of their
API. Make the sample generator extension support this by letting the
policy_generator_config_file option be set to a list of tuples mapping
the config file to the output file base name, as we do in the sample
generator in oslo.config.

Change-Id: I0c7fa409a1ed0f49d65c9b90b71317066f6d3505
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-04-26 16:59:24 -04:00 committed by Matt Riedemann
parent c474b36793
commit 0e7bb28224
3 changed files with 42 additions and 3 deletions

View File

@ -27,6 +27,11 @@ where:
source directory (``app.srcdir``). If this option is not specified or is
invalid then the sample policy file generation will be skipped.
To handle cases where multiple files need to be generated, this
value can be a list of two-part tuples containing the path to the
configuration file and the base name for the output file (in this
case, ``sample_policy_basename`` should not be set).
``sample_policy_basename``
Base name of the output file. This name will be appended with a
``.policy.yaml.sample`` extension to generate the final output file and the

View File

@ -28,11 +28,21 @@ def generate_sample(app):
"skipping sample policy generation")
return
if isinstance(app.config.policy_generator_config_file, list):
for config_file, base_name in app.config.policy_generator_config_file:
if base_name is None:
base_name = _get_default_basename(config_file)
_generate_sample(app, config_file, base_name)
else:
_generate_sample(app,
app.config.policy_generator_config_file,
app.config.sample_policy_basename)
def _get_default_basename(config_file):
return os.path.splitext(os.path.basename(config_file))[0]
def _generate_sample(app, policy_file, base_name):
def info(msg):

View File

@ -50,3 +50,27 @@ class SingleSampleGenerationTest(base.BaseTestCase):
sample.assert_called_once_with(args=[
'--config-file', '/opt/nova/nova.conf',
'--output-file', '/opt/nova/sample.policy.yaml'])
@mock.patch('os.path.isdir')
@mock.patch('os.path.isfile')
@mock.patch('oslo_policy.generator.generate_sample')
def test_sample_gen_with_multiple_config_files(self, sample, isfile,
isdir):
# Tests the scenario that policy_generator_config_file is a list
# of two-item tuples of the config file name and policy basename.
isfile.side_effect = [False, True] * 2
isdir.return_value = True
config = mock.Mock(policy_generator_config_file=[
('nova.conf', 'nova'),
('placement.conf', 'placement')])
app = mock.Mock(srcdir='/opt/nova', config=config)
sphinxpolicygen.generate_sample(app)
sample.assert_has_calls([
mock.call(args=[
'--config-file', '/opt/nova/nova.conf',
'--output-file', '/opt/nova/nova.policy.yaml.sample']),
mock.call(args=[
'--config-file', '/opt/nova/placement.conf',
'--output-file', '/opt/nova/placement.policy.yaml.sample'])])