spdx_tools.spdx.model.actor

 1# SPDX-FileCopyrightText: 2022 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from enum import Enum, auto
 5
 6from beartype.typing import Optional
 7
 8from spdx_tools.common.typing.dataclass_with_properties import dataclass_with_properties
 9from spdx_tools.common.typing.type_checks import check_types_and_set_values
10
11
12class ActorType(Enum):
13    PERSON = auto()
14    ORGANIZATION = auto()
15    TOOL = auto()
16
17
18@dataclass_with_properties
19class Actor:
20    actor_type: ActorType
21    name: str
22    email: Optional[str] = None
23
24    def __init__(self, actor_type: ActorType, name: str, email: Optional[str] = None):
25        check_types_and_set_values(self, locals())
26
27    def to_serialized_string(self) -> str:
28        """
29        All serialization formats use the same representation of an actor, so this method is included in the data model
30        """
31        optional_email = f" ({self.email})" if self.email else ""
32        return "".join([f"{self.actor_type.name.title()}:", f" {self.name}", optional_email])
33
34    def __str__(self):
35        return self.to_serialized_string()
class ActorType(enum.Enum):
13class ActorType(Enum):
14    PERSON = auto()
15    ORGANIZATION = auto()
16    TOOL = auto()

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access::
>>> Color.RED
<Color.RED: 1>
  • value lookup:
>>> Color(1)
<Color.RED: 1>
  • name lookup:
>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

PERSON = <ActorType.PERSON: 1>
ORGANIZATION = <ActorType.ORGANIZATION: 2>
TOOL = <ActorType.TOOL: 3>
Inherited Members
enum.Enum
name
value
@dataclass_with_properties
class Actor:
19@dataclass_with_properties
20class Actor:
21    actor_type: ActorType
22    name: str
23    email: Optional[str] = None
24
25    def __init__(self, actor_type: ActorType, name: str, email: Optional[str] = None):
26        check_types_and_set_values(self, locals())
27
28    def to_serialized_string(self) -> str:
29        """
30        All serialization formats use the same representation of an actor, so this method is included in the data model
31        """
32        optional_email = f" ({self.email})" if self.email else ""
33        return "".join([f"{self.actor_type.name.title()}:", f" {self.name}", optional_email])
34
35    def __str__(self):
36        return self.to_serialized_string()
Actor( actor_type: ActorType, name: str, email: Optional[str] = None)
25    def __init__(self, actor_type: ActorType, name: str, email: Optional[str] = None):
26        check_types_and_set_values(self, locals())
actor_type: ActorType
47    def get_field(self) -> field_type:
48        return getattr(self, f"_{field_name}")
name: str
47    def get_field(self) -> field_type:
48        return getattr(self, f"_{field_name}")
email: Optional[str]
47    def get_field(self) -> field_type:
48        return getattr(self, f"_{field_name}")
def to_serialized_string(self) -> str:
28    def to_serialized_string(self) -> str:
29        """
30        All serialization formats use the same representation of an actor, so this method is included in the data model
31        """
32        optional_email = f" ({self.email})" if self.email else ""
33        return "".join([f"{self.actor_type.name.title()}:", f" {self.name}", optional_email])

All serialization formats use the same representation of an actor, so this method is included in the data model