1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Add convenience functions for loading YAML/JSON data from file

This commit is contained in:
Jeremy Stretch
2019-08-14 13:08:21 -04:00
parent 11e5e1c490
commit 7f65e009a8

View File

@ -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