@@ -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+
392533class ReviewParser (BaseParser ):
393534 def __init__ (self , builder , logger ):
394535 super (ReviewParser , self ).__init__ (builder , logger )
0 commit comments