Passed
Push — master ( ffbe9f...5c3c8b )
by Konrad
01:19
created

get_database_configuration()   F

Complexity

Conditions 18

Size

Total Lines 75
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 75
rs 1.2
c 0
b 0
f 0
cc 18
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like db_sync_tool.utility.parser.get_database_configuration() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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