Unverified Commit 66bf06f7 authored by Maxime Lefrançois's avatar Maxime Lefrançois
Browse files

extract from ts and improved cli

parent f052403a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ markdown = "*"
pygments = "*"
beautifulsoup4 = "*"
python-docx = "*"
python-dotenv = "*"

[dev-packages]
pytest = "*"
+2 −1
Original line number Diff line number Diff line
{
    "_meta": {
        "hash": {
            "sha256": "61451871c2689e6dcf0ecb3dd9f595052940889c2d2d1a911cd67b12960f3d64"
            "sha256": "410dadbc04cc19328fce46b2bfdf8f374ece1496e0e59d83b620a4ebc257a8e4"
        },
        "pipfile-spec": 6,
        "requires": {},
@@ -445,6 +445,7 @@
                "sha256:31f23644fe2602f88ff55e1f5c79ba497e01224ee7737937930c448e4d0e24dc",
                "sha256:a8a6399716257f45be6a007360200409fce5cda2661e3dec71d23dc15f6189ab"
            ],
            "index": "pypi",
            "markers": "python_version >= '3.9'",
            "version": "==1.1.1"
        },
+125 −60
Original line number Diff line number Diff line
@@ -11,83 +11,148 @@ except ModuleNotFoundError as e:
    if __name__ == "__main__":
        import sys
        from pathlib import Path

        sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
        from saref_pypeline.pipeline import SAREFPipeline
        from saref_pypeline.constants import PipelineMode
    else:
        raise

def main():

