From ea35fe65abe6f71866965535cc1043f4c4d99c0e Mon Sep 17 00:00:00 2001 From: jgstew Date: Tue, 26 Sep 2023 14:45:05 -0400 Subject: [PATCH] dictionary key read processor --- SharedProcessors/DictionaryKeyRead.py | 60 +++++++++++++++++++ .../DictionaryKeyRead.test.recipe.yaml | 20 +++++++ 2 files changed, 80 insertions(+) create mode 100644 SharedProcessors/DictionaryKeyRead.py create mode 100644 Test-Recipes/DictionaryKeyRead.test.recipe.yaml diff --git a/SharedProcessors/DictionaryKeyRead.py b/SharedProcessors/DictionaryKeyRead.py new file mode 100644 index 0000000..c98cd88 --- /dev/null +++ b/SharedProcessors/DictionaryKeyRead.py @@ -0,0 +1,60 @@ +#!/usr/local/autopkg/python +# +# James Stewart @JGStew - 2023 +# +"""See docstring for DictionaryKeyRead class""" + +from autopkglib import ( # pylint: disable=import-error,wrong-import-position,unused-import + Processor, + ProcessorError, +) + +__all__ = ["DictionaryKeyRead"] + + +class DictionaryKeyRead(Processor): + """Reads a key-value pair from a Python dictionary.""" + + description = __doc__ + + input_variables = { + "input_dictionary": { + "required": True, + "description": "The Python dictionary to read from.", + }, + "dictionary_key": { + "required": True, + "description": "The key in the dictionary whose value you want to read.", + }, + "output_variable": { + "required": True, + "description": "Name of the output variable to store the value.", + }, + } + + def read_dictionary_key(self, input_dictionary, dictionary_key): + try: + value = input_dictionary.get(dictionary_key) + return value + except Exception as e: + raise ProcessorError( + f"Failed to read key '{dictionary_key}' from the dictionary: {str(e)}" + ) + + def main(self): + input_dictionary = self.env.get("input_dictionary") + dictionary_key = self.env.get("dictionary_key") + output_variable = self.env.get("output_variable") + + value = self.read_dictionary_key(input_dictionary, dictionary_key) + + if value is not None: + self.output(f"Read '{dictionary_key}' from the dictionary: {value}") + self.env[output_variable] = value + else: + raise ProcessorError(f"Key '{dictionary_key}' not found in the dictionary.") + + +if __name__ == "__main__": + PROCESSOR = DictionaryKeyRead() + PROCESSOR.execute_shell() diff --git a/Test-Recipes/DictionaryKeyRead.test.recipe.yaml b/Test-Recipes/DictionaryKeyRead.test.recipe.yaml new file mode 100644 index 0000000..f3841de --- /dev/null +++ b/Test-Recipes/DictionaryKeyRead.test.recipe.yaml @@ -0,0 +1,20 @@ +--- +Description: Test TemplateDictionaryAppendInput Processor +Identifier: com.github.jgstew.test.TemplateDictionaryAppendInput +Input: + NAME: TemplateDictionaryAppendInputTest + DisplayName: TemplateDictionaryAppendInput + Template_version: "0.0.0" + Template_example: "example value" +MinimumVersion: "2.3" +Process: + - Processor: com.github.jgstew.SharedProcessors/TemplateDictionaryAppendInput + - Processor: com.github.jgstew.SharedProcessors/DictionaryKeyRead + Arguments: + input_dictionary: template_dictionary + dictionary_key: version + output_variable: version + - Processor: com.github.jgstew.SharedProcessors/AssertInputContainsString + Arguments: + input_string: "%version%" + assert_string: "0.0.0"