Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code style #546

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check_codestyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Installation
run: pip install ".[codestyle]"
run: pip install ".[code_style]"
- name: Check code with isort
run: |
isort src tests --check
Expand Down
1 change: 1 addition & 0 deletions src/common/typing/constructor_type_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ConstructorTypeErrors(TypeError):
Helper class that holds a list of error messages. Intended to capture all TypeErrors encountered during a
constructor call, instead of raising only the first one.
"""

messages: List[str]

def __init__(self, messages: List[str]):
Expand Down
7 changes: 4 additions & 3 deletions src/common/typing/dataclass_with_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ def get_field_with_better_error_message(self) -> field_type:
return get_field(self)
except TypeError as err:
error_message: str = f"GetterError {self.__class__.__name__}: {err.args[0]}"
# As getters are created dynamically, their argument name is always "the return value". We replace it by the
# actual name so the error message is more helpful.
# As getters are created dynamically, their argument name is always "the return value".
# We replace it by the actual name so the error message is more helpful.
raise TypeError(
error_message.replace("the return value", field_name, 1) + f': {getattr(self, f"_{field_name}")}')
error_message.replace("the return value", field_name, 1) + f': {getattr(self, f"_{field_name}")}'
)

return get_field_with_better_error_message
41 changes: 26 additions & 15 deletions src/spdx/clitools/pyspdxtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@

@click.command()
@click.option("--infile", "-i", help="The file containing the document to be validated or converted.")
@click.option("--outfile", "-o",
help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion).")
@click.option("--version",
help='The SPDX version to be used during parsing and validation ("SPDX-2.2" or "SPDX-2.3"). Will be read from the document if not provided.',
default=None)
@click.option(
"--outfile",
"-o",
help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion).",
)
@click.option(
"--version",
help='The SPDX version to be used during parsing and validation ("SPDX-2.2" or "SPDX-2.3"). '
"Will be read from the document if not provided.",
default=None,
)
@click.option("--novalidation", is_flag=True, help="Don't validate the provided document.")
def main(infile: str, outfile: str, version: str, novalidation: bool):
"""
Expand All @@ -49,15 +55,16 @@ def main(infile: str, outfile: str, version: str, novalidation: bool):
if not version:
version = document.creation_info.spdx_version

if not version in ["SPDX-2.2", "SPDX-2.3"]:
if version not in ["SPDX-2.2", "SPDX-2.3"]:
logging.error(f"This tool only supports SPDX versions SPDX-2.2 and SPDX-2.3, but got: {version}")
sys.exit(1)

validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, version)
if validation_messages:
log_string = "\n".join(
["The document is invalid. The following issues have been found:"] +
[message.validation_message for message in validation_messages])
["The document is invalid. The following issues have been found:"]
+ [message.validation_message for message in validation_messages]
)
logging.error(log_string)
sys.exit(1)
else:
Expand All @@ -67,16 +74,20 @@ def main(infile: str, outfile: str, version: str, novalidation: bool):
write_file(document, outfile, validate=False)

except NotImplementedError as err:
logging.error(err.args[0] +
"\nPlease note that this project is currently undergoing a major refactoring and therefore missing "
"a few features which will be added in time (refer to https://github.com/spdx/tools-python/issues "
"for insights into the current status).\n"
"In the meantime, please use the current PyPI release version.")
logging.error(
err.args[0]
+ "\nPlease note that this project is currently undergoing a major refactoring and therefore missing "
"a few features which will be added in time (refer to https://github.com/spdx/tools-python/issues "
"for insights into the current status).\n"
"In the meantime, please use the current PyPI release version."
)
sys.exit(1)

except SPDXParsingError as err:
log_string = "\n".join(["There have been issues while parsing the provided document:"] +
[message for message in err.get_messages()])
log_string = "\n".join(
["There have been issues while parsing the provided document:"]
+ [message for message in err.get_messages()]
)
logging.error(log_string)
sys.exit(1)

