Skip to content

Commit

Permalink
ref: refactored Imports to fallback to sanexml.
Browse files Browse the repository at this point in the history
- Added try catch blocks for import.

Signed-off-by: Jay <jaykumar20march@gmail.com>
  • Loading branch information
35C4n0r committed Aug 27, 2023
1 parent 994a7fa commit 61973d3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ install_requires =
pygments
pymaven_patch >= 0.2.8
requests >= 2.7.0
sanexml
saneyaml >= 0.6.0
spdx_tools == 0.7.0rc0
text_unidecode >= 1.0
Expand Down
5 changes: 4 additions & 1 deletion src/formattedcode/output_cyclonedx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import attr
from commoncode.cliutils import OUTPUT_GROUP
from commoncode.cliutils import PluggableCommandLineOption
from lxml import etree
try:
from lxml import etree
except ImportError:
from sanexml import etree
from plugincode.output import OutputPlugin
from plugincode.output import output_impl

Expand Down
14 changes: 10 additions & 4 deletions src/packagedcode/maven.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
from pprint import pformat

import javaproperties
import lxml
try:
from lxml import etree
except ImportError:
from sanexml import etree
from fnmatch import fnmatchcase
from packageurl import PackageURL
from pymaven import artifact
Expand Down Expand Up @@ -425,16 +428,17 @@ def __init__(self, location=None, text=None):
xml_text = analysis.unicode_text(location)
else:
xml_text = text
xml_text = xml_text.strip().strip("'").strip("\n'")
xml_text = strip_namespace(xml_text)
xml_text = xml_text.encode('utf-8')
if TRACE:
logger.debug('MavenPom.__init__: xml_text: {}'.format(xml_text))

self._pom_data = lxml.etree.fromstring(xml_text, parser=pom.POM_PARSER) # NOQA
self._pom_data = etree.fromstring(xml_text, parser=pom.POM_PARSER()) # NOQA

# collect and then remove XML comments from the XML elements tree
self.comments = self._get_comments()
lxml.etree.strip_tags(self._pom_data, lxml.etree.Comment) # NOQA
etree.strip_tags(self._pom_data, etree.Comment) # NOQA

# FIXME: we do not use a client for now.
# There are pending issues at pymaven to address this
Expand Down Expand Up @@ -753,7 +757,9 @@ def _get_comments(self, xml=None):
"""Return a list of comment texts or an empty list."""
if xml is None:
xml = self.pom_data
comments = [c.text for c in xml.xpath('//comment()')]
expression = etree.XPath('//comment()')
children = expression(xml)
comments = [c.text for c in children]
return [c.strip() for c in comments if c and c.strip()]

def _find_licenses(self):
Expand Down
5 changes: 4 additions & 1 deletion tests/formattedcode/test_output_cyclonedx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import os

import saneyaml
from lxml import etree
try:
from lxml import etree
except ImportError:
from sanexml import etree
from commoncode.testcase import FileDrivenTesting

from formattedcode.output_cyclonedx import CycloneDxComponent
Expand Down

0 comments on commit 61973d3

Please sign in to comment.