Skip to content

Commit

Permalink
Change from show_warnings/show_errors to log_warnings/log_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zimeon committed Oct 14, 2024
1 parent 8e8f0be commit 8945be5
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 73 deletions.
16 changes: 8 additions & 8 deletions ocfl-validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,33 @@ def do_validation(args):
num = 0
num_good = 0
num_paths = len(args.path)
show_warnings = not args.quiet and not args.very_quiet
show_errors = not args.very_quiet
log_warnings = not args.quiet and not args.very_quiet
log_errors = not args.very_quiet
for path in args.path:
num += 1
path_type = ocfl.find_path_type(path)
if path_type == 'object':
logging.debug("Validating OCFL Object at %s", path)
obj = ocfl.Object(lax_digests=args.lax_digests)
if validate_object(obj, path,
show_warnings=show_warnings,
show_errors=show_errors,
log_warnings=log_warnings,
log_errors=log_errors,
check_digests=not args.no_check_digests):
num_good += 1
elif path_type == 'root':
logging.debug("Validating OCFL Storage Root at %s", path)
store = ocfl.StorageRoot(root=path,
lax_digests=args.lax_digests)
if store.validate(show_warnings=show_warnings,
show_errors=show_errors,
if store.validate(log_warnings=log_warnings,
log_errors=log_errors,
check_digests=not args.no_check_digests):
num_good += 1
elif path_type == 'file':
logging.debug("Validating separate OCFL Inventory at %s", path)
obj = ocfl.Object(lax_digests=args.lax_digests)
if validate_object_inventory(obj, path,
show_warnings=show_warnings,
show_errors=show_errors):
log_warnings=log_warnings,
log_errors=log_errors):
num_good += 1
else:
print("Bad path %s (%s)", path, path_type)
Expand Down
2 changes: 1 addition & 1 deletion ocfl.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def validate(store, args):
check_digests - True to check all digests for each object
max_error - Maximum number of error to show
"""
valid = store.validate(show_warnings=not args.quiet,
valid = store.validate(log_warnings=not args.quiet,
validate_objects=args.validate_objects,
check_digests=args.check_digests,
max_errors=args.max_errors)
Expand Down
24 changes: 12 additions & 12 deletions ocfl/command_line_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def check_verbosity_args(args):
logging.basicConfig(level=level)


def validate_object(obj, objdir, show_warnings=True,
show_errors=True, check_digests=True):
def validate_object(obj, objdir, log_warnings=True,
log_errors=True, check_digests=True):
"""Validate object with control of console output.
Arguments:
Expand All @@ -117,14 +117,14 @@ def validate_object(obj, objdir, show_warnings=True,
Returns True if passed validation, False if failed.
Depending on the settings show_warnings and show_errors, will write
Depending on the settings log_warnings and log_errors, will write
to stdout warning and error codes/messages encountered during
validation.
"""
passed, validator = obj.validate(objdir=objdir,
show_warnings=show_warnings,
show_errors=show_errors,
log_warnings=log_warnings,
log_errors=log_errors,
check_digests=check_digests)
messages = str(validator)
if messages != '':
Expand All @@ -134,28 +134,28 @@ def validate_object(obj, objdir, show_warnings=True,
return passed


def validate_object_inventory(obj, path, show_warnings=True,
show_errors=True, force_spec_version=None):
def validate_object_inventory(obj, path, log_warnings=True,
log_errors=True, force_spec_version=None):
"""Validate just an Object inventory at path with control of console output.
Arguments:
obj: Object() instance
path: path of inventory file
show_warnings: bool, True to log warnings
show_errors: bool, True to log errors
log_warnings: bool, True to log warnings
log_errors: bool, True to log errors
force_spec_version: None to read specification version from
inventory; or specific number to force validation against
that specification version
Returns True if passed validation, False if failed.
Depending on the settings show_warnings and show_errors, will write
Depending on the settings log_warnings and log_errors, will write
to stdout warning and error codes/messages encountered during
validation.
"""
passed, validator = obj.validate_inventory(path,
show_warnings=show_warnings,
show_errors=show_errors,
log_warnings=log_warnings,
log_errors=log_errors,
force_spec_version=force_spec_version)
messages = str(validator)
if messages != '':
Expand Down
22 changes: 11 additions & 11 deletions ocfl/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ def _show_indent(level, last=False, last_v=False):
return tree_last if last else tree_next
return (tree_indent if last else tree_pass) + (tree_last if last_v else tree_next)

validator = Validator(show_warnings=False,
show_errors=True,
validator = Validator(log_warnings=False,
log_errors=True,
check_digests=False,
lax_digests=self.lax_digests)
passed = validator.validate_object(objdir)
Expand Down Expand Up @@ -550,31 +550,31 @@ def _show_indent(level, last=False, last_v=False):
tree += _show_indent(1, last, (nn == len(v_notes))) + v_note + "\n"
return tree

def validate(self, objdir=None, show_warnings=True, show_errors=True, check_digests=True):
def validate(self, objdir=None, log_warnings=True, log_errors=True, check_digests=True):
"""Validate OCFL object at objdir.
Returns tuple (passed, validator) where:
passed: True is validation passed, False otherwise.
validator: Validator object used for validation. State records
validation history including validator.messages
"""
validator = Validator(show_warnings=show_warnings,
show_errors=show_errors,
validator = Validator(log_warnings=log_warnings,
log_errors=log_errors,
check_digests=check_digests,
lax_digests=self.lax_digests)
if objdir is None:
objdir = self.obj_fs
passed = validator.validate_object(objdir)
return passed, validator

def validate_inventory(self, path, show_warnings=True,
show_errors=True, force_spec_version=None):
def validate_inventory(self, path, log_warnings=True,
log_errors=True, force_spec_version=None):
"""Validate just an OCFL Object inventory at path.
Arguments:
path: path of inventory file
show_warnings: bool, True to log warnings
show_errors: bool, True to log errors
log_warnings: bool, True to log warnings
log_errors: bool, True to log errors
force_spec_version: None to read specification version from
inventory; or specific number to force validation against
that specification version
Expand All @@ -584,8 +584,8 @@ def validate_inventory(self, path, show_warnings=True,
validator: Validator object with state that records validation
log and results
"""
validator = Validator(show_warnings=show_warnings,
show_errors=show_errors)
validator = Validator(log_warnings=log_warnings,
log_errors=log_errors)
try:
(inv_dir, inv_file) = fs.path.split(path)
validator.obj_fs = open_fs(inv_dir, create=False)
Expand Down
12 changes: 6 additions & 6 deletions ocfl/storage_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def list_objects(self):
# FIXME - maybe do some more stuff in here

def validate_hierarchy(self, validate_objects=True, check_digests=True,
show_warnings=False, max_errors=100):
log_warnings=False, max_errors=100):
"""Validate storage root hierarchy and, optionally, all objects.
Returns:
Expand All @@ -311,23 +311,23 @@ def validate_hierarchy(self, validate_objects=True, check_digests=True,
if validate_objects:
validator = Validator(check_digests=check_digests,
lax_digests=self.lax_digests,
show_warnings=show_warnings)
log_warnings=log_warnings)
# FIXME - Should check that all objest are not higher spec
# version that storage root https://ocfl.io/1.1/spec/#E081
if validator.validate_object(ocfl_opendir(self.root_fs, dirpath)):
good_objects += 1
else:
logging.debug("Object at %s in INVALID", dirpath)
if len(errors) < max_errors:
# Record detail of errors (and warnings if show_warnings)
# Record detail of errors (and warnings if log_warnings)
messages = validator.status_str(prefix='[[' + dirpath + ']]')
if messages != '':
errors.append([dirpath, messages])
num_objects += 1
return num_objects, good_objects, errors

def validate(self, validate_objects=True, check_digests=True,
show_warnings=False, show_errors=True, max_errors=100,
log_warnings=False, log_errors=True, max_errors=100,
lang='en'):
"""Validate OCFL storage root, structure, and optionally all objects.
Expand All @@ -343,15 +343,15 @@ def validate(self, validate_objects=True, check_digests=True,
"""
valid = True
self.structure_error = None
self.log = ValidationLogger(show_warnings=show_warnings, show_errors=show_errors, lang=lang)
self.log = ValidationLogger(log_warnings=log_warnings, log_errors=log_errors, lang=lang)
self.open_root_fs()
try:
self.check_root_structure()
except StorageRootException as e:
valid = False
self.structure_error = str(e)
logging.debug("Storage root structure is INVALID (%s)", str(e))
self.num_objects, self.good_objects, self.errors = self.validate_hierarchy(validate_objects=validate_objects, check_digests=check_digests, show_warnings=show_warnings, max_errors=max_errors)
self.num_objects, self.good_objects, self.errors = self.validate_hierarchy(validate_objects=validate_objects, check_digests=check_digests, log_warnings=log_warnings, max_errors=max_errors)
if self.num_traversal_errors > 0:
valid = False
return valid
Expand Down
31 changes: 19 additions & 12 deletions ocfl/validation_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@


class ValidationLogger():
"""Class for OCFL ValidationLogger."""
"""Class for OCFL ValidationLogger.
All validation issues to be recorded are either errors (corresponding with
MUST etc. in the specification) or warnings (corresponding with SHOULD etc.
in the specification).
"""

validation_codes = None

def __init__(self, show_warnings=False, show_errors=True,
def __init__(self, log_warnings=False, log_errors=True,
lang='en', validation_codes=None):
"""Initialize OCFL validation logger."""
self.show_warnings = show_warnings
self.show_errors = show_errors
self.log_warnings = log_warnings
self.log_errors = log_errors
self.lang = lang
self.codes = {}
self.messages = []
Expand All @@ -51,21 +56,23 @@ def __init__(self, show_warnings=False, show_errors=True,
with open(os.path.join(os.path.dirname(__file__), 'data/validation-errors.json'), 'r', encoding="utf-8") as fh:
self.validation_codes = json.load(fh)

def error_or_warning(self, code, severity='error', **args):
"""Add error or warning to self.codes.
def log(self, code, is_error, **args):
"""Log either an error or a warning.
Arguments:
code: string for the error code (starts with `E` or `W`).
severity: string for severity, `error` (default) or `warning`.
is_error: boolean, True for an error, False for a wanring.
**args: additional arguments that correspond with named arguments
in the error messages.
Adds or updates the `codes` attribute with the last message for the given
code.
Adds to the log of all messages in the `messages` atttibute depending on
severity and the values of the `show_warnings` and `show_errors` attributes.
Adds to the log of all messages in the `messages` atttibute depending
whether we are dealing with an error or a warning, and the values of
the `log_warnings` and `log_errors` attributes.
"""
severity = "error" if is_error else "warning"
if code in self.validation_codes and 'description' in self.validation_codes[code]:
desc = self.validation_codes[code]['description']
lang_desc = None
Expand Down Expand Up @@ -96,17 +103,17 @@ def error_or_warning(self, code, severity='error', **args):
message += ' (see ' + self.spec + '#' + m.group(1) + ')'
# Store set of codes with last message for that code, and _full_ list of messages
self.codes[code] = message
if (severity == 'error' and self.show_errors) or (severity != 'error' and self.show_warnings):
if (is_error and self.log_errors) or (not is_error and self.log_warnings):
self.messages.append(message)

def error(self, code, **args):
"""Log an error."""
self.error_or_warning(code, severity='error', **args)
self.log(code, is_error=True, **args)
self.num_errors += 1

def warning(self, code, **args):
"""Log a warning."""
self.error_or_warning(code, severity='warning', **args)
self.log(code, is_error=False, **args)
self.num_warnings += 1

def status_str(self, prefix=''):
Expand Down
10 changes: 5 additions & 5 deletions ocfl/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class ValidatorAbortException(Exception):
class Validator():
"""Class for OCFL Object Validator."""

def __init__(self, show_warnings=False, show_errors=True,
def __init__(self, log_warnings=False, log_errors=True,
check_digests=True, lax_digests=False,
force_spec_version=None, default_spec_version='1.1',
log=None, lang='en'):
"""Initialize OCFL Object validator object.
Arguments:
show_warnings: True to record warnings during validation
log_warnings: True to record warnings during validation
(default: False)
show_errors: True to record errors during validation
log_errors: True to record errors during validation
(default: True)
check_digests: True to check digests of files within the objects
lax_digests: default is False. Set True to allow digests beyond
Expand All @@ -60,8 +60,8 @@ def __init__(self, show_warnings=False, show_errors=True,
self.default_spec_version = default_spec_version
self.log = log
if self.log is None:
self.log = ValidationLogger(show_warnings=show_warnings,
show_errors=show_errors,
self.log = ValidationLogger(log_warnings=log_warnings,
log_errors=log_errors,
lang=lang)
self.registered_extensions = [
'0001-digest-algorithms', '0002-flat-direct-storage-layout',
Expand Down
12 changes: 6 additions & 6 deletions tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,22 @@ def test_validate_inventory(self):
(passed, validator) = oo.validate_inventory(path='fixtures/1.1/good-objects/minimal_one_version_one_file/inventory.json')
self.assertTrue(passed)
self.assertEqual(validator.status_str(), '')
# Error case, first with default show_errors=True, then with
# explcit show_errors=False
# Error case, first with default log_errors=True, then with
# explcit log_errors=False
(passed, validator) = oo.validate_inventory(path='tests/testdata/i_do_not_exist')
self.assertFalse(passed)
self.assertIn("[E033]", validator.status_str())
(passed, validator) = oo.validate_inventory(path='tests/testdata/i_do_not_exist',
show_errors=False)
log_errors=False)
self.assertFalse(passed)
self.assertNotIn("[E033]", validator.status_str())
# Warning case, first with default show_warnings=True, then with
# explicit show_warnings=False
# Warning case, first with default log_warnings=True, then with
# explicit log_warnings=False
(passed, validator) = oo.validate_inventory(path='fixtures/1.1/warn-objects/W001_zero_padded_versions/inventory.json')
self.assertTrue(passed)
self.assertIn("[W001]", validator.status_str())
(passed, validator) = oo.validate_inventory(path='fixtures/1.1/warn-objects/W001_zero_padded_versions/inventory.json',
show_warnings=False)
log_warnings=False)
self.assertTrue(passed)
self.assertNotIn("[W001]", validator.status_str())

Expand Down
Loading

0 comments on commit 8945be5

Please sign in to comment.