Skip to content

Commit 4be3af6

Browse files
Add support for writing and parsing Snippet information.
Signed-off-by: Xavier Figueroa <[email protected]>
1 parent a5f435d commit 4be3af6

File tree

2 files changed

+176
-1
lines changed

2 files changed

+176
-1
lines changed

spdx/parsers/jsonyaml.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,147 @@ def parse_annotation_id(self, annotation_id):
389389
else:
390390
self.value_error('ANNOTATION_ID', annotation_id)
391391

392+
class SnippetParser(BaseParser):
393+
def __init__(self, builder, logger):
394+
super(SnippetParser, self).__init__(builder, logger)
395+
396+
def parse_snippets(self, snippets):
397+
"""
398+
Responsible for parsing Snippet Information fields
399+
- snippets: Python list with Snippet Information dicts in it
400+
"""
401+
if isinstance(snippets, list):
402+
for snippet in snippets:
403+
if isinstance(snippet, dict):
404+
if self.parse_snippet_id(snippet.get('id')):
405+
self.parse_snippet_name(snippet.get('name'))
406+
self.parse_snippet_comment(snippet.get('comment'))
407+
self.parse_snippet_copyright(snippet.get('copyrightText'))
408+
self.parse_snippet_license_comment(snippet.get('licenseComments'))
409+
self.parse_snippet_file_spdxid(snippet.get('fileId'))
410+
self.parse_snippet_concluded_license(snippet.get('licenseConcluded'))
411+
self.parse_snippet_license_info_from_snippet(snippet.get('licenseInfoFromSnippet'))
412+
else:
413+
self.value_error('SNIPPET', snippet)
414+
415+
def parse_snippet_id(self, snippet_id):
416+
"""
417+
Responsible for parsing Snippet id
418+
- snippet_id: Python str/unicode
419+
"""
420+
if isinstance(snippet_id, six.string_types):
421+
try:
422+
return self.builder.create_snippet(self.document, snippet_id)
423+
except SPDXValueError:
424+
self.value_error('SNIPPET_SPDX_ID_VALUE', snippet_id)
425+
else:
426+
self.value_error('SNIPPET_SPDX_ID_VALUE', snippet_id)
427+
428+
def parse_snippet_name(self, snippet_name):
429+
"""
430+
Responsible for parsing Snippet name
431+
- snippet_name: Python str/unicode
432+
"""
433+
if isinstance(snippet_name, six.string_types):
434+
try:
435+
return self.builder.set_snippet_name(self.document, snippet_name)
436+
except CardinalityError:
437+
self.more_than_one_error('SNIPPET_NAME')
438+
elif snippet_name is not None:
439+
self.value_error('SNIPPET_NAME', snippet_name)
440+
441+
def parse_snippet_comment(self, snippet_comment):
442+
"""
443+
Responsible for parsing Snippet comment
444+
- snippet_comment: Python str/unicode
445+
"""
446+
if isinstance(snippet_comment, six.string_types):
447+
try:
448+
return self.builder.set_snippet_comment(self.document, snippet_comment)
449+
except CardinalityError:
450+
self.more_than_one_error('SNIPPET_COMMENT')
451+
elif snippet_comment is not None:
452+
self.value_error('SNIPPET_COMMENT', snippet_comment)
453+
454+
def parse_snippet_copyright(self, copyright_text):
455+
"""
456+
Responsible for parsing Snippet copyright text
457+
- copyright_text: Python str/unicode
458+
"""
459+
if isinstance(copyright_text, six.string_types):
460+
try:
461+
return self.builder.set_snippet_copyright(self.document, copyright_text)
462+
except CardinalityError:
463+
self.more_than_one_error('SNIPPET_COPYRIGHT')
464+
else:
465+
self.value_error('SNIPPET_COPYRIGHT', copyright_text)
466+
467+
def parse_snippet_license_comment(self, license_comment):
468+
"""
469+
Responsible for parsing Snippet license comment
470+
- license_comment: Python str/unicode
471+
"""
472+
if isinstance(license_comment, six.string_types):
473+
try:
474+
return self.builder.set_snippet_lic_comment(self.document, license_comment)
475+
except CardinalityError:
476+
self.more_than_one_error('SNIPPET_LIC_COMMENTS')
477+
elif license_comment is not None:
478+
self.value_error('SNIPPET_LIC_COMMENTS', license_comment)
479+
480+
def parse_snippet_file_spdxid(self, file_spdxid):
481+
"""
482+
Responsible for parsing Snippet file id
483+
- file_spdxid: Python str/unicode
484+
"""
485+
if isinstance(file_spdxid, six.string_types):
486+
try:
487+
return self.builder.set_snip_from_file_spdxid(self.document, file_spdxid)
488+
except SPDXValueError:
489+
self.value_error('SNIPPET_FILE_ID', file_spdxid)
490+
except CardinalityError:
491+
self.more_than_one_error('SNIPPET_FILE_ID')
492+
else:
493+
self.value_error('SNIPPET_FILE_ID', file_spdxid)
494+
495+
def parse_snippet_concluded_license(self, concluded_license):
496+
"""
497+
Responsible for parsing Snippet concluded license
498+
- concluded_license: Python str/unicode
499+
"""
500+
if isinstance(concluded_license, six.string_types):
501+
lic_parser = utils.LicenseListParser()
502+
lic_parser.build(write_tables=0, debug=0)
503+
license_object = self.replace_license(lic_parser.parse(concluded_license))
504+
try:
505+
return self.builder.set_snip_concluded_license(self.document, license_object)
506+
except SPDXValueError:
507+
self.value_error('SNIPPET_SINGLE_LICS', concluded_license)
508+
except CardinalityError:
509+
self.more_than_one_error('SNIPPET_SINGLE_LICS')
510+
else:
511+
self.value_error('SNIPPET_SINGLE_LICS', concluded_license)
512+
513+
def parse_snippet_license_info_from_snippet(self, license_info_from_snippet):
514+
"""
515+
Responsible for parsing Snippet license information from snippet
516+
- license_info_from_snippet: Python list of licenses information from snippet (str/unicode)
517+
"""
518+
if isinstance(license_info_from_snippet, list):
519+
for lic_in_snippet in license_info_from_snippet:
520+
if isinstance(lic_in_snippet, six.string_types):
521+
lic_parser = utils.LicenseListParser()
522+
lic_parser.build(write_tables=0, debug=0)
523+
license_object = self.replace_license(lic_parser.parse(lic_in_snippet))
524+
try:
525+
return self.builder.set_snippet_lics_info(self.document, license_object)
526+
except SPDXValueError:
527+
self.value_error('SNIPPET_LIC_INFO', lic_in_snippet)
528+
else:
529+
self.value_error('SNIPPET_LIC_INFO', lic_in_snippet)
530+
else:
531+
self.value_error('SNIPPET_LIC_INFO_FIELD', license_info_from_snippet)
532+
392533
class ReviewParser(BaseParser):
393534
def __init__(self, builder, logger):
394535
super(ReviewParser, self).__init__(builder, logger)