Expand Down
4 changes: 2 additions & 2 deletions src/spdx/datetime_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def datetime_from_str(date_str: str) -> datetime:
if not isinstance(date_str, str):
raise TypeError(f"Could not convert str to datetime, invalid type: {type(date_str).__name__}")

date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ") # raises ValueError if format does not match
date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ") # raises ValueError if format does not match
return date


def datetime_to_iso_string(date: datetime) -> str:
"""
Return an ISO-8601 representation of a datetime object.
"""
return date.isoformat() + "Z"

7 changes: 3 additions & 4 deletions src/spdx/document_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
# 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.
from typing import List
from typing import Union
from typing import List, Union

from spdx.model.document import Document
from spdx.model.snippet import Snippet
from spdx.model.package import Package
from spdx.model.file import File
from spdx.model.package import Package
from spdx.model.snippet import Snippet


def get_contained_spdx_element_ids(document: Document) -> List[str]:
Expand Down
7 changes: 4 additions & 3 deletions src/spdx/jsonschema/annotation_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# 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.
from typing import Type, Any
from typing import Any, Type

from spdx.datetime_conversions import datetime_to_iso_string
from spdx.jsonschema.annotation_properties import AnnotationProperty
Expand All @@ -19,8 +19,9 @@


class AnnotationConverter(TypedConverter[Annotation]):
def _get_property_value(self, annotation: Annotation, annotation_property: AnnotationProperty,
document: Document = None) -> Any:
def _get_property_value(
self, annotation: Annotation, annotation_property: AnnotationProperty, document: Document = None
) -> Any:
if annotation_property == AnnotationProperty.ANNOTATION_DATE:
return datetime_to_iso_string(annotation.annotation_date)
elif annotation_property == AnnotationProperty.ANNOTATION_TYPE:
Expand Down
6 changes: 3 additions & 3 deletions src/spdx/jsonschema/checksum_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@


class ChecksumConverter(TypedConverter[Checksum]):

def get_data_model_type(self) -> Type[Checksum]:
return Checksum

def get_json_type(self) -> Type[JsonProperty]:
return ChecksumProperty

def _get_property_value(self, checksum: Checksum, checksum_property: ChecksumProperty,
_document: Document = None) -> str:
def _get_property_value(
self, checksum: Checksum, checksum_property: ChecksumProperty, _document: Document = None
) -> str:
if checksum_property == ChecksumProperty.ALGORITHM:
return algorithm_to_json_string(checksum.algorithm)
elif checksum_property == ChecksumProperty.CHECKSUM_VALUE:
Expand Down
7 changes: 4 additions & 3 deletions src/spdx/jsonschema/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from abc import ABC, abstractmethod
from typing import Any, Type, Dict, TypeVar, Generic
from typing import Any, Dict, Generic, Type, TypeVar

from spdx.casing_tools import snake_case_to_camel_case
from spdx.jsonschema.json_property import JsonProperty
from spdx.model.document import Document
from spdx.casing_tools import snake_case_to_camel_case

MISSING_IMPLEMENTATION_MESSAGE = "Must be implemented"

Expand Down Expand Up @@ -60,7 +60,8 @@ def convert(self, instance: T, document: Document = None) -> Dict:
if not isinstance(instance, self.get_data_model_type()):
raise TypeError(
f"Converter of type {self.__class__} can only convert objects of type "
f"{self.get_data_model_type()}. Received {type(instance)} instead.")
f"{self.get_data_model_type()}. Received {type(instance)} instead."
)
if self.requires_full_document() and not document:
raise ValueError(f"Converter of type {self.__class__} requires the full document")

Expand Down
7 changes: 4 additions & 3 deletions src/spdx/jsonschema/creation_info_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# 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.
from typing import Type, Any
from typing import Any, Type

from spdx.datetime_conversions import datetime_to_iso_string
from spdx.jsonschema.converter import TypedConverter
Expand All @@ -25,8 +25,9 @@ def get_data_model_type(self) -> Type[CreationInfo]:
def get_json_type(self) -> Type[JsonProperty]:
return CreationInfoProperty

