Coverage for muuntaa/advisor.py: 100.00%

10 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-21 12:16:47 +00:00

1import re 

2from typing import Union 

3 

4UNDERSCORE = '_' 

5ID_UNKNOWN = 'out' 

6INVALID = '_invalid' 

7CSAF_FILENAME_PATTERN = re.compile(r'([^+\-a-z0-9]+)') 

8CSAF_FILE_SUFFIX = '.json' 

9 

10 

11def derive_csaf_filename(identifier: Union[str, None] = None, is_valid: bool = False) -> str: 

12 """Returns CSAF filename derived from the identifier (according to CSAF v2.0 OASIS standard) and the validity. 

13 

14 Cf. https://docs.oasis-open.org/csaf/csaf/v2.0/csaf-v2.0.html#51-filename 

15 

16 If the advisory is not valid, then the stem of the derived filename ends in `_invalid`. 

17 """ 

18 derived = CSAF_FILENAME_PATTERN.sub(UNDERSCORE, identifier.lower()) if identifier is not None else ID_UNKNOWN 

19 return f'{derived}{INVALID}{CSAF_FILE_SUFFIX}' if not is_valid else f'{derived}{CSAF_FILE_SUFFIX}'