File: contact.py

package info (click to toggle)
cyclonedx-python-lib 9.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,112 kB
  • sloc: xml: 14,752; python: 11,463; makefile: 21; sh: 16
file content (386 lines) | stat: -rw-r--r-- 11,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# This file is part of CycloneDX Python Library
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.


from typing import Any, Iterable, Optional, Union

import py_serializable as serializable
from sortedcontainers import SortedSet

from .._internal.bom_ref import bom_ref_from_str as _bom_ref_from_str
from .._internal.compare import ComparableTuple as _ComparableTuple
from ..schema.schema import SchemaVersion1Dot6
from . import XsUri
from .bom_ref import BomRef


@serializable.serializable_class
class PostalAddress:
    """
    This is our internal representation of the `postalAddressType` complex type that can be used in multiple places
    within a CycloneDX BOM document.

    .. note::
        See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.6/xml/#type_postalAddressType
    """

    def __init__(
        self, *,
        bom_ref: Optional[Union[str, BomRef]] = None,
        country: Optional[str] = None,
        region: Optional[str] = None,
        locality: Optional[str] = None,
        post_office_box_number: Optional[str] = None,
        postal_code: Optional[str] = None,
        street_address: Optional[str] = None,
    ) -> None:
        self._bom_ref = _bom_ref_from_str(bom_ref, optional=True)
        self.country = country
        self.region = region
        self.locality = locality
        self.post_office_box_number = post_office_box_number
        self.postal_code = postal_code
        self.street_address = street_address

    @property
    @serializable.json_name('bom-ref')
    @serializable.type_mapping(BomRef)
    @serializable.xml_attribute()
    @serializable.xml_name('bom-ref')
    def bom_ref(self) -> Optional[BomRef]:
        """
        An optional identifier which can be used to reference the component elsewhere in the BOM. Every bom-ref MUST be
        unique within the BOM.

        Returns:
            `BomRef`
        """
        return self._bom_ref

    @property
    @serializable.xml_sequence(10)
    def country(self) -> Optional[str]:
        """
        The country name or the two-letter ISO 3166-1 country code.

        Returns:
             `str` or `None`
        """
        return self._country

    @country.setter
    def country(self, country: Optional[str]) -> None:
        self._country = country

    @property
    @serializable.xml_sequence(20)
    def region(self) -> Optional[str]:
        """
        The region or state in the country. For example, Texas.

        Returns:
             `str` or `None`
        """
        return self._region

    @region.setter
    def region(self, region: Optional[str]) -> None:
        self._region = region

    @property
    @serializable.xml_sequence(30)
    def locality(self) -> Optional[str]:
        """
        The locality or city within the country. For example, Austin.

        Returns:
             `str` or `None`
        """
        return self._locality

    @locality.setter
    def locality(self, locality: Optional[str]) -> None:
        self._locality = locality

    @property
    @serializable.xml_sequence(40)
    def post_office_box_number(self) -> Optional[str]:
        """
        The post office box number. For example, 901.

        Returns:
             `str` or `None`
        """
        return self._post_office_box_number

    @post_office_box_number.setter
    def post_office_box_number(self, post_office_box_number: Optional[str]) -> None:
        self._post_office_box_number = post_office_box_number

    @property
    @serializable.xml_sequence(60)
    def postal_code(self) -> Optional[str]:
        """
        The postal code. For example, 78758.

        Returns:
             `str` or `None`
        """
        return self._postal_code

    @postal_code.setter
    def postal_code(self, postal_code: Optional[str]) -> None:
        self._postal_code = postal_code

    @property
    @serializable.xml_sequence(70)
    def street_address(self) -> Optional[str]:
        """
        The street address. For example, 100 Main Street.

        Returns:
             `str` or `None`
        """
        return self._street_address

    @street_address.setter
    def street_address(self, street_address: Optional[str]) -> None:
        self._street_address = street_address

    def __comparable_tuple(self) -> _ComparableTuple:
        return _ComparableTuple((
            self.country, self.region, self.locality, self.postal_code,
            self.post_office_box_number,
            self.street_address,
            None if self.bom_ref is None else self.bom_ref.value,
        ))

    def __eq__(self, other: object) -> bool:
        if isinstance(other, PostalAddress):
            return self.__comparable_tuple() == other.__comparable_tuple()
        return False

    def __lt__(self, other: Any) -> bool:
        if isinstance(other, PostalAddress):
            return self.__comparable_tuple() < other.__comparable_tuple()
        return NotImplemented

    def __hash__(self) -> int:
        return hash(self.__comparable_tuple())

    def __repr__(self) -> str:
        return f'<PostalAddress bom-ref={self.bom_ref}, street_address={self.street_address}, country={self.country}>'


