Skip to content

Commit 17602fc

Browse files
[issue-721] update Actor regex and parsing
Signed-off-by: Armin Tänzer <[email protected]>
1 parent 30af851 commit 17602fc

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

src/spdx_tools/spdx/parser/actor_parser.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44
import re
55

6-
from beartype.typing import Match, Optional, Pattern
6+
from beartype.typing import Match, Pattern
77

88
from spdx_tools.spdx.model import Actor, ActorType
99
from spdx_tools.spdx.parser.error import SPDXParsingError
@@ -14,8 +14,8 @@ class ActorParser:
1414
@staticmethod
1515
def parse_actor(actor: str) -> Actor:
1616
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)
17+
person_re: Pattern = re.compile(r"^Person:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
18+
org_re: Pattern = re.compile(r"^Organization:\s*(?:(.*)\((.*)\)|(.*))$", re.UNICODE)
1919
tool_match: Match = tool_re.match(actor)
2020
person_match: Match = person_re.match(actor)
2121
org_match: Match = org_re.match(actor)
@@ -24,34 +24,30 @@ def parse_actor(actor: str) -> Actor:
2424
name: str = tool_match.group(1).strip()
2525
if not name:
2626
raise SPDXParsingError([f"No name for Tool provided: {actor}."])
27-
creator = construct_or_raise_parsing_error(Actor, dict(actor_type=ActorType.TOOL, name=name))
27+
return construct_or_raise_parsing_error(Actor, dict(actor_type=ActorType.TOOL, name=name))
2828

29-
elif person_match:
30-
name: str = person_match.group(1).strip()
31-
if not name:
32-
raise SPDXParsingError([f"No name for Person provided: {actor}."])
33-
email: Optional[str] = ActorParser.get_email_or_none(person_match)
34-
creator = construct_or_raise_parsing_error(
35-
Actor, dict(actor_type=ActorType.PERSON, name=name, email=email)
36-
)
29+
if person_match:
30+
actor_type = ActorType.PERSON
31+
match = person_match
3732
elif org_match:
38-
name: str = org_match.group(1).strip()
39-
if not name:
40-
raise SPDXParsingError([f"No name for Organization provided: {actor}."])
41-
email: Optional[str] = ActorParser.get_email_or_none(org_match)
42-
creator = construct_or_raise_parsing_error(
43-
Actor, dict(actor_type=ActorType.ORGANIZATION, name=name, email=email)
44-
)
33+
actor_type = ActorType.ORGANIZATION
34+
match = org_match
4535
else:
4636
raise SPDXParsingError([f"Actor {actor} doesn't match any of person, organization or tool."])
4737

48-
return creator
49-
50-
@staticmethod
51-
def get_email_or_none(match: Match) -> Optional[str]:
52-
email_match = match.group(4)
53-
if email_match and email_match.strip():
54-
email = email_match.strip()
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+
)
5542
else:
56-
email = None
57-
return email
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+
)

tests/spdx/parser/tagvalue/test_annotation_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_parse_annotation():
5757
"not match specified grammar rule. Line: 1', 'Error while parsing "
5858
"AnnotationDate: Token did not match specified grammar rule. Line: 2']",
5959
),
60-
("Annotator: Person: ()", "Error while parsing Annotation: [['No name for Person provided: Person: ().']]"),
60+
("Annotator: Person: ()", "Error while parsing Annotation: [['No name for Actor provided: Person: ().']]"),
6161
(
6262
"AnnotationType: REVIEW",
6363
"Element Annotation is not the current element in scope, probably the "

tests/spdx/test_actor_parser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@
2121
2222
),
2323
("Organization: Example organization ( )", ActorType.ORGANIZATION, "Example organization", None),
24+
("Person: Example person ()", ActorType.PERSON, "Example person", None),
25+
("Person: Example person ", ActorType.PERSON, "Example person", None),
2426
("Tool: Example tool ", ActorType.TOOL, "Example tool", None),
27+
("Tool: Example tool ([email protected])", ActorType.TOOL, "Example tool ([email protected])", None),
28+
(
29+
"Organization: (c) Chris Sainty ([email protected])",
30+
ActorType.ORGANIZATION,
31+
"(c) Chris Sainty",
32+
33+
),
2534
],
2635
)
2736
def test_parse_actor(actor_string, expected_type, expected_name, expected_mail):
@@ -42,6 +51,8 @@ def test_parse_actor(actor_string, expected_type, expected_name, expected_mail):
4251
["Actor Perso: Jane Doe ([email protected]) doesn't match any of person, organization or tool."],
4352
),
4453
("Toole Example Tool ()", ["Actor Toole Example Tool () doesn't match any of person, organization or tool."]),
54+
("Organization:", ["No name for Actor provided: Organization:."]),
55+
("Person: ( )", ["No name for Actor provided: Person: ( )."]),
4556
],
4657
)
4758
def test_parse_invalid_actor(actor_string, expected_message):

0 commit comments

Comments
 (0)