Skip to content

Commit 04d0552

Browse files
committed
fix(model): 🚨 add missing methods and reformat code
1 parent 93e2b0b commit 04d0552

1 file changed

Lines changed: 87 additions & 37 deletions

File tree

rocrate_validator/models.py

Lines changed: 87 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import bisect
1818
import enum
19+
import importlib
1920
import inspect
2021
import json
2122
import re
@@ -25,35 +26,43 @@
2526
from datetime import datetime, timezone
2627
from functools import total_ordering
2728
from pathlib import Path
28-
from typing import Optional, Protocol, Tuple, Union
29+
from typing import Optional, Protocol, Tuple, Type, Union
2930
from urllib.error import HTTPError
3031

3132
import enum_tools
3233
from rdflib import RDF, RDFS, Graph, Namespace, URIRef
3334

3435
from rocrate_validator import __version__
35-
from rocrate_validator.constants import (DEFAULT_HTTP_CACHE_MAX_AGE,
36-
DEFAULT_ONTOLOGY_FILE,
37-
DEFAULT_PROFILE_IDENTIFIER,
38-
DEFAULT_PROFILE_README_FILE,
39-
IGNORED_PROFILE_DIRECTORIES,
40-
JSON_OUTPUT_FORMAT_VERSION, PROF_NS,
41-
PROFILE_FILE_EXTENSIONS,
42-
PROFILE_SPECIFICATION_FILE,
43-
ROCRATE_METADATA_FILE, SCHEMA_ORG_NS)
44-
from rocrate_validator.errors import (DuplicateRequirementCheck,
45-
InvalidProfilePath, ProfileNotFound,
46-
ProfileSpecificationError,
47-
ProfileSpecificationNotFound,
48-
ROCrateMetadataNotFoundError)
36+
from rocrate_validator.constants import (
37+
DEFAULT_HTTP_CACHE_MAX_AGE,
38+
DEFAULT_ONTOLOGY_FILE,
39+
DEFAULT_PROFILE_IDENTIFIER,
40+
DEFAULT_PROFILE_README_FILE,
41+
IGNORED_PROFILE_DIRECTORIES,
42+
JSON_OUTPUT_FORMAT_VERSION,
43+
PROF_NS,
44+
PROFILE_FILE_EXTENSIONS,
45+
PROFILE_SPECIFICATION_FILE,
46+
ROCRATE_METADATA_FILE,
47+
SCHEMA_ORG_NS,
48+
)
49+
from rocrate_validator.errors import (
50+
DuplicateRequirementCheck,
51+
InvalidProfilePath,
52+
ProfileNotFound,
53+
ProfileSpecificationError,
54+
ProfileSpecificationNotFound,
55+
ROCrateMetadataNotFoundError,
56+
)
4957
from rocrate_validator.events import Event, EventType, Publisher, Subscriber
5058
from rocrate_validator.rocrate import ROCrate
5159
from rocrate_validator.utils import log as logging
5260
from rocrate_validator.utils.collections import MapIndex, MultiIndexMap
5361
from rocrate_validator.utils.http import HttpRequester
5462
from rocrate_validator.utils.paths import get_profiles_path
55-
from rocrate_validator.utils.python_helpers import \
56-
get_requirement_name_from_file
63+
from rocrate_validator.utils.python_helpers import (
64+
get_requirement_name_from_file,
65+
)
5766
from rocrate_validator.utils.uri import URI
5867

5968
# set the default profiles path
@@ -1108,9 +1117,36 @@ def to_dict(self, with_profile: bool = True, with_checks: bool = True) -> dict:
11081117
result["checks"] = [_.to_dict(with_requirement=False, with_profile=False) for _ in self._checks]
11091118
return result
11101119

1120+
@classmethod
1121+
def initialize(cls, context: ValidationContext) -> None:
1122+
logger.debug(
1123+
"Starting %s requirement initialization for context %s",
1124+
cls.__name__,
1125+
context,
1126+
)
1127+
# do initialization logic here (empty for now)
1128+
logger.debug(
1129+
"Completed %s requirement initialization for context %s",
1130+
cls.__name__,
1131+
context,
1132+
)
1133+
1134+
@classmethod
1135+
def finalize(cls, context: ValidationContext) -> None:
1136+
logger.debug(
1137+
"Starting %s requirement finalization for context %s",
1138+
cls.__name__,
1139+
context,
1140+
)
1141+
# do finalization logic here (empty for now)
1142+
logger.debug(
1143+
"Completed %s requirement finalization for context %s",
1144+
cls.__name__,
1145+
context,
1146+
)
11111147

