Module: check_mk
Branch: master
Commit: 538e08f27482b9c2eea8d0b30b066ffdf8f9b14c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=538e08f27482b9…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Sep 29 08:47:37 2016 +0200
Can now save data pretty printed
---
lib/store.py | 23 +++++++++++++++++++++--
tests/lib/test_store.py | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+), 2 deletions(-)
diff --git a/lib/store.py b/lib/store.py
index 0783282..d22544e 100644
--- a/lib/store.py
+++ b/lib/store.py
@@ -34,6 +34,7 @@ import os
import pprint
import tempfile
import time
+import pprint
from .exceptions import MKGeneralException
@@ -139,6 +140,10 @@ open = LockedOpenWithTempfile
# | Loading and saving of .mk configuration files |
# '----------------------------------------------------------------------'
+# TODO: These functions could handle paths unicode > str conversion. This would make
+# the using code again shorter in some cases. It would not have to care about
+# encoding anymore.
+
# This function generalizes reading from a .mk configuration file. It is basically meant
to
# generalize the exception handling for all file IO. This function handles all those
files
# that are read with execfile().
@@ -207,8 +212,22 @@ def load_data_from_file(path, default=None, lock=False):
# 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)
+def save_data_to_file(path, data, pretty=True):
+ if pretty:
+ try:
+ formated_data = pprint.pformat(data)
+ except UnicodeDecodeError:
+ # When writing a dict with unicode keys and normal strings with garbled
+ # umlaut encoding pprint.pformat() fails with UnicodeDecodeError().
+ # example:
+ # pprint.pformat({'Z\xc3\xa4ug': 'on', 'Z\xe4ug':
'on', u'Z\xc3\xa4ugx': 'on'})
+ # Catch the exception and use repr() instead
+ formated_data = repr(data)
+ else:
+ formated_data = repr(data)
+
+ print formated_data
+ save_file(path, "%s\n" % formated_data)
# Saving assumes a locked destination file (usually done by loading code)
diff --git a/tests/lib/test_store.py b/tests/lib/test_store.py
index 1935991..e577e29 100644
--- a/tests/lib/test_store.py
+++ b/tests/lib/test_store.py
@@ -71,6 +71,40 @@ def test_save_data_to_file(tmpdir):
assert store.load_data_from_file(path) == [2, 3]
+def test_save_data_to_file_pretty(tmpdir):
+ f = tmpdir.join("test")
+ path = "%s" % f
+
+ data = {
+ "asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "1asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "2asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "3asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "4asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "5asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ }
+ store.save_data_to_file(path, data)
+ assert file(path).read().count("\n") > 4
+ assert store.load_data_from_file(path) == data
+
+
+def test_save_data_to_file_not_pretty(tmpdir):
+ f = tmpdir.join("test")
+ path = "%s" % f
+
+ data = {
+ "asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "1asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "2asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "3asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "4asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ "5asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd",
+ }
+ store.save_data_to_file(path, data, pretty=False)
+ assert file(path).read().count("\n") == 1
+ assert store.load_data_from_file(path) == data
+
+
def test_acquire_lock_not_existing(tmpdir):
store.aquire_lock("%s/asd" % tmpdir)