From 7f65e009a8b42e2611583a5eb35ef3a80239548a Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 14 Aug 2019 13:08:21 -0400 Subject: [PATCH] Add convenience functions for loading YAML/JSON data from file --- netbox/extras/scripts.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index fac44a530..47bd8284c 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -1,7 +1,10 @@ from collections import OrderedDict import inspect +import json +import os import pkgutil import time +import yaml from django import forms from django.conf import settings @@ -196,6 +199,28 @@ class Script: def log_failure(self, message): self.log.append((LOG_FAILURE, message)) + # Convenience functions + + def load_yaml(self, filename): + """ + Return data from a YAML file + """ + file_path = os.path.join(settings.SCRIPTS_ROOT, filename) + with open(file_path, 'r') as datafile: + data = yaml.load(datafile) + + return data + + def load_json(self, filename): + """ + Return data from a JSON file + """ + file_path = os.path.join(settings.SCRIPTS_ROOT, filename) + with open(file_path, 'r') as datafile: + data = json.load(datafile) + + return data + # # Functions