1112-
class RequirementLoader:
11131148

1149+
class RequirementLoader:
11141150
def __init__(self, profile: Profile):
11151151
self._profile = profile
11161152

@@ -1129,7 +1165,6 @@ def __get_requirement_type__(requirement_path: Path) -> str:
11291165

11301166
@classmethod
11311167
def __get_requirement_loader__(cls, profile: Profile, requirement_path: Path) -> RequirementLoader:
1132-
import importlib
11331168
requirement_type = cls.__get_requirement_type__(requirement_path)
11341169
loader_instance_name = f"_{requirement_type}_loader_instance"
11351170
loader_instance = getattr(profile, loader_instance_name, None)
@@ -1174,16 +1209,21 @@ def load_requirements(profile: Profile, severity: Severity = None) -> list[Requi
11741209
"""
11751210
Load the requirements related to the profile
11761211
"""
1177-
def ok_file(p: Path) -> bool:
1178-
return p.is_file() \
1179-
and p.suffix in PROFILE_FILE_EXTENSIONS \
1180-
and not p.name == DEFAULT_ONTOLOGY_FILE \
1181-
and not p.name == PROFILE_SPECIFICATION_FILE \
1182-
and not p.name.startswith('.') \
1183-
and not p.name.startswith('_')
11841212

1185-
files = sorted((p for p in profile.path.rglob('*.*') if ok_file(p)),
1186-
key=lambda x: (not x.suffix == '.py', x))
1213+
def ok_file(p: Path) -> bool:
1214+
return (
1215+
p.is_file()
1216+
and p.suffix in PROFILE_FILE_EXTENSIONS
1217+
and not p.name == DEFAULT_ONTOLOGY_FILE
1218+
and not p.name == PROFILE_SPECIFICATION_FILE
1219+
and not p.name.startswith(".")
1220+
and not p.name.startswith("_")
1221+
)
1222+
1223+
files = sorted(
1224+
(p for p in profile.path.rglob("*.*") if ok_file(p)),
1225+
key=lambda x: (not x.suffix == ".py", x),
1226+
)
11871227

11881228
# set the requirement level corresponding to the severity
11891229
requirement_level = LevelCollection.get(severity.name)
@@ -1195,23 +1235,33 @@ def ok_file(p: Path) -> bool:
11951235
if requirement_level_from_path < requirement_level:
11961236
continue
11971237
except ValueError:
1198-
logger.debug("The requirement level could not be determined from the path: %s", requirement_path)
1238+
logger.debug(
1239+
"The requirement level could not be determined from the path: %s",
1240+
requirement_path,
1241+
)
11991242
requirement_loader = RequirementLoader.__get_requirement_loader__(profile, requirement_path)
12001243
for requirement in requirement_loader.load(
1201-
profile, requirement_level,
1202-
requirement_path, publicID=profile.publicID):
1244+
profile,
1245+
requirement_level,
1246+
requirement_path,
1247+
publicID=profile.publicID,
1248+
):
12031249
requirements.append(requirement)
12041250
# sort the requirements by severity
1205-
requirements = sorted(requirements,
1206-
key=lambda x: (-x.severity_from_path.value, x.path.name, x.name)
1207-
if x.severity_from_path is not None else (0, x.path.name, x.name),
1208-
reverse=False)
1251+
requirements = sorted(
1252+
requirements,
1253+
key=lambda x: (
1254+
(-x.severity_from_path.value, x.path.name, x.name)
1255+
if x.severity_from_path is not None
1256+
else (0, x.path.name, x.name)
1257+
),
1258+
reverse=False,
1259+
)
12091260
# assign order numbers to requirements
12101261
for i, requirement in enumerate(requirements):
12111262
requirement._order_number = i + 1
12121263
# log and return the requirements
1213-
logger.debug("Profile %s loaded %s requirements: %s",
1214-
profile.identifier, len(requirements), requirements)
1264+
logger.debug("Profile %s loaded %s requirements: %s", profile.identifier, len(requirements), requirements)
12151265
return requirements
12161266

12171267

0 commit comments

Comments
 (0)