openpreserve / jpylyzer

JP2 (JPEG 2000 Part 1) validator and properties extractor. Jpylyzer was specifically created to check that a JP2 file really conforms to the format's specifications. Additionally jpylyzer is able to extract technical characteristics.
http://jpylyzer.openpreservation.org/
Other
69 stars 28 forks source link

Refactor main validate function and calls to it #114

Closed bitsgalore closed 5 years ago

bitsgalore commented 5 years ago

Following @stweil's comment here I noticed some weirdness with the validate function: depending on a number of conditions, the output tuple varies in length (2 or 3 elements) and contents. This makes things unnecessarily complex and hard to understand. We now have (numbers added here for reference):

        if self.isValid is not None:
            return (self.isValid, self.tests, self.characteristics)  #[1]
        elif self.returnOffset is None:
            return (self.tests, self.characteristics)  #[2]
        else:
            return (self.tests, self.characteristics, self.returnOffset) #[3]

So this is a bit of a mess. A better approach might be to rewrite the main function as:

def validate(self):
    """Generic box validation function"""
    try:
        to_call = getattr(self, "validate_" + self.boxType)
    except AttributeError:
        shared.printWarning(
            "ignoring '" + self.boxType + "' (validator function not yet implemented)")

    else:
        to_call()

    return self

And then use self.tests, self.characteristics, self.isValid, self.offsetNext and self.tilePartLength as needed.

bitsgalore commented 5 years ago

Done: https://github.com/openpreserve/jpylyzer/commit/2de7cdcf0c45ce3030a07815ae387ed723b3bbc8, https://github.com/openpreserve/jpylyzer/commit/4a400b372e32cf7e33149b896fc1fb21e7b81dd1 and https://github.com/openpreserve/jpylyzer/commit/ae4c9bcab5b82e588fccc397d9c2121c06c924a5