def _get_property_value(self, creation_info: CreationInfo, creation_info_property: CreationInfoProperty,
_document: Document = None) -> Any:
def _get_property_value(
self, creation_info: CreationInfo, creation_info_property: CreationInfoProperty, _document: Document = None
) -> Any:
if creation_info_property == CreationInfoProperty.CREATED:
return datetime_to_iso_string(creation_info.created)
elif creation_info_property == CreationInfoProperty.CREATORS:
Expand Down
75 changes: 48 additions & 27 deletions src/spdx/jsonschema/document_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# 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.
from typing import Type, Any
from typing import Any, Type

from spdx.document_utils import get_contained_spdx_element_ids
from spdx.jsonschema.annotation_converter import AnnotationConverter
Expand All @@ -24,9 +24,12 @@
from spdx.jsonschema.snippet_converter import SnippetConverter
from spdx.model.document import Document
from spdx.model.relationship import RelationshipType
from spdx.model.relationship_filters import filter_by_type_and_origin, filter_by_type_and_target, \
find_package_contains_file_relationships, \
find_file_contained_by_package_relationships
from spdx.model.relationship_filters import (
filter_by_type_and_origin,
filter_by_type_and_target,
find_file_contained_by_package_relationships,
find_package_contains_file_relationships,
)


class DocumentConverter(TypedConverter[Document]):
Expand Down Expand Up @@ -60,15 +63,17 @@ def json_property_name(self, document_property: DocumentProperty) -> str:
return "SPDXID"
return super().json_property_name(document_property)

