3
3
# SPDX-License-Identifier: Apache-2.0
4
4
import re
5
5
6
- from beartype .typing import Match , Optional , Pattern
6
+ from beartype .typing import Match , Pattern
7
7
8
8
from spdx_tools .spdx .model import Actor , ActorType
9
9
from spdx_tools .spdx .parser .error import SPDXParsingError
@@ -14,8 +14,8 @@ class ActorParser:
14
14
@staticmethod
15
15
def parse_actor (actor : str ) -> Actor :
16
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 )
17
+ person_re : Pattern = re .compile (r"^Person:\s*(?:(.*) \((.*)\)|(.*))$ " , re .UNICODE )
18
+ org_re : Pattern = re .compile (r"^Organization:\s*(?:(.*) \((.*)\)|(.*))$ " , re .UNICODE )
19
19
tool_match : Match = tool_re .match (actor )
20
20
person_match : Match = person_re .match (actor )
21
21
org_match : Match = org_re .match (actor )
@@ -24,34 +24,30 @@ def parse_actor(actor: str) -> Actor:
24
24
name : str = tool_match .group (1 ).strip ()
25
25
if not name :
26
26
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 ))
28
28
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
37
32
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
45
35
else :
46
36
raise SPDXParsingError ([f"Actor { actor } doesn't match any of person, organization or tool." ])
47
37
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
+ )
55
42
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
+ )
0 commit comments