|
| 1 | +#!/usr/local/autopkg/python |
| 2 | +# |
| 3 | +# Copyright 2025 Nathan Felton (n8felton) |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +"""Generic processor to parse JSON into usable autopkg variables.""" |
| 17 | + |
| 18 | +import json |
| 19 | + |
| 20 | +from autopkglib import URLGetter |
| 21 | + |
| 22 | +__all__ = ["JSON"] |
| 23 | + |
| 24 | + |
| 25 | +class JSON(URLGetter): |
| 26 | + """Parse JSON into usable autopkg variables.""" |
| 27 | + |
| 28 | + description = __doc__ |
| 29 | + input_variables = { |
| 30 | + "url": {"required": True, "description": "Full URL to the JSON file"}, |
| 31 | + "keys": { |
| 32 | + "required": True, |
| 33 | + "description": "The keys you wish to parse the values from the JSON source", |
| 34 | + }, |
| 35 | + } |
| 36 | + output_variables = {} |
| 37 | + |
| 38 | + def get_json(self, json_url): |
| 39 | + """Returns the JSON file at the given URL.""" |
| 40 | + response = self.download(json_url) |
| 41 | + releases = json.loads(response) |
| 42 | + return releases |
| 43 | + |
| 44 | + def main(self): |
| 45 | + json_response = self.get_json(self.env.get("url")) |
| 46 | + for k in self.env.get("keys"): |
| 47 | + if k in json_response: |
| 48 | + self.env[k] = json_response[k] |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + PROCESSOR = JSON() |
| 53 | + PROCESSOR.execute_shell() |
0 commit comments