summaryrefslogtreecommitdiff
path: root/python/vyos/util.py
diff options
context:
space:
mode:
authorThomas Mangin <thomas.mangin@exa.net.uk>2020-04-20 17:04:56 +0100
committerThomas Mangin <thomas.mangin@exa.net.uk>2020-04-20 17:04:56 +0100
commit1169b4298bb8bce5cb36f8fa8601d63c30abe67b (patch)
tree58fd6df3f5fc829feff4d5390a834024373cba48 /python/vyos/util.py
parentb704d0676ab2d623d2eeb1ed4dc1bcf2a2c4a5e2 (diff)
downloadvyos-1x-1169b4298bb8bce5cb36f8fa8601d63c30abe67b.tar.gz
vyos-1x-1169b4298bb8bce5cb36f8fa8601d63c30abe67b.zip
airbag: T2186: improve information reported
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r--python/vyos/util.py35
1 files changed, 30 insertions, 5 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index b1d95fbbf..7a1f39ff2 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -159,11 +159,36 @@ def call(command, flag='', shell=None, input=None, timeout=None, env=None,
return code
-def read_file(path):
- """ Read a file to string """
- with open(path, 'r') as f:
- data = f.read().strip()
- return data
+def read_file(fname, defaultonfailure=None):
+ """
+ read the content of a file, stripping any end characters (space, newlines)
+ should defaultonfailure be not None, it is returned on failure to read
+ """
+ try:
+ """ Read a file to string """
+ with open(fname, 'r') as f:
+ data = f.read().strip()
+ return data
+ except Exception as e:
+ if defaultonfailure is not None:
+ return defaultonfailure
+ raise e
+
+
+def read_json(fname, defaultonfailure=None):
+ """
+ read and json decode the content of a file
+ should defaultonfailure be not None, it is returned on failure to read
+ """
+ import json
+ try:
+ with open(fname, 'r') as f:
+ data = json.load(f)
+ return data
+ except Exception as e:
+ if defaultonfailure is not None:
+ return defaultonfailure
+ raise e
def chown(path, user, group):