Passed
Push — master ( 8d66d0...ffbe9f )
by Konrad
02:02
created

db_sync_tool.utility.parser.load_parser_target()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
cc 2
nop 1
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: utf-8 -*-
3
4
import sys
5
from db_sync_tool.utility import mode, connect, system, output
6
7
8
#
9
# GLOBALS
10
#
11
12
class Framework:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
13
    TYPO3 = 'TYPO3'
14
    SYMFONY = 'Symfony'
15
    DRUPAL = 'Drupal'
16
    WORDPRESS = 'Wordpress'
17
18
#
19
# FUNCTIONS
20
#
21
def get_framework():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
22
    return Framework
23
24
25
def get_database_configuration(client):
26
    """
27
    Getting database configuration of given client and defined sync base (framework type)
28
    :param client: String
29
    :return:
30
    """
31
    system.config['db'] = {}
32
33
    # check framework type
34
    _base = ''
35
    if 'type' in system.config['host']:
36
        _type = system.config['host']['type'].lower()
37
        if _type == 'typo3':
38
            # TYPO3 sync base
39
            _base = Framework.TYPO3
40
        elif _type == 'symfony':
41
            # Symfony sync base
42
            _base = Framework.SYMFONY
43
        elif _type == 'drupal':
44
            # Symfony sync base
45
            _base = Framework.DRUPAL
46
        elif _type == 'wordpress':
47
            # Symfony sync base
48
            _base = Framework.WORDPRESS
49
        else:
50
            sys.exit(
51
                output.message(
52
                    output.Subject.ERROR,
53
                    f'Framework type not supported: {_type}',
54
                    False
55
                )
56
            )
57
    else:
58
        # Default is TYPO3 sync base
59
        _base = Framework.TYPO3
60
61
    sys.path.append('../extension')
62
    if _base == Framework.TYPO3:
63
        # Import TYPO3 parser
64
        from ..extension import typo3
0 ignored issues
show
introduced by
Import outside toplevel (extension.typo3)
Loading history...
65
        _parser = typo3
66
67
    elif _base == Framework.SYMFONY:
68
        # Import Symfony parser
69
        from ..extension import symfony
0 ignored issues
show
introduced by
Import outside toplevel (extension.symfony)
Loading history...
70
        _parser = symfony
71
72
    elif _base == Framework.DRUPAL:
73
        # Import Symfony parser
74
        from ..extension import drupal
0 ignored issues
show
introduced by
Import outside toplevel (extension.drupal)
Loading history...
75
        _parser = drupal
76
77
    elif _base == Framework.WORDPRESS:
78
        # Import Symfony parser
79
        from ..extension import wordpress
0 ignored issues
show
introduced by
Import outside toplevel (extension.wordpress)
Loading history...
80
        _parser = wordpress
81
82
    if client == mode.Client.ORIGIN:
83
        output.message(
84
            output.Subject.INFO,
85
            'Sync base: ' + _base,
86
            True
87
        )
88
89
        load_parser_origin(_parser)
0 ignored issues
show
introduced by
The variable _parser does not seem to be defined for all execution paths.
Loading history...
90
    else:
91
        load_parser_target(_parser)
92
93
    validate_database_credentials(client)
94
95
96
def load_parser_origin(parser):
97
    """
98
    Loading origin parser
99
    :param parser:
100
    :return:
101
    """
102
    # @ToDo: Code duplication
103
    output.message(
104
        output.Subject.ORIGIN,
105
        'Checking database configuration',
106
        True
107
    )
108
    if mode.is_origin_remote():
109
        connect.load_ssh_client_origin()
110
        parser.check_remote_configuration(mode.Client.ORIGIN)
111
    else:
112
        connect.run_before_script(mode.Client.ORIGIN)
113
        parser.check_local_configuration(mode.Client.ORIGIN)
114
115
116
def load_parser_target(parser):
117
    """
118
    Loading target parser
119
    :param parser:
120
    :return:
121
    """
122
    # @ToDo: Code duplication
123
    output.message(
124
        output.Subject.TARGET,
125
        'Checking database configuration',
126
        True
127
    )
128
    if mode.is_target_remote():
129
        connect.load_ssh_client_target()
130
        parser.check_remote_configuration(mode.Client.TARGET)
131
    else:
132
        connect.run_before_script(mode.Client.TARGET)
133
        parser.check_local_configuration(mode.Client.TARGET)
134
135
136
def validate_database_credentials(client):
137
    """
138
    Validate the parsed database credentials
139
    :param client: String
140
    :return:
141
    """
142
    output.message(
143
        output.host_to_subject(client),
144
        'Validating database credentials',
145
        True
146
    )
147
    _db_credential_keys = ['dbname', 'host', 'password', 'port', 'user']
148
149
    for _key in _db_credential_keys:
150
        if _key not in system.config['db'][client]:
151
            sys.exit(
152
                output.message(
153
                    output.Subject.ERROR,
154
                    f'Missing database credential "{_key}" for {client} client',
155
                    False
156
                )
157
            )
158
        if system.config['db'][client][_key] is None or system.config['db'][client][_key] == '':
159
            sys.exit(
160
                output.message(
161
                    output.Subject.ERROR,
162
                    f'Missing database credential "{_key}" for {client} client',
163
                    False
164
                )
165
            )
166