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

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import os
2from configparser import ConfigParser
3from typing import Optional
5from cleo.commands.command import Command
6from cleo.io.io import IO
7from pystratum_backend.Backend import Backend
8from pystratum_backend.StratumIO import StratumIO
11class BaseCommand(Command):
12 """
13 Base command for other commands of PyStratum.
14 """
16 # ------------------------------------------------------------------------------------------------------------------
17 def __init__(self):
18 """
19 Object constructor.
20 """
21 super().__init__()
23 self._config = ConfigParser()
24 """
25 The configuration object.
26 """
28 self._io: Optional[StratumIO] = None
29 """
30 The Output decorator.
31 """
33 # ------------------------------------------------------------------------------------------------------------------
34 def _create_backend_factory(self) -> Backend:
35 """
36 Creates the PyStratum Style object.
37 """
38 class_name = self._config['stratum']['backend']
40 parts = class_name.split('.')
41 module_name = ".".join(parts[:-1])
42 module = __import__(module_name)
43 for comp in parts[1:]:
44 module = getattr(module, comp)
46 return module()
48 # ------------------------------------------------------------------------------------------------------------------
49 def _read_config_file(self) -> None:
50 """
51 Reads the PyStratum configuration file.
53 :rtype: ConfigParser
54 """
55 config_filename = self.argument('config_file')
56 self._config.read(config_filename)
58 if 'database' in self._config and 'supplement' in self._config['database']:
59 path = os.path.join(os.path.dirname(config_filename), self._config.get('database', 'supplement'))
60 config_supplement = ConfigParser()
61 config_supplement.read(path)
63 if 'database' in config_supplement: 63 ↛ exitline 63 didn't return from function '_read_config_file', because the condition on line 63 was never false
64 options = config_supplement.options('database')
65 for option in options:
66 self._config['database'][option] = config_supplement['database'][option]
68 # ------------------------------------------------------------------------------------------------------------------
69 def execute(self, io: IO) -> int:
70 """
71 Executes this command.
73 :param io: The input/output object.
74 """
75 self._io = StratumIO(io.input, io.output, io.error_output)
77 return self.handle()
79# ----------------------------------------------------------------------------------------------------------------------