Module: check_mk
Branch: master
Commit: f5a47739fd53934a42019bc00f2843c80a2112a3
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f5a47739fd5393…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Sep 28 17:20:44 2016 +0200
store: Added general method to deal with .mk files containing python data structures
---
lib/store.py | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/lib/store.py b/lib/store.py
index dea4bce..bc92d2b 100644
--- a/lib/store.py
+++ b/lib/store.py
@@ -28,6 +28,7 @@
functionality is the locked file opening realized with the File() context
manager."""
+import ast
import fcntl
import os
import pprint
@@ -139,7 +140,8 @@ open = LockedOpenWithTempfile
# '----------------------------------------------------------------------'
# This function generalizes reading from a .mk configuration file. It is basically meant
to
-# generalize the exception handling for all file IO.
+# generalize the exception handling for all file IO. This function handles all those
files
+# that are read with execfile().
def load_mk_file(path, default=None, lock=False):
if default == None:
raise MKGeneralException(_("You need to provide a config dictionary to merge
with the "
@@ -176,6 +178,32 @@ def save_mk_file(path, mk_content):
save_file(path, content)
+# Handle .mk files that are only holding a python data structure and often
+# directly read via file/open and then parsed using eval.
+# TODO: Consolidate with load_mk_file?
+def load_data_from_file(path, default=None, lock=False):
+ if lock:
+ aquire_lock(path)
+
+ try:
+ return ast.literal_eval(file(path).read())
+ except IOError, e:
+ if e.errno == 2: # IOError: [Errno 2] No such file or directory
+ return default
+ else:
+ raise
+
+ except Exception, e:
+ # TODO: How to handle debug mode or logging?
+ raise MKGeneralException(_("Cannot read file \"%s\": %s") %
(path, e))
+
+
+# A simple wrapper for cases where you want to store a python data
+# structure that is then read by load_data_from_file() again
+def save_data_to_file(path, data):
+ save_file(path, "%r\n" % data)
+
+
# Saving assumes a locked destination file (usually done by loading code)
# Then the new file is written to a temporary file and moved to the target path
def save_file(path, content, mode=0660):