Merge pull request #213 from YunoHost/detailed_error_messages_for_assert

[mod] add detail error messages on asserts
This commit is contained in:
Alexandre Aubin 2019-08-18 14:12:56 +02:00 committed by GitHub
commit 3878ffdc04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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: