@@ -603,46 +603,73 @@ def simple_tokenizer(self, expression):
603603 sym = LicenseSymbol (key = sym_or_op )
604604 yield Token (start , end , sym_or_op , sym )
605605
606- def validate (self , expression , strict = False , ** kwargs ):
607- data = {
608- 'normalized_license_expression' : '' ,
609- 'errors' : [],
610- 'valid_symbols' : [],
611- 'invalid_symbols' : [],
612- 'exception_symbols' : [],
613- }
606+ def validate (self , expression , strict = True , ** kwargs ):
607+ """
608+ Return a ExpressionInfo object that contains information about
609+ `expression` by parsing `expression` using Licensing.parse()
610+
611+ The ExpressionInfo class has the following fields:
612+ - normalized_license_expression: str
613+ - errors: list
614+ - valid_symbols: list
615+ - invalid_symbols: list
616+ - exception_symbols: list
617+
618+ If `expression` is valid, then
619+ `ExpressionInfo.normalized_license_expression` is set, along with a list
620+ of valid license symbols in `valid_symbols` and `exception_symbols`.
621+
622+ If an error was encountered when validating `expression`,
623+ `ExpressionInfo.errors` will be populated with strings containing the
624+ error message that has occured. If an error has occured due to invalid
625+ license symbols, the offending symbols will be present in
626+ `ExpressionInfo.invalid_symbols`
627+
628+ If `strict` is True, additional exceptions will be raised if in a "WITH"
629+ expression such as "XXX with ZZZ" if the XXX symbol has `is_exception`
630+ set to True or the YYY symbol has `is_exception` set to False. This
631+ checks that symbols are used strictly as constructed.
632+ """
633+ class ExpressionInfo :
634+ normalized_license_expression = ''
635+ errors = []
636+ valid_symbols = []
637+ invalid_symbols = []
638+ exception_symbols = []
639+
640+ data = ExpressionInfo ()
614641
615642 # Check `expression` type
616643 try :
617644 self .parse (expression )
618645 except ExpressionError as e :
619- data [ ' errors' ] .append (str (e ))
646+ data . errors .append (str (e ))
620647 return data
621648
622649 # Check `expression` syntax
623650 try :
624651 self .parse (expression , strict = strict )
625652 except ExpressionParseError as e :
626- data [ ' errors' ] .append (str (e ))
627- data [ ' invalid_symbols' ] .append (e .token_string )
653+ data . errors .append (str (e ))
654+ data . invalid_symbols .append (e .token_string )
628655 return data
629656
630657 # Check `expression` keys
631658 try :
632659 parsed_expression = self .parse (expression , strict = strict , validate = True )
633660 except ExpressionError as e :
634661 error_message = str (e )
635- data [ ' errors' ] .append (error_message )
662+ data . errors .append (error_message )
636663 if 'Unknown license key' in error_message :
637664 unknown_keys = self .unknown_license_keys (expression )
638- data [ ' invalid_symbols' ] .extend (unknown_keys )
665+ data . invalid_symbols .extend (unknown_keys )
639666 return data
640667
641668 # If we have not hit an exception, load `data` and return it
642669 symbols = list (parsed_expression .symbols )
643- data [ ' normalized_license_expression' ] = parsed_expression .render ()
644- data [ ' valid_symbols' ] = [s .render () for s in symbols ]
645- data [ ' exception_symbols' ] = [s .render () for s in symbols if isinstance (s , LicenseWithExceptionSymbol ) or s .is_exception ]
670+ data . normalized_license_expression = parsed_expression .render ()
671+ data . valid_symbols = [s .render () for s in symbols ]
672+ data . exception_symbols = [s .render () for s in symbols if isinstance (s , LicenseWithExceptionSymbol ) or s .is_exception ]
646673 return data
647674
648675
0 commit comments