def build_parser():
    parser = argparse.ArgumentParser(
      description="Runs the SAREF pipeline as specified in ETSI TS 103 673."
    )
    parser.add_argument(
      "mode",
      choices=[mode.value for mode in PipelineMode],
      help="Mode of operation for the SAREF pipeline."
        description="Check or document SAREF projects."
    )
    parser.add_argument(

    common_arguments = argparse.ArgumentParser(add_help=False)
    common_arguments.add_argument(
        "directory",
        nargs="?",
        default=".",
      help="Directory (default: current directory)."
        help="Project directory (default: current directory)."
    )
    parser.add_argument(
      "-e", "--no-examples",
      action="store_true",
      help="Do not check examples."
    common_arguments.add_argument(
        '--verbose',
        '-V',
        action='count',
        default=0,
        help="Increase verbosity level (default: WARNING. -V: INFO. -VV: DEBUG. -VVV TRACE)."
    )
    parser.add_argument(
      "-s", "--no-site",
      action="store_true",
      help="Do not generate the static portal."
    
    check_arguments = argparse.ArgumentParser(add_help=False)
    check_arguments.add_argument(
        "-c", "--include-clauses",
        action="append",
        default=None,
        metavar="REGEX",
        help="""Regex to select which clauses to check.
        Regexes Evaluated against the TS and clause name (e.g., "TS103673 Clause 9.7.1").
        Can be given multiple times."""
    )
    parser.add_argument(
      "-t", "--no-terms",
    check_arguments.add_argument(
        "--strict",
        action="store_true",
      help="Do not generate the static portal for terms."
        help="Enable strict check mode."
    )
    parser.add_argument(
      "-f", "--no-fetch",
      action="store_true",
      help="Do not fetch the changes for the existing sources (git fetch)."

    sources_arguments = argparse.ArgumentParser(add_help=False)
    sources_default = os.path.join("target", "sources")
    sources_arguments.add_argument(
        "-s", "--sources",
        type=str,
        default=sources_default,
        metavar="DIR",
        help=f"""Path to the folder where projects are stored/cloned (default: {sources_default} relative to the project directory)."""
    )
    sources_arguments.add_argument(
        "-p", "--include-projects",
        action="append",
        type=str,
        default=None,
        metavar="REGEX",
        help="""Regex to select which projects to process.
        Regexes are evaluated against the project name (e.g., "SAREF", "^SAREF4[A-E]").
        Can be given multiple times.
        Shall only be used on the saref-portal project."""
    )
    parser.add_argument(
      "-i", "--include",
    sources_arguments.add_argument(
        "-v", "--include-versions",
        action="append",
      help="Include specific project versions (can be provided multiple times)."
        type=str,
        default=None,
        metavar="REGEX",
        help="""Regex to select which project versions to process.
        Regexes are evaluated against the project name and version (e.g., "SAREF V4.1.1", "^SAREF4[A-E] V2.1.1").
        Shall be compatible with --include-projects. Can be given multiple times.
        Shall only be used on the saref-portal project."""
    )
    parser.add_argument(
      "-x", "--exclude",
    sources_arguments.add_argument(
        "-d", "--include-document",
        action="append", 
      help="Exclude specific project versions (can be provided multiple times)."
        default=None,
        metavar="REGEX",
        help="""Regex to select which documents to check (ontologies, examples, vocabularies, patterns).
        Evaluated against the path to the documents.
        Can be given multiple times."""
    )
    sources_arguments.add_argument(
        "--skip-fetch",
        action="store_true",
        help="Skip git fetch for source folders."
    )

    subparsers = parser.add_subparsers(dest="command", required=True)

    subparsers.add_parser(
        "clean",
        parents=[common_arguments],
        help="Delete the target folder.",
        description="Delete the target folder."
    )
    subparsers.add_parser(
        "check",
        parents=[common_arguments, sources_arguments, check_arguments],
        help="Check a SAREF project.",
        description="Check a SAREF project."
    )
    subparsers.add_parser(
        "website",
        parents=[common_arguments, sources_arguments],
        help="Generate the documentation website for a SAREF project.",
        description="Generate the documentation website for a SAREF project."
    )
    subparsers.add_parser(
        "ts",
        parents=[common_arguments, sources_arguments],
        help="Generate the Technical Specification (DOCX) for a SAREF project.",
        description="Generate the Technical Specification (DOCX) for a SAREF project."
    )
    subparsers.add_parser(
        "ts2md",
        parents=[common_arguments, sources_arguments],
        help="Extract documentation files from the Technical Specification (DOCX) of a SAREF project (experimental).",
        description="Extract documentation files from the Technical Specification (DOCX) of a SAREF project (experimental)."
    )

    return parser

def main():
    parser = build_parser()
    try:
        args = parser.parse_args()
      args.mode = PipelineMode(args.mode)
    except SystemExit:
        raise
    except:
        print("Error: Invalid arguments provided.", file=sys.stderr)
        parser.print_help()
        sys.exit(-1)

    if args.mode == PipelineMode.HELP:
      parser.print_help()
      sys.exit(0)
    
    pipeline = SAREFPipeline(
      mode=args.mode,
      directory=args.directory,
      no_examples=args.no_examples,
      no_terms=args.no_terms,
      no_site=args.no_site,
      no_fetch=args.no_fetch,
      include=args.include,
      exclude=args.exclude
    )
    with Profile() as profile:
    kwargs = vars(args)
    pipeline = SAREFPipeline(**kwargs)
    # with Profile() as profile:
    pipeline.run()
        # Stats(profile).sort_stats(SortKey.CUMULATIVE).print_stats(0.1)        
    #     # Stats(profile).sort_stats(SortKey.CUMULATIVE).print_stats(0.1)


if __name__ == "__main__":
    main()
+3 −2
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ from saref_pypeline.entities import SAREFGraphDocument
import time
from typing import TYPE_CHECKING

from saref_pypeline.utils import skip_if_filtered
from saref_pypeline.utils import filter_document, filter_clause
if TYPE_CHECKING:
    from saref_pypeline.pipeline import SAREFPipeline

@@ -52,7 +52,8 @@ class BaseGraphDocumentSHACLChecker(BaseGraphDocumentChecker):
    def update_shapes_graph(self, shapes_graph: Graph):
        pass
    
    @skip_if_filtered
    @filter_document
    @filter_clause
    def check_clause(self):
        start_time = time.time()        
        dataset = self.pipeline.dataset
+2 −2
Original line number Diff line number Diff line
@@ -7,9 +7,9 @@ if TYPE_CHECKING:

class BasePatternChecker(BaseChecker):
    
    def __init__(self, pipeline: "SAREFPipeline", project_version: SAREFProjectVersion = None, pattern_name:str = None):
    def __init__(self, caller_file:str, pipeline: "SAREFPipeline", project_version: SAREFProjectVersion = None, pattern_name:str = None):
        if not isinstance(pattern_name, str):
            raise ValueError()
        super().__init__(__file__, pipeline, project_version)
        super().__init__(caller_file, pipeline, project_version)
        self.pattern_name = pattern_name
        self.ontological_definition, self.shacl_specification = self.project_version.patterns[pattern_name]
Loading