Skip to content

Commit

Permalink
Merge pull request #24 from E-ARK-Software/refact/specifications
Browse files Browse the repository at this point in the history
REFACT: Specification types
  • Loading branch information
shsdev authored Feb 21, 2024
2 parents 6f719f4 + 09bbfb5 commit 969dfe5
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 341 deletions.
2 changes: 0 additions & 2 deletions eark_validator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@
E-ARK : Python information package validation
"""

__version__ = '1.1.1'
2 changes: 1 addition & 1 deletion eark_validator/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
E-ARK (https://e-ark4all.eu/)
Open Preservation Foundation (http://www.openpreservation.org)
See LICENSE for license information.
Author: Carl Wilson (OPF), 2016-17
Author: Carl Wilson (OPF), 2016-24
This work was funded by the European commission project funded
as grant number LC-01390244 CEF-TC-2019-3 E-ARK3 under
CONNECTING EUROPE FACILITY (CEF) - TELECOMMUNICATIONS SECTOR
Expand Down
112 changes: 112 additions & 0 deletions eark_validator/model/specifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# flake8: noqa
#
# E-ARK Validation
# Copyright (C) 2019
# All rights reserved.
#
# Licensed to the E-ARK project under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The E-ARK project licenses
# this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
"""
E-ARK : Information Package Validation
Information Package Package Details type
"""
from enum import Enum, unique
from importlib_resources import files
from typing import Dict, List, Optional

from pydantic import BaseModel, computed_field


@unique
class Level(str, Enum):
"""Enum covering information package validation statuses."""
MAY = 'MAY'
# Package has basic parse / structure problems and can't be validated
SHOULD = 'SHOULD'
# Package structure is OK
MUST = 'MUST'

@staticmethod
def from_string(level: str) -> 'Level':
"""Convert a string to a Level."""
for item in Level:
if item.value == level or item.name == level:
return item
raise ValueError(f'No Level with value {level}')

class StructuralRequirement(BaseModel):
"""Encapsulates a structural requirement."""
id: str
level: Level = Level.MUST
message: Optional[str] = None

class Requirement(BaseModel):
"""Encapsulates a requirement."""
id: str
name: str
level: Level = Level.MUST
xpath: Optional[str] = None
cardinality: Optional[str] = None

class Specification(BaseModel):
"""Stores the vital facts and figures an IP specification."""
title: str
url: Optional[str] = None
version: str
date: str
structural_requirements: List[StructuralRequirement] = []
requirements: Dict[str, List[Requirement]] = {}

@computed_field
def id(self) -> str:
"""Return the specification id."""
return self.url.split('/')[-1].split('.')[0].split('-')[-1]

@property
def sections(self) -> List[str]:
"""Return the sections in the specification."""
return list(self.requirements.keys())

@computed_field
def requirement_count(self) -> int:
"""Return the number of requirements."""
return sum([len(self.requirements[sect]) for sect in self.sections])

def section_requirements(self, section: Optional[str]=None) -> List[Requirement]:
"""Get the specification requirements, by section if offered."""
requirements = []
if section:
requirements = self.requirements[section]
else:
for sect in self.sections:
requirements += self.requirements[sect]
return requirements

def get_requirement_by_id(self, id: str) -> Optional[Requirement]:
"""Retrieve a requirement by id."""
for sect in self.sections:
req = self.get_requirement_by_sect(id, sect)
if req:
return req
return None

def get_requirement_by_sect(self, id: str, section: str) -> Optional[Requirement]:
"""Retrieve a requirement by id."""
return next((req for req in self.requirements[section] if req.id == id), None)
10 changes: 1 addition & 9 deletions eark_validator/model/validation_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@
from pydantic import BaseModel

from eark_validator.model.package_details import InformationPackage

@unique
class Level(str, Enum):
"""Enum covering information package validation statuses."""
MAY = 'MAY'
# Package has basic parse / structure problems and can't be validated
SHOULD = 'SHOULD'
# Package structure is OK
MUST = 'MUST'
from eark_validator.model.specifications import Level

@unique
class Severity(str, Enum):
Expand Down
Loading

0 comments on commit 969dfe5

Please sign in to comment.