-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |