Skip to content

Commit

Permalink
dictionary key read processor
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Sep 26, 2023
1 parent 35dfd78 commit ea35fe6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
60 changes: 60 additions & 0 deletions SharedProcessors/DictionaryKeyRead.py
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions Test-Recipes/DictionaryKeyRead.test.recipe.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit ea35fe6

Please sign in to comment.