Skip to content

Commit 4fdce4e

Browse files
Make license texts YAML safe
* Makes license reference texts YAML safe * Makes matched texts YAML safe Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent 0f1c32d commit 4fdce4e

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

src/licensedcode/detection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def dict_fields(attr, value):
436436
include_text=include_text,
437437
license_text_diagnostics=license_text_diagnostics,
438438
whole_lines=whole_lines,
439+
yaml_safe=True,
439440
)
440441
)
441442

src/licensedcode/match.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from licensedcode import MAX_DIST
1818
from licensedcode import SMALL_RULE
1919
from licensedcode import query
20+
from licensedcode.models import get_yaml_safe_text
2021
from licensedcode.spans import Span
2122
from licensedcode.stopwords import STOPWORDS
2223
from licensedcode.tokenize import index_tokenizer
@@ -718,6 +719,7 @@ def itokens_hash(self, idx):
718719
# location at once to avoid reprocessing many times the original text
719720
def matched_text(
720721
self,
722+
yaml_safe=False,
721723
whole_lines=False,
722724
highlight=True,
723725
highlight_matched='{}',
@@ -744,7 +746,7 @@ def matched_text(
744746
if whole_lines and query.has_long_lines:
745747
whole_lines = False
746748

747-
return ''.join(get_full_matched_text(
749+
matched_text = ''.join(get_full_matched_text(
748750
match=self,
749751
location=query.location,
750752
query_string=query.query_string,
@@ -756,26 +758,32 @@ def matched_text(
756758
_usecache=_usecache
757759
)).rstrip()
758760

761+
if yaml_safe:
762+
return get_yaml_safe_text(text=matched_text)
763+
else:
764+
return matched_text
765+
759766
def to_dict(
760767
self,
761768
license_url_template=SCANCODE_LICENSEDB_URL,
762769
spdx_license_url=SPDX_LICENSE_URL,
763770
include_text=False,
764771
license_text_diagnostics=False,
765772
whole_lines=True,
773+
yaml_safe=False,
766774
):
767775
"""
768776
Return a "result" scan data built from a LicenseMatch object.
769777
"""
770778
matched_text = None
771779
if include_text:
772780
if license_text_diagnostics:
773-
matched_text = self.matched_text(whole_lines=False, highlight=True)
781+
matched_text = self.matched_text(whole_lines=False, highlight=True, yaml_safe=yaml_safe)
774782
else:
775783
if whole_lines:
776-
matched_text = self.matched_text(whole_lines=True, highlight=False)
784+
matched_text = self.matched_text(whole_lines=True, highlight=False, yaml_safe=yaml_safe)
777785
else:
778-
matched_text = self.matched_text(whole_lines=False, highlight=False)
786+
matched_text = self.matched_text(whole_lines=False, highlight=False, yaml_safe=yaml_safe)
779787

780788
result = {}
781789

src/licensedcode/models.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ def to_reference(self):
469469
data['scancode_url'] = self.scancode_url
470470
data['licensedb_url'] = self.licensedb_url
471471
data['spdx_url'] = self.spdx_url
472+
data['text'] = get_yaml_safe_text(text=data['text'])
472473
return data
473474

474475
def dump(self, licenses_data_dir):
@@ -1867,7 +1868,7 @@ def to_reference(self):
18671868
data['ignorable_authors'] = self.ignorable_authors
18681869
data['ignorable_urls'] = self.ignorable_urls
18691870
data['ignorable_emails'] = self.ignorable_emails
1870-
data['text'] = self.text
1871+
data['text'] = get_yaml_safe_text(text=self.text)
18711872
return data
18721873

18731874
def to_dict(self, include_text=False):
@@ -1969,6 +1970,28 @@ def as_int(num):
19691970
return num
19701971

19711972

1973+
def get_yaml_safe_text(text):
1974+
"""
1975+
Given a `text` return a YAML safe version of the text
1976+
after removing any left-leading whitespace, empty lines and
1977+
lines with only whitespace.
1978+
"""
1979+
if not text:
1980+
return
1981+
1982+
lines = text.split("\n")
1983+
safe_lines = []
1984+
1985+
for line in lines:
1986+
if line and not line.isspace():
1987+
safe_lines.append(
1988+
line.lstrip()
1989+
)
1990+
1991+
safe_text = "\n".join(safe_lines)
1992+
return safe_text
1993+
1994+
19721995
@attr.s(slots=True)
19731996
class Rule(BasicRule):
19741997
"""

0 commit comments

Comments
 (0)