Coverage for src/parsers/parsers.py: 100.00%

35 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-10-18 15:02 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from argparse import Namespace 

5from sys import exc_info 

6from typing import Dict, List, Optional 

7 

8# Components 

9from ..models.pipelines import Pipeline 

10from ..package.bundle import Bundle 

11from ..prints.colors import Colors 

12from ..types.yaml import YAML 

13from .gitlab import GitLab 

14 

15# Parsers class, pylint: disable=too-few-public-methods 

16class Parsers: 

17 

18 # Members 

19 __options: Namespace 

20 

21 # Constructor 

22 def __init__( 

23 self, 

24 options: Namespace, 

25 ) -> None: 

26 

27 # Prepare options 

28 self.__options = options 

29 

30 # Read 

31 def read(self) -> Optional[Pipeline]: 

32 

33 # Prepare inputs 

34 inputs: Dict[str, str] = {} 

35 if self.__options.input: 

36 for input_key_value in self.__options.input: 

37 input_fields: List[str] = input_key_value.split('=') 

38 if len(input_fields) == 2: 

39 input_key: str = input_fields[0] 

40 input_value: str = input_fields[1] 

41 inputs[input_key] = input_value 

42 

43 # Read GitLab CI YAML 

44 try: 

45 with open( 

46 self.__options.configuration, 

47 encoding='utf8', 

48 mode='r', 

49 ) as configuration_data: 

50 data: Optional[YAML.Data] = YAML.load( 

51 configuration_data, 

52 inputs=inputs, 

53 configure=self.__options.configure, 

54 ) 

55 if not data: 

56 raise YAML.Error( 

57 f'Empty YAML content found in \'{self.__options.configuration}\'...' 

58 ) 

59 return GitLab(self.__options).parse(data) 

60 except YAML.Error as exc: 

61 print(' ') 

62 print( 

63 f' {Colors.GREEN}{Bundle.NAME}: {Colors.RED}ERROR:' \ 

64 f' {Colors.BOLD}{exc}{Colors.RESET}' 

65 ) 

66 print(' ') 

67 except (FileNotFoundError, PermissionError): 

68 print(' ') 

69 print( 

70 f' {Colors.GREEN}{Bundle.NAME}: {Colors.RED}ERROR:' \ 

71 f' {Colors.BOLD}{str(exc_info()[1])}{Colors.RESET}' 

72 ) 

73 print(' ') 

74 

75 # Failure 

76 return None