diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-04-22 07:38:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-22 07:38:23 +0200 |
commit | 675f400bacb03ae93be928e7270f89205d1036b9 (patch) | |
tree | da3ba7ff0148fec1ad912f517ee6d97ca7604287 /python/vyos/util.py | |
parent | 43e606d88fd88f4d82720cb74b721903a4ebe9f3 (diff) | |
parent | 1169b4298bb8bce5cb36f8fa8601d63c30abe67b (diff) | |
download | vyos-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.py | 35 |
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): |