spdx_python_model.bindings.v3_0_1.cmd
1#! /usr/bin/env python3 2# 3# This file was auto-generated by shacl2code 1.1.0. DO NOT MANUALLY MODIFY IT 4# 5# SPDX-License-Identifier: Apache-2.0 6 7import argparse 8from pathlib import Path 9from typing import Any, Iterable, List 10 11from .model import ( 12 JSONLDDeserializer, 13 JSONLDSerializer, 14 ListProxy, 15 SHACLObject, 16 SHACLObjectSet, 17) 18 19 20def print_tree(objects: Iterable[SHACLObject], all_fields: bool = False) -> None: 21 """ 22 Print object tree 23 """ 24 seen = set() 25 26 def callback(value: Any, path: List[str]) -> bool: 27 s = (" " * (len(path) - 1)) + f"{path[-1]}" 28 if isinstance(value, SHACLObject): 29 s += f" {value} ({id(value)})" 30 is_empty = False 31 elif isinstance(value, ListProxy): 32 is_empty = len(value) == 0 33 if is_empty: 34 s += " []" 35 else: 36 s += f" {value!r}" 37 is_empty = value is None 38 39 if all_fields or not is_empty: 40 print(s) 41 42 if isinstance(value, SHACLObject): 43 if value in seen: 44 return False 45 seen.add(value) 46 return True 47 48 return True 49 50 for o in objects: 51 o.walk(callback) 52 53 54def main() -> int: 55 parser = argparse.ArgumentParser(description="Python SHACL model test") 56 parser.add_argument("infile", type=Path, help="Input file") 57 parser.add_argument("--print", action="store_true", help="Print object tree") 58 parser.add_argument("--outfile", type=Path, help="Output file") 59 60 args = parser.parse_args() 61 62 objectset = SHACLObjectSet() 63 with args.infile.open("r") as f: 64 d = JSONLDDeserializer() 65 d.read(f, objectset) 66 67 if args.print: 68 print_tree(objectset.objects) 69 70 if args.outfile: 71 with args.outfile.open("wb") as f: 72 s = JSONLDSerializer() 73 s.write(objectset, f) 74 75 return 0
def
print_tree( objects: Iterable[spdx_python_model.bindings.v3_0_1.model.SHACLObject], all_fields: bool = False) -> None:
21def print_tree(objects: Iterable[SHACLObject], all_fields: bool = False) -> None: 22 """ 23 Print object tree 24 """ 25 seen = set() 26 27 def callback(value: Any, path: List[str]) -> bool: 28 s = (" " * (len(path) - 1)) + f"{path[-1]}" 29 if isinstance(value, SHACLObject): 30 s += f" {value} ({id(value)})" 31 is_empty = False 32 elif isinstance(value, ListProxy): 33 is_empty = len(value) == 0 34 if is_empty: 35 s += " []" 36 else: 37 s += f" {value!r}" 38 is_empty = value is None 39 40 if all_fields or not is_empty: 41 print(s) 42 43 if isinstance(value, SHACLObject): 44 if value in seen: 45 return False 46 seen.add(value) 47 return True 48 49 return True 50 51 for o in objects: 52 o.walk(callback)
Print object tree
def
main() -> int:
55def main() -> int: 56 parser = argparse.ArgumentParser(description="Python SHACL model test") 57 parser.add_argument("infile", type=Path, help="Input file") 58 parser.add_argument("--print", action="store_true", help="Print object tree") 59 parser.add_argument("--outfile", type=Path, help="Output file") 60 61 args = parser.parse_args() 62 63 objectset = SHACLObjectSet() 64 with args.infile.open("r") as f: 65 d = JSONLDDeserializer() 66 d.read(f, objectset) 67 68 if args.print: 69 print_tree(objectset.objects) 70 71 if args.outfile: 72 with args.outfile.open("wb") as f: 73 s = JSONLDSerializer() 74 s.write(objectset, f) 75 76 return 0