Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Oct 4, 2023
1 parent f779a12 commit c92ec4a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
6 changes: 4 additions & 2 deletions cyclonedx/output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ def generate(self, force_regeneration: bool = False) -> None:

@abstractmethod
def output_as_string(self, *,
indent: Optional[Union[int, str]] = None) -> str:
indent: Optional[Union[int, str]] = None,
**kwargs) -> str:
...

def output_to_file(self, filename: str, allow_overwrite: bool = False, *,
indent: Optional[Union[int, str]] = None) -> None:
indent: Optional[Union[int, str]] = None,
**kwargs) -> None:
# Check directory writable
output_filename = os.path.realpath(filename)
output_directory = os.path.dirname(output_filename)
Expand Down
3 changes: 2 additions & 1 deletion cyclonedx/output/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def generate(self, force_regeneration: bool = False) -> None:
self.generated = True

def output_as_string(self, *,
indent: Optional[Union[int, str]] = None) -> str:
indent: Optional[Union[int, str]] = None,
**kwargs) -> str:
self.generate()
return json_dumps(self._bom_json,
indent=indent)
Expand Down
12 changes: 9 additions & 3 deletions cyclonedx/output/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ def generate(self, force_regeneration: bool = False) -> None:
_view = SCHEMA_VERSIONS[self.schema_version_enum]
self.get_bom().validate()
xmlns = self.get_target_namespace()
self._bom_xml = xml_dumps(
self._bom_xml = '<?xml version="1.0" ?>\n' + xml_dumps(
self.get_bom().as_xml( # type:ignore[attr-defined]
_view, as_string=False, xmlns=xmlns),
method='xml',
encoding='unicode', xml_declaration=True,
encoding='unicode',
# `xml-declaration` is inconsistent/bugged in py38, especially on Windows it will print a non-UTF8 codepage.
# Furthermore, it might add an encoding of "utf-8" which is redundant default value of XML.
# -> so we write the declaration manually, as long as py38 is supported.
xml_declaration=False,
default_namespace=xmlns)

self.generated = True
Expand All @@ -73,7 +77,9 @@ def __make_indent(v: Optional[Union[int, str]]) -> str:
return v
return ''

def output_as_string(self, *, indent: Optional[Union[int, str]] = None) -> str:
def output_as_string(self, *,
indent: Optional[Union[int, str]] = None,
**kwargs) -> str:
self.generate()
return self._bom_xml if indent is None else dom_parseString(self._bom_xml).toprettyxml(
indent=self.__make_indent(indent)
Expand Down
2 changes: 1 addition & 1 deletion tests/_data/own/xml/1.4/indented_None.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c92ec4a

Please sign in to comment.