spdx/writers/jsonyaml.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,37 @@ def set_all_annotations_as_document_annotations(self):
307307
if annotation_objects:
308308
self.document_object['annotations'] = annotation_objects
309309

310+
class SnippetWriter(BaseWriter):
311+
"""
312+
Responsible for representing spdx.annotation as json-serializable objects
313+
"""
314+
def __init__(self, document):
315+
super(SnippetWriter, self).__init__(document)
316+
317+
def create_snippet_info(self):
318+
snippet_info_objects = []
319+
snippets = self.document.snippet
320+
321+
for snippet in snippets:
322+
snippet_object = dict()
323+
snippet_object['id'] = self.spdx_id(snippet.spdx_id)
324+
snippet_object['copyrightText'] = snippet.copyright
325+
snippet_object['fileId'] = self.spdx_id(snippet.snip_from_file_spdxid)
326+
snippet_object['licenseConcluded'] = self.license(snippet.conc_lics)
327+
snippet_object['licenseInfoFromSnippet'] = list(map(self.license, snippet.licenses_in_snippet))
328+
329+
if snippet.has_optional_field('name'):
330+
snippet_object['name'] = snippet.name
331+
332+
if snippet.has_optional_field('comment'):
333+
snippet_object['comment'] = snippet.comment
334+
335+
if snippet.has_optional_field('license_comment'):
336+
snippet_object['licenseComments'] = snippet.license_comment
337+
338+
snippet_info_objects.append(snippet_object)
339+
340+
return snippet_info_objects
310341

311342
class ExtractedLicenseWriter(BaseWriter):
312343
"""
@@ -360,7 +391,7 @@ def create_extracted_license(self):
360391
return extracted_license_objects
361392

362393
class Writer(CreationInfoWriter, ReviewInfoWriter, FileWriter, PackageWriter,
363-
AnnotationInfoWriter, ExtractedLicenseWriter):
394+
AnnotationInfoWriter, SnippetWriter, ExtractedLicenseWriter):
364395
"""
365396
Wrapper for the other writers.
366397
Responsible for representing a whole SPDX Document as json-serializable objects to then
@@ -413,6 +444,9 @@ def create_document(self):
413444

414445
if self.document.reviews:
415446
self.document_object['reviewers'] = self.create_review_info()
447+
448+
if self.document.snippet:
449+
self.document_object['snippets'] = self.create_snippet_info()
416450

417451
self.set_all_annotations_as_document_annotations()
418452

0 commit comments

Comments
 (0)