spdx_tools.spdx.parser.actor_parser

 1# SPDX-FileCopyrightText: 2022 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4import re
 5
 6from beartype.typing import Match, Pattern
 7
 8from spdx_tools.spdx.model import Actor, ActorType
 9from spdx_tools.spdx.parser.error import SPDXParsingError
10from spdx_tools.spdx.parser.parsing_functions import construct_or_raise_parsing_error
11
12
13class ActorParser:
14    @staticmethod
15    def parse_actor(actor: str) -> Actor:
16        tool_re: Pattern = re.compile(r"^Tool:\s*(.+)", re.UNICODE)
17        person_re: Pattern = re.compile(r"^Person:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
18        org_re: Pattern = re.compile(r"^Organization:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
19        tool_match: Match = tool_re.match(actor)
20        person_match: Match = person_re.match(actor)
21        org_match: Match = org_re.match(actor)
22
23        if tool_match:
24            name: str = tool_match.group(1).strip()
25            if not name:
26                raise SPDXParsingError([f"No name for Tool provided: {actor}."])
27            return construct_or_raise_parsing_error(Actor, dict(actor_type=ActorType.TOOL, name=name))
28
29        if person_match:
30            actor_type = ActorType.PERSON
31            match = person_match
32        elif org_match:
33            actor_type = ActorType.ORGANIZATION
34            match = org_match
35        else:
36            raise SPDXParsingError([f"Actor {actor} doesn't match any of person, organization or tool."])
37
38        if match.group(3):
39            return construct_or_raise_parsing_error(
40                Actor, dict(actor_type=actor_type, name=match.group(3).strip(), email=None)
41            )
42        else:
43            name = match.group(1)
44            if not name:
45                raise SPDXParsingError([f"No name for Actor provided: {actor}."])
46            else:
47                name = name.strip()
48
49            email = match.group(2).strip()
50
51            return construct_or_raise_parsing_error(
52                Actor, dict(actor_type=actor_type, name=name, email=email if email else None)
53            )
class ActorParser:
14class ActorParser:
15    @staticmethod
16    def parse_actor(actor: str) -> Actor:
17        tool_re: Pattern = re.compile(r"^Tool:\s*(.+)", re.UNICODE)
18        person_re: Pattern = re.compile(r"^Person:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
19        org_re: Pattern = re.compile(r"^Organization:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
20        tool_match: Match = tool_re.match(actor)
21        person_match: Match = person_re.match(actor)
22        org_match: Match = org_re.match(actor)
23
24        if tool_match:
25            name: str = tool_match.group(1).strip()
26            if not name:
27                raise SPDXParsingError([f"No name for Tool provided: {actor}."])
28            return construct_or_raise_parsing_error(Actor, dict(actor_type=ActorType.TOOL, name=name))
29
30        if person_match:
31            actor_type = ActorType.PERSON
32            match = person_match
33        elif org_match:
34            actor_type = ActorType.ORGANIZATION
35            match = org_match
36        else:
37            raise SPDXParsingError([f"Actor {actor} doesn't match any of person, organization or tool."])
38
39        if match.group(3):
40            return construct_or_raise_parsing_error(
41                Actor, dict(actor_type=actor_type, name=match.group(3).strip(), email=None)
42            )
43        else:
44            name = match.group(1)
45            if not name:
46                raise SPDXParsingError([f"No name for Actor provided: {actor}."])
47            else:
48                name = name.strip()
49
50            email = match.group(2).strip()
51
52            return construct_or_raise_parsing_error(
53                Actor, dict(actor_type=actor_type, name=name, email=email if email else None)
54            )
@staticmethod
def parse_actor(actor: str) -> spdx_tools.spdx.model.actor.Actor:
15    @staticmethod
16    def parse_actor(actor: str) -> Actor:
17        tool_re: Pattern = re.compile(r"^Tool:\s*(.+)", re.UNICODE)
18        person_re: Pattern = re.compile(r"^Person:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
19        org_re: Pattern = re.compile(r"^Organization:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
20        tool_match: Match = tool_re.match(actor)
21        person_match: Match = person_re.match(actor)
22        org_match: Match = org_re.match(actor)
23
24        if tool_match:
25            name: str = tool_match.group(1).strip()
26            if not name:
27                raise SPDXParsingError([f"No name for Tool provided: {actor}."])
28            return construct_or_raise_parsing_error(Actor, dict(actor_type=ActorType.TOOL, name=name))
29
30        if person_match:
31            actor_type = ActorType.PERSON
32            match = person_match
33        elif org_match:
34            actor_type = ActorType.ORGANIZATION
35            match = org_match
36        else:
37            raise SPDXParsingError([f"Actor {actor} doesn't match any of person, organization or tool."])
38
39        if match.group(3):
40            return construct_or_raise_parsing_error(
41                Actor, dict(actor_type=actor_type, name=match.group(3).strip(), email=None)
42            )
43        else:
44            name = match.group(1)
45            if not name:
46                raise SPDXParsingError([f"No name for Actor provided: {actor}."])
47            else:
48                name = name.strip()
49
50            email = match.group(2).strip()
51
52            return construct_or_raise_parsing_error(
53                Actor, dict(actor_type=actor_type, name=name, email=email if email else None)
54            )