summaryrefslogtreecommitdiff
path: root/python/vyos/util.py
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-04-22 07:38:23 +0200
committerGitHub <noreply@github.com>2020-04-22 07:38:23 +0200
commit675f400bacb03ae93be928e7270f89205d1036b9 (patch)
treeda3ba7ff0148fec1ad912f517ee6d97ca7604287 /python/vyos/util.py
parent43e606d88fd88f4d82720cb74b721903a4ebe9f3 (diff)
parent1169b4298bb8bce5cb36f8fa8601d63c30abe67b (diff)
downloadvyos-1x-675f400bacb03ae93be928e7270f89205d1036b9.tar.gz
vyos-1x-675f400bacb03ae93be928e7270f89205d1036b9.zip
Merge pull request #360 from thomas-mangin/T2186-syslog
airbag: T2186: generic syslog and better text
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 3d4f1c42f..4340332d3 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -160,11 +160,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):