Coverage for pystratum_cli/command/BaseCommand.py: 98%

36 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-05-12 12:02 +0200

1import os 

2from configparser import ConfigParser 

3 

4from cleo import Command, Input, Output 

5from pystratum_backend.Backend import Backend 

6from pystratum_backend.StratumStyle import StratumStyle 

7 

8 

9class BaseCommand(Command): 

10 """ 

11 Base command for other commands of PyStratum. 

12 """ 

13 

14 # ------------------------------------------------------------------------------------------------------------------ 

15 def __init__(self): 

16 """ 

17 Object constructor. 

18 """ 

19 super().__init__() 

20 

21 self._config = ConfigParser() 

22 """ 

23 The configuration object. 

24 

25 :type: ConfigParser  

26 """ 

27 

28 self._io = None 

29 """ 

30 The Output decorator. 

31 

32 :type: StratumStyle|None  

33 """ 

34 

35 # ------------------------------------------------------------------------------------------------------------------ 

36 def _create_backend_factory(self) -> Backend: 

37 """ 

38 Creates the PyStratum Style object. 

39 """ 

40 class_name = self._config['stratum']['backend'] 

41 

42 parts = class_name.split('.') 

43 module_name = ".".join(parts[:-1]) 

44 module = __import__(module_name) 

45 for comp in parts[1:]: 

46 module = getattr(module, comp) 

47 

48 return module() 

49 

50 # ------------------------------------------------------------------------------------------------------------------ 

51 def _read_config_file(self, input_object: Input) -> None: 

52 """ 

53 Reads the PyStratum configuration file. 

54 

55 :rtype: ConfigParser 

56 """ 

57 config_filename = input_object.get_argument('config_file') 

58 self._config.read(config_filename) 

59 

60 if 'database' in self._config and 'supplement' in self._config['database']: 

61 path = os.path.join(os.path.dirname(config_filename), self._config.get('database', 'supplement')) 

62 config_supplement = ConfigParser() 

63 config_supplement.read(path) 

64 

65 if 'database' in config_supplement: 65 ↛ exitline 65 didn't return from function '_read_config_file', because the condition on line 65 was never false

66 options = config_supplement.options('database') 

67 for option in options: 

68 self._config['database'][option] = config_supplement['database'][option] 

69 

70 # ------------------------------------------------------------------------------------------------------------------ 

71 def execute(self, input_object: Input, output_object: Output) -> int: 

72 """ 

73 Executes this command. 

74 

75 :param input_object: The input object. 

76 :param output_object: The output object. 

77 """ 

78 self.input = input_object 

79 self.output = output_object 

80 

81 self._io = StratumStyle(input_object, output_object) 

82 

83 return self.handle() 

84 

85# ----------------------------------------------------------------------------------------------------------------------