@serializable.serializable_class
class OrganizationalContact:
    """
    This is our internal representation of the `organizationalContact` complex type that can be used in multiple places
    within a CycloneDX BOM document.

    .. note::
        See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.6/xml/#type_organizationalContact
    """

    def __init__(
        self, *,
        name: Optional[str] = None,
        phone: Optional[str] = None,
        email: Optional[str] = None,
    ) -> None:
        self.name = name
        self.email = email
        self.phone = phone

    @property
    @serializable.xml_sequence(1)
    @serializable.xml_string(serializable.XmlStringSerializationType.NORMALIZED_STRING)
    def name(self) -> Optional[str]:
        """
        Get the name of the contact.

        Returns:
            `str` if set else `None`
        """
        return self._name

    @name.setter
    def name(self, name: Optional[str]) -> None:
        self._name = name

    @property
    @serializable.xml_sequence(2)
    @serializable.xml_string(serializable.XmlStringSerializationType.NORMALIZED_STRING)
    def email(self) -> Optional[str]:
        """
        Get the email of the contact.

        Returns:
            `str` if set else `None`
        """
        return self._email

    @email.setter
    def email(self, email: Optional[str]) -> None:
        self._email = email

    @property
    @serializable.xml_sequence(3)
    @serializable.xml_string(serializable.XmlStringSerializationType.NORMALIZED_STRING)
    def phone(self) -> Optional[str]:
        """
        Get the phone of the contact.

        Returns:
            `str` if set else `None`
        """
        return self._phone

    @phone.setter
    def phone(self, phone: Optional[str]) -> None:
        self._phone = phone

    def __comparable_tuple(self) -> _ComparableTuple:
        return _ComparableTuple((
            self.name, self.email, self.phone
        ))

    def __eq__(self, other: object) -> bool:
        if isinstance(other, OrganizationalContact):
            return self.__comparable_tuple() == other.__comparable_tuple()
        return False

    def __lt__(self, other: Any) -> bool:
        if isinstance(other, OrganizationalContact):
            return self.__comparable_tuple() < other.__comparable_tuple()
        return NotImplemented

    def __hash__(self) -> int:
        return hash(self.__comparable_tuple())

    def __repr__(self) -> str:
        return f'<OrganizationalContact name={self.name}, email={self.email}, phone={self.phone}>'


@serializable.serializable_class
class OrganizationalEntity:
    """
    This is our internal representation of the `organizationalEntity` complex type that can be used in multiple places
    within a CycloneDX BOM document.

    .. note::
        See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.6/xml/#type_organizationalEntity
    """

    def __init__(
        self, *,
        name: Optional[str] = None,
        urls: Optional[Iterable[XsUri]] = None,
        contacts: Optional[Iterable[OrganizationalContact]] = None,
        address: Optional[PostalAddress] = None,
    ) -> None:
        self.name = name
        self.address = address
        self.urls = urls or []  # type:ignore[assignment]
        self.contacts = contacts or []  # type:ignore[assignment]

    @property
    @serializable.xml_sequence(10)
    @serializable.xml_string(serializable.XmlStringSerializationType.NORMALIZED_STRING)
    def name(self) -> Optional[str]:
        """
        Get the name of the organization.

        Returns:
            `str` if set else `None`
        """
        return self._name

    @name.setter
    def name(self, name: Optional[str]) -> None:
        self._name = name

    @property
    @serializable.view(SchemaVersion1Dot6)
    @serializable.xml_sequence(20)
    def address(self) -> Optional[PostalAddress]:
        """
        The physical address (location) of the organization.

        Returns:
            `PostalAddress` or `None`
        """
        return self._address

    @address.setter
    def address(self, address: Optional[PostalAddress]) -> None:
        self._address = address

    @property
    @serializable.json_name('url')
    @serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'url')
    @serializable.xml_sequence(30)
    def urls(self) -> 'SortedSet[XsUri]':
        """
        Get a list of URLs of the organization. Multiple URLs are allowed.

        Returns:
            Set of `XsUri`
        """
        return self._urls

    @urls.setter
    def urls(self, urls: Iterable[XsUri]) -> None:
        self._urls = SortedSet(urls)

    @property
    @serializable.json_name('contact')
    @serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'contact')
    @serializable.xml_sequence(40)
    def contacts(self) -> 'SortedSet[OrganizationalContact]':
        """
        Get a list of contact person at the organization. Multiple contacts are allowed.

        Returns:
            Set of `OrganizationalContact`
        """
        return self._contacts

    @contacts.setter
    def contacts(self, contacts: Iterable[OrganizationalContact]) -> None:
        self._contacts = SortedSet(contacts)

    def __comparable_tuple(self) -> _ComparableTuple:
        return _ComparableTuple((
            self.name, _ComparableTuple(self.urls), _ComparableTuple(self.contacts)
        ))

    def __eq__(self, other: object) -> bool:
        if isinstance(other, OrganizationalEntity):
            return self.__comparable_tuple() == other.__comparable_tuple()
        return False

    def __lt__(self, other: Any) -> bool:
        if isinstance(other, OrganizationalEntity):
            return self.__comparable_tuple() < other.__comparable_tuple()
        return NotImplemented

    def __hash__(self) -> int:
        return hash(self.__comparable_tuple())

    def __repr__(self) -> str:
        return f'<OrganizationalEntity name={self.name}>'