Explorar o código

Add convenience functions for loading YAML/JSON data from file

Jeremy Stretch %!s(int64=6) %!d(string=hai) anos
pai
achega
7f65e009a8
Modificáronse 1 ficheiros con 25 adicións e 0 borrados
  1. 25 0
      netbox/extras/scripts.py

+ 25 - 0
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