From f49c004a59560afa8613cc1235629cea9e64cc26 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 4 Aug 2019 18:48:16 +0200 Subject: [PATCH] [mod] add detail error messages on asserts --- moulinette/utils/filesystem.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index b3b25bf5..c0a608d0 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -22,7 +22,7 @@ def read_file(file_path): Keyword argument: file_path -- Path to the text file """ - assert isinstance(file_path, basestring) + assert isinstance(file_path, basestring), "Error: file_path '%s' should be a string but is of type '%s' instead" % (file_path, type(file_path)) # Check file exists if not os.path.isfile(file_path): @@ -148,14 +148,14 @@ def write_to_file(file_path, data, file_mode="w"): file_mode -- Mode used when writing the file. Option meant to be used by append_to_file to avoid duplicating the code of this function. """ - assert isinstance(data, basestring) or isinstance(data, list) - assert not os.path.isdir(file_path) - assert os.path.isdir(os.path.dirname(file_path)) + assert isinstance(data, basestring) or isinstance(data, list), "Error: data '%s' should be either a string or a list but is of type '%s'" % (data, type(data)) + assert not os.path.isdir(file_path), "Error: file_path '%s' point to a dir, it should be a file" % file_path + assert os.path.isdir(os.path.dirname(file_path)), "Error: the path ('%s') base dir ('%s') is not a dir" % (file_path, os.path.dirname(file_path)) # If data is a list, check elements are strings and build a single string if not isinstance(data, basestring): for element in data: - assert isinstance(element, basestring) + assert isinstance(element, basestring), "Error: element '%s' should be a string but is of type '%s' instead" % (element, type(element)) data = '\n'.join(data) try: @@ -189,10 +189,10 @@ def write_to_json(file_path, data): """ # Assumptions - assert isinstance(file_path, basestring) - assert isinstance(data, dict) or isinstance(data, list) - assert not os.path.isdir(file_path) - assert os.path.isdir(os.path.dirname(file_path)) + assert isinstance(file_path, basestring), "Error: file_path '%s' should be a string but is of type '%s' instead" % (file_path, type(file_path)) + assert isinstance(data, dict) or isinstance(data, list), "Error: data '%s' should be a dict or a list but is of type '%s' instead" % (data, type(data)) + assert not os.path.isdir(file_path), "Error: file_path '%s' point to a dir, it should be a file" % file_path + assert os.path.isdir(os.path.dirname(file_path)), "Error: the path ('%s') base dir ('%s') is not a dir" % (file_path, os.path.dirname(file_path)) # Write dict to file try: