1
|
1 |
|
import os |
2
|
1 |
|
from configparser import ConfigParser |
3
|
|
|
from typing import Optional |
4
|
1 |
|
|
5
|
1 |
|
from cleo.commands.command import Command |
6
|
1 |
|
from cleo.io.io import IO |
7
|
|
|
from pystratum_backend.Backend import Backend |
8
|
|
|
from pystratum_backend.StratumIO import StratumIO |
9
|
1 |
|
|
10
|
|
|
|
11
|
|
|
class BaseCommand(Command): |
12
|
|
|
""" |
13
|
|
|
Base command for other commands of PyStratum. |
14
|
|
|
""" |
15
|
1 |
|
|
16
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
17
|
|
|
def __init__(self): |
18
|
|
|
""" |
19
|
1 |
|
Object constructor. |
20
|
|
|
""" |
21
|
1 |
|
super().__init__() |
22
|
|
|
|
23
|
|
|
self._config = ConfigParser() |
24
|
|
|
""" |
25
|
|
|
The configuration object. |
26
|
|
|
""" |
27
|
|
|
|
28
|
1 |
|
self._io: Optional[StratumIO] = None |
29
|
1 |
|
""" |
30
|
|
|
The Output decorator. |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
34
|
|
|
def _create_backend_factory(self) -> Backend: |
35
|
|
|
""" |
36
|
1 |
|
Creates the PyStratum Style object. |
37
|
|
|
""" |
38
|
|
|
class_name = self._config['stratum']['backend'] |
39
|
|
|
|
40
|
1 |
|
parts = class_name.split('.') |
41
|
|
|
module_name = ".".join(parts[:-1]) |
42
|
1 |
|
module = __import__(module_name) |
43
|
1 |
|
for comp in parts[1:]: |
44
|
1 |
|
module = getattr(module, comp) |
45
|
1 |
|
|
46
|
1 |
|
return module() |
47
|
|
|
|
48
|
1 |
|
# ------------------------------------------------------------------------------------------------------------------ |
49
|
|
|
def _read_config_file(self) -> None: |
50
|
|
|
""" |
51
|
1 |
|
Reads the PyStratum configuration file. |
52
|
|
|
|
53
|
|
|
:rtype: ConfigParser |
54
|
|
|
""" |
55
|
|
|
config_filename = self.argument('config_file') |
56
|
|
|
self._config.read(config_filename) |
57
|
1 |
|
|
58
|
1 |
|
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
|
1 |
|
config_supplement = ConfigParser() |
61
|
1 |
|
config_supplement.read(path) |
62
|
1 |
|
|
63
|
1 |
|
if 'database' in config_supplement: |
64
|
|
|
options = config_supplement.options('database') |
65
|
1 |
|
for option in options: |
66
|
1 |
|
self._config['database'][option] = config_supplement['database'][option] |
67
|
1 |
|
|
68
|
1 |
|
# ------------------------------------------------------------------------------------------------------------------ |
69
|
|
|
def execute(self, io: IO) -> int: |
70
|
|
|
""" |
71
|
1 |
|
Executes this command. |
72
|
|
|
|
73
|
|
|
:param io: The input/output object. |
74
|
|
|
""" |
75
|
|
|
self._io = StratumIO(io.input, io.output, io.error_output) |
76
|
|
|
|
77
|
|
|
return self.handle() |
78
|
1 |
|
|
79
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
80
|
|
|
|