From 377ce73c58b6dea6c2cb61915f8a5c540bc96324 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Fri, 16 Feb 2018 11:47:36 -0600 Subject: [PATCH] new: usr: Added new Config source that allows to add arbitrary data directly from the pipeline definition. --- examples/config/pipeline.toml | 27 +++++++ lib/flowbber/components/base.py | 5 +- lib/flowbber/plugins/sources/config.py | 102 +++++++++++++++++++++++++ setup.py | 3 + test/test_examples.py | 1 + 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 examples/config/pipeline.toml create mode 100644 lib/flowbber/plugins/sources/config.py diff --git a/examples/config/pipeline.toml b/examples/config/pipeline.toml new file mode 100644 index 0000000..92b721c --- /dev/null +++ b/examples/config/pipeline.toml @@ -0,0 +1,27 @@ +[[sources]] +type = "config" +id = "config1" + + [sources.config.data] + name = "this is a name" + + [sources.config.data.embedded] + somefield = 123 + + [sources.config.data.embedded.morefields] + comment = "This could go forever" + + +[[sinks]] +type = "print" +id = "print" + +[[sinks]] +type = "archive" +id = "archive" + + [sinks.config] + output = "data.json" + override = true + create_parents = true + pretty = true diff --git a/lib/flowbber/components/base.py b/lib/flowbber/components/base.py index 0c343b2..2e52fa4 100644 --- a/lib/flowbber/components/base.py +++ b/lib/flowbber/components/base.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2017 KuraLabs S.R.L +# Copyright (C) 2017-2018 KuraLabs S.R.L # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -118,7 +118,7 @@ class Component(metaclass=NamedABCMeta): That is, it is allowed to fail and the pipeline won't fail. :var int timeout: Execution timeout for this component, in seconds. None means no timeout, wait forever. - :var namedtuple config: Frozen configuration after validation. + :var namedtuple Component.config: Frozen configuration after validation. **Parameters**: @@ -147,6 +147,7 @@ def __init__( configurator = Configurator() self.declare_config(configurator) + self.config = configurator.validate(config or {}) @property diff --git a/lib/flowbber/plugins/sources/config.py b/lib/flowbber/plugins/sources/config.py new file mode 100644 index 0000000..9f3b2ac --- /dev/null +++ b/lib/flowbber/plugins/sources/config.py @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2017-2018 KuraLabs S.R.L +# +# 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. + +""" + +Config +====== + +This sources let the user indicate static data to describe current pipeline. + +**Data collected:** + +*Anything the user entered on the configuration* + +**Dependencies:** + +.. code-block:: sh + + pip3 install flowbber[config] + +**Usage:** + +.. code-block:: toml + + [[sources]] + type = "config" + id = "..." + + [sources.config.data] + anydata = "...." + +.. code-block:: json + + { + "sources": [ + { + "type": "config", + "id": "...", + "config": { + "data": { + "anydata": "...." + } + } + } + ] + } + + +data +---- + +Free off schema data. + +- **Default**: ``False`` +- **Optional**: ``False`` +- **Schema**: + + .. code-block:: python3 + + { + 'type': 'dict' + } + +- **Secret**: ``False`` + +""" # noqa + +from copy import deepcopy + +from flowbber.components import Source + + +class ConfigSource(Source): + + def declare_config(self, config): + config.add_option( + 'data', + schema={ + 'type': 'dict', + 'empty': False, + }, + ) + + def collect(self): + return deepcopy(self.config.data.value) + + +__all__ = ['ConfigSource'] diff --git a/setup.py b/setup.py index a94cc83..9779ece 100755 --- a/setup.py +++ b/setup.py @@ -337,6 +337,8 @@ def find_packages(lib_directory): # CoberturaSource 'cobertura': ['pycobertura'], + # ConfigSource + 'config': [], # CPUSource 'cpu': ['psutil'], # EnvSource @@ -424,6 +426,7 @@ def find_packages(lib_directory): entry_points={ 'flowbber_plugin_sources_1_0': [ 'cobertura = flowbber.plugins.sources.cobertura:CoberturaSource', + 'config = flowbber.plugins.sources.config:ConfigSource', 'cpu = flowbber.plugins.sources.cpu:CPUSource', 'env = flowbber.plugins.sources.env:EnvSource', 'git = flowbber.plugins.sources.git:GitSource', diff --git a/test/test_examples.py b/test/test_examples.py index 27c4441..c564ad4 100644 --- a/test/test_examples.py +++ b/test/test_examples.py @@ -30,6 +30,7 @@ def setup_module(module): ['test', 'pipeline.toml'], ['lcov', 'pipeline.toml'], ['valgrind', 'pipeline.toml'], + ['config', 'pipeline.toml'], ]) def test_pipelines(name, pipelinedef): # Exceptions ...