Skip to content
Open

dev #176

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,21 @@ pipeline {
}
}
}
stage('pyDocStyle'){
steps{
catchError(buildResult: 'SUCCESS', message: 'Did not pass all pyDocStyle tests', stageResult: 'UNSTABLE') {
sh(
label: 'Run pydocstyle',
script: 'uv run pydocstyle src/uiucprescon/imagevalidate > reports/pydocstyle-report.txt'
)
}
}
post {
always{
recordIssues(tools: [pyDocStyle(pattern: 'reports/pydocstyle-report.txt')])
}
}
}
stage('Run Doctest Tests'){
steps {
catchError(buildResult: 'SUCCESS', message: 'Doctest found issues', stageResult: 'UNSTABLE') {
Expand Down Expand Up @@ -1019,6 +1034,24 @@ pipeline {
}
}
}
stage('Run Pylint Static Analysis') {
steps{
catchError(buildResult: 'SUCCESS', message: 'Pylint found issues', stageResult: 'UNSTABLE') {
sh(
script: '''mkdir -p logs
mkdir -p reports
uv run pylint src/uiucprescon/imagevalidate -r n --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" > reports/pylint.txt
''',
label: 'Running pylint'
)
}
}
post{
always {
recordIssues(tools: [pyLint(pattern: 'reports/pylint.txt')])
}
}
}
}
post{
always{
Expand Down Expand Up @@ -1071,12 +1104,12 @@ pipeline {
if (env.CHANGE_ID){
sh(
label: 'Running Sonar Scanner',
script: "uv run pysonar -t \$token -Dsonar.projectVersion=\$VERSION -Dsonar.buildString=\"${env.BUILD_TAG}\" -Dsonar.pullrequest.key=${env.CHANGE_ID} -Dsonar.pullrequest.base=${env.CHANGE_TARGET} -Dsonar.cfamily.cache.enabled=false -Dsonar.cfamily.threads=\$(grep -c ^processor /proc/cpuinfo) -Dsonar.cfamily.build-wrapper-output=build/build_wrapper_output_directory"
script: "uv run pysonar -t \$token -Dsonar.projectVersion=\$VERSION -Dsonar.buildString=\"${env.BUILD_TAG}\" -Dsonar.pullrequest.key=${env.CHANGE_ID} -Dsonar.pullrequest.base=${env.CHANGE_TARGET} -Dsonar.cfamily.cache.enabled=false -Dsonar.cfamily.threads=\$(grep -c ^processor /proc/cpuinfo) -Dsonar.cfamily.build-wrapper-output=build/build_wrapper_output_directory -Dsonar.python.pylint.reportPaths=reports/pylint.txt"
)
} else {
sh(
label: 'Running Sonar Scanner',
script: "uv run pysonar -t \$token -Dsonar.projectVersion=\$VERSION -Dsonar.buildString=\"${env.BUILD_TAG}\" -Dsonar.branch.name=${env.BRANCH_NAME} -Dsonar.cfamily.cache.enabled=false -Dsonar.cfamily.threads=\$(grep -c ^processor /proc/cpuinfo) -Dsonar.cfamily.build-wrapper-output=build/build_wrapper_output_directory"
script: "uv run pysonar -t \$token -Dsonar.projectVersion=\$VERSION -Dsonar.buildString=\"${env.BUILD_TAG}\" -Dsonar.branch.name=${env.BRANCH_NAME} -Dsonar.cfamily.cache.enabled=false -Dsonar.cfamily.threads=\$(grep -c ^processor /proc/cpuinfo) -Dsonar.cfamily.build-wrapper-output=build/build_wrapper_output_directory -Dsonar.python.pylint.reportPaths=reports/pylint.txt"
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_project_metadata():
author = metadata['authors'][0]['name']

# The short X.Y version
version_extractor = re.compile("\d+[.]\d+[.]\d+")
version_extractor = re.compile(r"\d+[.]\d+[.]\d+")
version = version_extractor.search(metadata["version"]).group(0)
# The full version, including alpha/beta/rc tags.
release = metadata["version"]
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ norecursedirs = "build"
markers = "integration"
junit_family="xunit2"

[tool.pylint.MAIN]
init-hook = "import sys; sys.path.append('src')" # Replace 'src' with your module directory
extension-pkg-allow-list = ["py3exiv2bind.core", "uiucprescon.imagevalidate.openjp2wrap"]


[tool.cibuildwheel]
test-groups = ["test"]
test-command = "pytest {project}/tests"
Expand Down
4 changes: 3 additions & 1 deletion src/uiucprescon/imagevalidate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from .issues import IssueCategory
from .report import Report
from .profile import Profile, available_profiles, get_profile, get_profile_classes
from .profile import (
Profile, available_profiles, get_profile, get_profile_classes
)
from . import profiles

__all__ = [
Expand Down
14 changes: 10 additions & 4 deletions src/uiucprescon/imagevalidate/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class InvalidStrategy(Exception):


class AbsColorSpaceExtractor(metaclass=abc.ABCMeta):
# pylint: disable=too-few-public-methods
"""Base class for extracting the color space from an image file."""

@abc.abstractmethod
Expand All @@ -29,6 +30,7 @@ def check(self, image: str) -> str:


class ExtractColorSpace:
# pylint: disable=too-few-public-methods
"""Strategy context for extract color space from a file."""

def __init__(self, strategy: AbsColorSpaceExtractor) -> None:
Expand All @@ -55,6 +57,7 @@ def check(self, image: str) -> str:


class ColorSpaceIccDeviceModelCheck(AbsColorSpaceExtractor):
# pylint: disable=too-few-public-methods
"""Extract color space by reading the device_model tag in the ICC profile.

Useful for identifying sRGB.
Expand All @@ -74,8 +77,8 @@ def check(self, image: str) -> str:
exiv_image = py3exiv2bind.Image(image)
try:
icc = exiv_image.icc()
except py3exiv2bind.core.NoICCError:
raise InvalidStrategy("Unable to get ICC profile.")
except py3exiv2bind.core.NoICCError as error:
raise InvalidStrategy("Unable to get ICC profile.") from error

device_model = icc.get('device_model')
if not device_model or \
Expand All @@ -85,6 +88,7 @@ def check(self, image: str) -> str:


class ColorSpaceIccPrefCcmCheck(AbsColorSpaceExtractor):
# pylint: disable=too-few-public-methods
"""Extract color space from reading pref_ccm in the ICC profile header."""

def check(self, image: str) -> str:
Expand All @@ -102,8 +106,9 @@ def check(self, image: str) -> str:
try:
icc = exiv2_image.icc()
except py3exiv2bind.core.NoICCError as error:
raise InvalidStrategy("Unable to get ICC profile."
"Reason: {}".format(error))
raise InvalidStrategy(
f"Unable to get ICC profile.Reason: {error}"
) from error

pref_ccm = icc.get("pref_ccm")
if not pref_ccm or pref_ccm.value.decode("ascii").rstrip(' \0') == '':
Expand All @@ -112,6 +117,7 @@ def check(self, image: str) -> str:


class ColorSpaceOJPCheck(AbsColorSpaceExtractor):
# pylint: disable=too-few-public-methods
"""Color space extractor using openjpeg library."""

def check(self, image: str) -> str:
Expand Down
4 changes: 4 additions & 0 deletions src/uiucprescon/imagevalidate/issues.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""Module for defining issue categories related to image validation."""

from enum import Enum


class IssueCategory(Enum):
"""Enum class defining issue categories."""

VALID = 0
EMPTY_DATA = 1
MISSING_FIELD = 2
Expand Down
5 changes: 5 additions & 0 deletions src/uiucprescon/imagevalidate/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


class AbsMessage(metaclass=abc.ABCMeta):
# pylint: disable=too-few-public-methods
"""Base class for messages."""

@abc.abstractmethod
Expand All @@ -14,6 +15,7 @@ def generate_message(self, field: str, data: report.Result) -> str:


class InvalidData(AbsMessage):
# pylint: disable=too-few-public-methods
"""Invalid data."""

def generate_message(self, field: str, data: report.Result) -> str:
Expand All @@ -24,6 +26,7 @@ def generate_message(self, field: str, data: report.Result) -> str:


class EmptyData(AbsMessage):
# pylint: disable=too-few-public-methods
"""Empty data."""

def generate_message(self, field: str, data: report.Result) -> str:
Expand All @@ -32,6 +35,7 @@ def generate_message(self, field: str, data: report.Result) -> str:


class MissingField(AbsMessage):
# pylint: disable=too-few-public-methods
"""Missing fields."""

def generate_message(self, field: str, data: report.Result) -> str:
Expand All @@ -40,6 +44,7 @@ def generate_message(self, field: str, data: report.Result) -> str:


class MessageGenerator:
# pylint: disable=too-few-public-methods
"""Message Generator."""

def __init__(self, strategy: AbsMessage) -> None:
Expand Down
11 changes: 9 additions & 2 deletions src/uiucprescon/imagevalidate/profile.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""Profile for validating images."""
from __future__ import annotations

import os
import inspect
from typing import Type, Set, Dict
from uiucprescon import imagevalidate
from typing import Type, Set, Dict, TYPE_CHECKING
from . import profiles as profile_pkg

if TYPE_CHECKING:
from uiucprescon import imagevalidate

known_profiles: Dict[str, Type[profile_pkg.AbsProfile]] = {}


class Profile:
# pylint: disable=too-few-public-methods
"""Profile loader for validating embedded metadata in image files."""

def __init__(self, validation_profile: profile_pkg.AbsProfile) -> None:
Expand All @@ -33,6 +37,7 @@ def validate(self, file: str) -> imagevalidate.Report:
if not os.path.exists(file):
raise FileNotFoundError(f"Unable to locate {file}")
return self._profile.validate(file)
# pylint: enable=too-few-public-methods


def available_profiles() -> Set[str]:
Expand All @@ -55,6 +60,7 @@ def get_profile(name: str) -> profile_pkg.AbsProfile:


def get_profile_classes():
"""Get all available profiles."""
known_package_profiles: Dict[str, Type[profile_pkg.AbsProfile]] = {}
profiles = \
inspect.getmembers(
Expand All @@ -66,4 +72,5 @@ def get_profile_classes():
known_package_profiles[profile[1].profile_name()] = profile[1]
return known_package_profiles


known_profiles = get_profile_classes()
22 changes: 14 additions & 8 deletions src/uiucprescon/imagevalidate/profiles/absProfile.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
"""Abstract class for creating a profile."""

Check warning on line 1 in src/uiucprescon/imagevalidate/profiles/absProfile.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Module name "absProfile" doesn't conform to snake_case naming style

See more on https://sonarcloud.io/project/issues?id=UIUCLibrary_imagevalidate&issues=AZ6n_x8sAar5jN9gwMUM&open=AZ6n_x8sAar5jN9gwMUM&pullRequest=176
from __future__ import annotations

import abc

from typing import Dict, List, Optional, Set
from typing import Dict, List, Optional, Set, TYPE_CHECKING

import py3exiv2bind
from uiucprescon.imagevalidate import Report, IssueCategory, messages
from uiucprescon.imagevalidate import messages
from uiucprescon.imagevalidate.issues import IssueCategory
from uiucprescon.imagevalidate.report import Result, ResultCategory

if TYPE_CHECKING:
from uiucprescon.imagevalidate.report import Report


class AbsProfile(metaclass=abc.ABCMeta):
"""Base class for metadata validation.

Implement the validate method when creating new profile
"""

expected_metadata_constants: Dict[str, str] = dict()
expected_metadata_any_value: List[str] = list()
expected_metadata_constants: Dict[str, str] = {}
expected_metadata_any_value: List[str] = []
valid_extensions: Set[str] = set()

@staticmethod
Expand All @@ -38,7 +44,7 @@
def _get_metadata_static_values(cls, image: py3exiv2bind.Image) \
-> Dict[str, Result]:

data = dict()
data = {}
for key, value in cls.expected_metadata_constants.items():
data[key] = Result(
expected=value,
Expand All @@ -50,7 +56,7 @@
def _get_metadata_has_values(cls, image: py3exiv2bind.Image) -> \
Dict[str, Result]:

data = dict()
data = {}
for key in cls.expected_metadata_any_value:
data[key] = Result(
expected=ResultCategory.ANY,
Expand All @@ -75,7 +81,7 @@

return message_generator.generate_message(field, report_data)

return "Unknown error with {}".format(field)
return f"Unknown error with {field}"

@staticmethod
def analyze_data_for_issues(result: Result) -> Optional[IssueCategory]:
Expand All @@ -98,7 +104,7 @@
-> Dict[str, Result]:
"""Access data from image."""
image = py3exiv2bind.Image(filename)
data: Dict[str, Result] = dict()
data: Dict[str, Result] = {}
data.update(cls._get_metadata_has_values(image))
data.update(cls._get_metadata_static_values(image))
return data
77 changes: 77 additions & 0 deletions src/uiucprescon/imagevalidate/profiles/hathi_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""Shared values for HathiTrust profiles."""
from __future__ import annotations

import collections
import typing
from abc import ABC

from uiucprescon.imagevalidate import Report
from . import AbsProfile

if typing.TYPE_CHECKING:
from uiucprescon.imagevalidate import IssueCategory

__all__ = [
"SHARED_EXPECTED_METADATA_ANY_VALUE",
"SHARED_EXPECT_RESOLUTION_CONSTANTS"
]

SHARED_EXPECTED_METADATA_ANY_VALUE = [
'Xmp.dc.creator',

# Address
'Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiAdrExtadr',

# City
'Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiAdrCity',

# State
'Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiAdrRegion',

# Zip code
'Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiAdrPcode',

# Country
'Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiAdrCtry',

# phone number
'Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiTelWork',
]

SHARED_EXPECT_RESOLUTION_CONSTANTS = {
"Exif.Image.XResolution": "400/1",
"Exif.Image.YResolution": "400/1",
}


class AbsValidateHathiTrustProfile(AbsProfile, ABC):
"""Profile for validating files for HathiTrust."""

def validate(self, file: str) -> Report:
"""Validate the image file as a HathiTrust image.

Args:
file:
File path to an image file

Returns:
Returns a report of the results.

"""
report = Report()
report.filename = file
report_data = self.get_data_from_image(file)
report._properties = report_data

Check warning on line 64 in src/uiucprescon/imagevalidate/profiles/hathi_common.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Access to a protected member _properties of a client class

See more on https://sonarcloud.io/project/issues?id=UIUCLibrary_imagevalidate&issues=AZ6n_x9TAar5jN9gwMUN&open=AZ6n_x9TAar5jN9gwMUN&pullRequest=176

analysis: typing.Dict[IssueCategory, list] = \
collections.defaultdict(list)

for key, result in report_data.items():
issue_category = self.analyze_data_for_issues(result)
if issue_category:
message = self.generate_error_msg(issue_category, key, result)
analysis[issue_category].append(message)

report._data.update(analysis)

Check warning on line 75 in src/uiucprescon/imagevalidate/profiles/hathi_common.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Access to a protected member _data of a client class

See more on https://sonarcloud.io/project/issues?id=UIUCLibrary_imagevalidate&issues=AZ6n_x9TAar5jN9gwMUO&open=AZ6n_x9TAar5jN9gwMUO&pullRequest=176

return report
Loading
Loading