def _get_property_value(self, document: Document, document_property: DocumentProperty,
_document: Document = None) -> Any:
def _get_property_value(
self, document: Document, document_property: DocumentProperty, _document: Document = None
) -> Any:
if document_property == DocumentProperty.SPDX_ID:
return document.creation_info.spdx_id
elif document_property == DocumentProperty.ANNOTATIONS:
# annotations referencing files, packages or snippets will be added to those elements directly
element_ids = get_contained_spdx_element_ids(document)
document_annotations = filter(lambda annotation: annotation.spdx_id not in element_ids,
document.annotations)
document_annotations = filter(
lambda annotation: annotation.spdx_id not in element_ids, document.annotations
)
return [self.annotation_converter.convert(annotation) for annotation in document_annotations] or None
elif document_property == DocumentProperty.COMMENT:
return document.creation_info.document_comment
Expand All @@ -77,24 +82,34 @@ def _get_property_value(self, document: Document, document_property: DocumentPro
elif document_property == DocumentProperty.DATA_LICENSE:
return document.creation_info.data_license
elif document_property == DocumentProperty.EXTERNAL_DOCUMENT_REFS:
return [self.external_document_ref_converter.convert(external_document_ref) for
external_document_ref in document.creation_info.external_document_refs] or None
return [
self.external_document_ref_converter.convert(external_document_ref)
for external_document_ref in document.creation_info.external_document_refs
] or None
elif document_property == DocumentProperty.HAS_EXTRACTED_LICENSING_INFOS:
return [self.extracted_licensing_info_converter.convert(licensing_info) for licensing_info in
document.extracted_licensing_info] or None
return [
self.extracted_licensing_info_converter.convert(licensing_info)
for licensing_info in document.extracted_licensing_info
] or None
elif document_property == DocumentProperty.NAME:
return document.creation_info.name
elif document_property == DocumentProperty.SPDX_VERSION:
return document.creation_info.spdx_version
elif document_property == DocumentProperty.DOCUMENT_NAMESPACE:
return document.creation_info.document_namespace
elif document_property == DocumentProperty.DOCUMENT_DESCRIBES:
describes_ids = [relationship.related_spdx_element_id for relationship in
filter_by_type_and_origin(document.relationships, RelationshipType.DESCRIBES,
document.creation_info.spdx_id)]
described_by_ids = [relationship.spdx_element_id for relationship in
filter_by_type_and_target(document.relationships, RelationshipType.DESCRIBED_BY,
document.creation_info.spdx_id)]
describes_ids = [
relationship.related_spdx_element_id
for relationship in filter_by_type_and_origin(
document.relationships, RelationshipType.DESCRIBES, document.creation_info.spdx_id
)
]
described_by_ids = [
relationship.spdx_element_id
for relationship in filter_by_type_and_target(
document.relationships, RelationshipType.DESCRIBED_BY, document.creation_info.spdx_id
)
]
return describes_ids + described_by_ids or None
elif document_property == DocumentProperty.PACKAGES:
return [self.package_converter.convert(package, document) for package in document.packages] or None
Expand All @@ -103,16 +118,22 @@ def _get_property_value(self, document: Document, document_property: DocumentPro
elif document_property == DocumentProperty.SNIPPETS:
return [self.snippet_converter.convert(snippet, document) for snippet in document.snippets] or None
elif document_property == DocumentProperty.RELATIONSHIPS:
already_covered_relationships = filter_by_type_and_origin(document.relationships,
RelationshipType.DESCRIBES,
document.creation_info.spdx_id)
already_covered_relationships = filter_by_type_and_origin(
document.relationships, RelationshipType.DESCRIBES, document.creation_info.spdx_id
)
already_covered_relationships.extend(
filter_by_type_and_target(document.relationships, RelationshipType.DESCRIBED_BY,
document.creation_info.spdx_id))
filter_by_type_and_target(
document.relationships, RelationshipType.DESCRIBED_BY, document.creation_info.spdx_id
)
)
for package in document.packages:
already_covered_relationships.extend(find_package_contains_file_relationships(document, package))
already_covered_relationships.extend(find_file_contained_by_package_relationships(document, package))
relationships_to_ignore = [relationship for relationship in already_covered_relationships if
relationship.comment is None]
return [self.relationship_converter.convert(relationship) for relationship in document.relationships if
relationship not in relationships_to_ignore] or None
relationships_to_ignore = [
relationship for relationship in already_covered_relationships if relationship.comment is None
]
return [
self.relationship_converter.convert(relationship)
for relationship in document.relationships
if relationship not in relationships_to_ignore
] or None
11 changes: 7 additions & 4 deletions src/spdx/jsonschema/external_document_ref_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# 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.
from typing import Type, Any
from typing import Any, Type

from spdx.jsonschema.checksum_converter import ChecksumConverter
from spdx.jsonschema.converter import TypedConverter
Expand All @@ -24,9 +24,12 @@ class ExternalDocumentRefConverter(TypedConverter[ExternalDocumentRef]):
def __init__(self):
self.checksum_converter = ChecksumConverter()

def _get_property_value(self, external_document_ref: ExternalDocumentRef,
external_document_ref_property: ExternalDocumentRefProperty,
_document: Document = None) -> Any:
def _get_property_value(
self,
external_document_ref: ExternalDocumentRef,
external_document_ref_property: ExternalDocumentRefProperty,
_document: Document = None,
) -> Any:
if external_document_ref_property == ExternalDocumentRefProperty.EXTERNAL_DOCUMENT_ID:
return external_document_ref.document_ref_id
elif external_document_ref_property == ExternalDocumentRefProperty.SPDX_DOCUMENT:
Expand Down
10 changes: 7 additions & 3 deletions src/spdx/jsonschema/external_package_ref_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# 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.
from typing import Type, Any
from typing import Any, Type

from spdx.jsonschema.converter import TypedConverter
from spdx.jsonschema.external_package_ref_properties import ExternalPackageRefProperty
Expand All @@ -18,8 +18,12 @@


class ExternalPackageRefConverter(TypedConverter[ExternalPackageRef]):
def _get_property_value(self, external_ref: ExternalPackageRef, external_ref_property: ExternalPackageRefProperty,
document: Document = None) -> Any:
def _get_property_value(
self,
external_ref: ExternalPackageRef,
external_ref_property: ExternalPackageRefProperty,
document: Document = None,
) -> Any:
if external_ref_property == ExternalPackageRefProperty.COMMENT:
return external_ref.comment
elif external_ref_property == ExternalPackageRefProperty.REFERENCE_CATEGORY:
Expand Down
Loading