|
| 1 | + |
| 2 | +# Copyright (c) 2018 Yash M. Nisar |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +from __future__ import absolute_import |
| 14 | +from __future__ import print_function |
| 15 | +from __future__ import unicode_literals |
| 16 | + |
| 17 | +from functools import total_ordering |
| 18 | + |
| 19 | + |
| 20 | +@total_ordering |
| 21 | +class ExternalDocumentRef(object): |
| 22 | + """ |
| 23 | + External Document References entity that contains the following fields : |
| 24 | + - external_document_id: A unique string containing letters, numbers, '.', |
| 25 | + '-' or '+'. |
| 26 | + - spdx_document_uri: The unique ID of the SPDX document being referenced. |
| 27 | + - check_sum: The checksum of the referenced SPDX document. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, external_document_id=None, spdx_document_uri=None, |
| 31 | + check_sum=None): |
| 32 | + self.external_document_id = external_document_id |
| 33 | + self.spdx_document_uri = spdx_document_uri |
| 34 | + self.check_sum = check_sum |
| 35 | + |
| 36 | + def __eq__(self, other): |
| 37 | + return ( |
| 38 | + isinstance(other, ExternalDocumentRef) |
| 39 | + and self.external_document_id == other.external_document_id |
| 40 | + and self.spdx_document_uri == other.spdx_document_uri |
| 41 | + and self.check_sum == other.check_sum |
| 42 | + ) |
| 43 | + |
| 44 | + def __lt__(self, other): |
| 45 | + return ( |
| 46 | + (self.external_document_id, self.spdx_document_uri, |
| 47 | + self.check_sum) < |
| 48 | + (other.external_document_id, other.spdx_document_uri, |
| 49 | + other.check_sum,) |
| 50 | + ) |
| 51 | + |
| 52 | + def validate(self, messages=None): |
| 53 | + """ |
| 54 | + Validate all fields of the ExternalDocumentRef class and update the |
| 55 | + messages list with user friendly error messages for display. |
| 56 | + """ |
| 57 | + messages = messages if messages is not None else [] |
| 58 | + |
| 59 | + return (self.validate_ext_doc_id(messages) and |
| 60 | + self.validate_spdx_doc_uri(messages) and |
| 61 | + self.validate_chksum(messages) |
| 62 | + ) |
| 63 | + |
| 64 | + def validate_ext_doc_id(self, messages=None): |
| 65 | + messages = messages if messages is not None else [] |
| 66 | + |
| 67 | + if self.external_document_id: |
| 68 | + return True |
| 69 | + else: |
| 70 | + messages.append('ExternalDocumentRef has no External Document ID.') |
| 71 | + return False |
| 72 | + |
| 73 | + def validate_spdx_doc_uri(self, messages=None): |
| 74 | + messages = messages if messages is not None else [] |
| 75 | + |
| 76 | + if self.spdx_document_uri: |
| 77 | + return True |
| 78 | + else: |
| 79 | + messages.append('ExternalDocumentRef has no SPDX Document URI.') |
| 80 | + return False |
| 81 | + |
| 82 | + def validate_chksum(self, messages=None): |
| 83 | + messages = messages if messages is not None else [] |
| 84 | + |
| 85 | + if self.check_sum: |
| 86 | + return True |
| 87 | + else: |
| 88 | + messages.append('ExternalDocumentRef has no Checksum.') |
| 89 | + return False |
0 commit comments