Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Sep 19, 2023
1 parent 646185a commit df4b329
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Get it all applied via:

```shell
poetry run isort .
poetry run autopep8 -ir cyclonedx/ tests/ typings/
poetry run autopep8 -ir cyclonedx/ tests/ typings/ examples/
```

## Documentation
Expand Down
8 changes: 1 addition & 7 deletions cyclonedx/validation/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@

_missing_deps_error: Optional[Tuple[MissingOptionalDependencyException, ImportError]] = None
try:
from lxml.etree import ( # type: ignore[import]
DocumentInvalid,
XMLParser,
XMLSchema,
fromstring as xml_fromstring,
parse as xml_parse,
)
from lxml.etree import XMLParser, XMLSchema, fromstring as xml_fromstring # type: ignore[import]
except ImportError as err:
_missing_deps_error = MissingOptionalDependencyException(
'This functionality requires optional dependencies.\n'
Expand Down
6 changes: 3 additions & 3 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
Examples
========

Build & Serialize
-----------------
Complex
-------

.. literalinclude:: ../examples/build_and_serialize.py
.. literalinclude:: ../examples/complex.py
:language: python
:linenos:
23 changes: 19 additions & 4 deletions examples/build_and_serialize.py → examples/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from cyclonedx.model.component import Component, ComponentType
from cyclonedx.output.json import JsonV1Dot4
from cyclonedx.output.xml import XmlV1Dot4
from cyclonedx.schema import SchemaVersion
from cyclonedx.validation import MissingOptionalDependencyException
from cyclonedx.validation.json import JsonValidator
from cyclonedx.validation.xml import XmlValidator
from packageurl import PackageURL

lc_factory = LicenseChoiceFactory(license_factory=LicenseFactory())
Expand Down Expand Up @@ -44,8 +48,19 @@

# endregion build the BOM

serializedJSON = JsonV1Dot4(bom).output_as_string()
print(serializedJSON)

serializedXML = XmlV1Dot4(bom).output_as_string()
print(serializedXML)
serialized_json = JsonV1Dot4(bom).output_as_string()
print(serialized_json)
try:
valid = JsonValidator(SchemaVersion.V1_4).validate_str(serialized_json)
print('JSON is', 'valid' if valid else 'invalid')
except MissingOptionalDependencyException as error:
print('JSON-validation was skipped due to', error)

serialized_xml = XmlV1Dot4(bom).output_as_string()
print(serialized_xml)
try:
valid = XmlValidator(SchemaVersion.V1_4).validate_str(serialized_xml)
print('XML is', 'valid' if valid else 'invalid')
except MissingOptionalDependencyException as error:
print('XML-validation was skipped due to', error)
1 change: 0 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.

import io
import json
import xml.etree.ElementTree
from datetime import datetime, timezone
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ commands =

[testenv:flake8]
commands =
poetry run flake8 cyclonedx/ tests/ typings/
poetry run flake8 cyclonedx/ tests/ typings/ examples/

[flake8]
## keep in sync with isort config - in `isort.cfg` file
Expand Down

0 comments on commit df4b329

Please sign in to comment.