Coverage for oc_validator / semantics.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-30 15:46 +0000

1# ISC License 

2# 

3# Copyright (c) 2023-2026, Elia Rizzetto, Silvio Peroni 

4# 

5# Permission to use, copy, modify, and/or distribute this software for any 

6# purpose with or without fee is hereby granted, provided that the above 

7# copyright notice and this permission notice appear in all copies. 

8# 

9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 

10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 

11# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 

12# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 

13# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 

14# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 

15# PERFORMANCE OF THIS SOFTWARE. 

16 

17class Semantics: 

18 """ 

19 Validates the semantic compatibility between identifier schemes and 

20 resource types in META-CSV rows. 

21 """ 

22 

23 def __init__(self) -> None: 

24 """ 

25 Initialise the Semantics checker. 

26 

27 :rtype: None 

28 """ 

29 pass 

30 

31 def check_semantics(self, row: dict, alignment: dict) -> dict: 

32 """ 

33 Check whether all identifiers in the ``id`` field are compatible with the ``type`` value. 

34 

35 Uses an alignment dictionary that maps each resource type to the set of 

36 allowed identifier schemes. 

37 

38 :param row: A dictionary representing a single CSV row. 

39 :type row: dict 

40 :param alignment: Mapping from resource type to the set of accepted ID schemes. 

41 :type alignment: dict 

42 :return: Dictionary locating incompatible fields and items, or an empty 

43 dictionary if no semantic errors were found. 

44 :rtype: dict 

45 """ 

46 invalid_row = {} 

47 row_type = row['type'] 

48 row_ids = row['id'].split(' ') # list 

49 invalid_ids_idxs = [] 

50 

51 if row['type'] and row['id']: # apply semantic checks only if both 'id' and 'type' are not empty 

52 for id_idx, id in enumerate(row_ids): 

53 if id[:id.index(':')] not in alignment[row_type]: 

54 invalid_ids_idxs.append(id_idx) 

55 

56 if invalid_ids_idxs: 

57 invalid_row['id'] = invalid_ids_idxs 

58 invalid_row['type'] = [0] 

59 return invalid_row