Passed
Push — master ( 1a4297...cea4ff )
by Konrad
08:18
created

db_sync_tool.__main__.build_config()   F

Complexity

Conditions 28

Size

Total Lines 90
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 90
rs 0
c 0
b 0
f 0
cc 28
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.__main__.build_config() 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: future_fstrings -*-
3
4
import argparse, sys, os
0 ignored issues
show
introduced by
Multiple imports on one line (argparse, sys, os)
Loading history...
5
from collections import defaultdict
6
# Workaround for ModuleNotFoundError
7
sys.path.append(os.getcwd())
8
from db_sync_tool import sync
0 ignored issues
show
introduced by
Import "from db_sync_tool import sync" should be placed at the top of the module
Loading history...
9
from db_sync_tool.utility import helper
0 ignored issues
show
introduced by
Import "from db_sync_tool.utility import helper" should be placed at the top of the module
Loading history...
10
11
12
def main(args={}):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
13
    """
14
    Main entry point for the command line. Parse the arguments and call to the main process.
15
    :param args:
16
    :return:
17
    """
18
    args = get_arguments(args)
19
    config = build_config(args)
20
    sync.Sync(
21
        config_file=args.config_file,
22
        verbose=args.verbose,
23
        mute=args.mute,
24
        import_file=args.import_file,
25
        dump_name=args.dump_name,
26
        keep_dump=args.keep_dump,
27
        host_file=args.host_file,
28
        config=config
29
    )
30
31
32
def get_arguments(args):
33
    """
34
    Parses and returns script arguments
35
    :param args:
36
    :return:
37
    """
38
    parser = argparse.ArgumentParser(prog='db_sync_tool', description='A tool for automatic database synchronization from and to host systems.')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (144/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
39
    parser.add_argument('-f', '--config-file',
40
                        help='Path to configuration file',
41
                        required=False,
42
                        type=str)
43
    parser.add_argument('-v', '--verbose',
44
                        help='Enable extended console output',
45
                        required=False,
46
                        action='store_true')
47
    parser.add_argument('-m', '--mute',
48
                        help='Mute console output',
49
                        required=False,
50
                        action='store_true')
51
    parser.add_argument('-i', '--import-file',
52
                        help='Import database from a specific file dump',
53
                        required=False,
54
                        type=str)
55
    parser.add_argument('-dn', '--dump-name',
56
                        help='Set a specific dump file name (default is "_[dbname]_[date]")',
57
                        required=False,
58
                        type=str)
59
    parser.add_argument('-kd', '--keep-dump',
60
                        help='Skipping target import of the database dump and saving the available dump file in the given directory',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (133/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
61
                        required=False,
62
                        type=str)
63
    parser.add_argument('-o', '--host-file',
64
                        help='Using an additional hosts file for merging hosts information with the configuration file',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (120/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
65
                        required=False,
66
                        type=str)
67
    parser.add_argument('-l', '--log-file',
68
                        help='File path for creating a additional log file',
69
                        required=False,
70
                        type=str)
71
    parser.add_argument('-t', '--type',
72
                        help='Defining the framework type [TYPO3, Symfony, Drupal, Wordpress]',
73
                        required=False,
74
                        type=str)
75
    parser.add_argument('-tp', '--target-path',
76
                        help='File path to target database credential file depending on the framework type',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
77
                        required=False,
78
                        type=str)
79
    parser.add_argument('-tn', '--target-name',
80
                        help='Providing a name for the target system',
81
                        required=False,
82
                        type=str)
83
    parser.add_argument('-th', '--target-host',
84
                        help='SSH host to target system',
85
                        required=False,
86
                        type=str)
87
    parser.add_argument('-tu', '--target-user',
88
                        help='SSH user for target system',
89
                        required=False,
90
                        type=str)
91
    parser.add_argument('-tpw', '--target-password',
92
                        help='SSH password for target system',
93
                        required=False,
94
                        type=str)
95
    parser.add_argument('-tk', '--target-key',
96
                        help='File path to SSH key for target system',
97
                        required=False,
98
                        type=str)
99
    parser.add_argument('-tpo', '--target-port',
100
                        help='SSH port for target system',
101
                        required=False,
102
                        type=int)
103
    parser.add_argument('-tdd', '--target-dump-dir',
104
                        help='Directory path for database dump file on target system',
105
                        required=False,
106
                        type=str)
107
    parser.add_argument('-tkd', '--target-keep-dumps',
108
                        help='Keep dump file count for target system',
109
                        required=False,
110
                        type=int)
111
    parser.add_argument('-tdn', '--target-db-name',
112
                        help='Database name for target system',
113
                        required=False,
114
                        type=str)
115
    parser.add_argument('-tdh', '--target-db-host',
116
                        help='Database host for target system',
117
                        required=False,
118
                        type=str)
119
    parser.add_argument('-tdu', '--target-db-user',
120
                        help='Database user for target system',
121
                        required=False,
122
                        type=str)
123
    parser.add_argument('-tdpw', '--target-db-password',
124
                        help='Database password for target system',
125
                        required=False,
126
                        type=str)
127
    parser.add_argument('-tdpo', '--target-db-port',
128
                        help='Database port for target system',
129
                        required=False,
130
                        type=int)
131
    parser.add_argument('-op', '--origin-path',
132
                        help='File path to origin database credential file depending on the framework type',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (108/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
133
                        required=False,
134
                        type=str)
135
    parser.add_argument('-on', '--origin-name',
136
                        help='Providing a name for the origin system',
137
                        required=False,
138
                        type=str)
139
    parser.add_argument('-oh', '--origin-host',
140
                        help='SSH host to origin system',
141
                        required=False,
142
                        type=str)
143
    parser.add_argument('-ou', '--origin-user',
144
                        help='SSH user for origin system',
145
                        required=False,
146
                        type=str)
147
    parser.add_argument('-opw', '--origin-password',
148
                        help='SSH password for origin system',
149
                        required=False,
150
                        type=str)
151
    parser.add_argument('-ok', '--origin-key',
152
                        help='File path to SSH key for origin system',
153
                        required=False,
154
                        type=str)
155
    parser.add_argument('-opo', '--origin-port',
156
                        help='SSH port for origin system',
157
                        required=False,
158
                        type=int)
159
    parser.add_argument('-odd', '--origin-dump-dir',
160
                        help='Directory path for database dump file on origin system',
161
                        required=False,
162
                        type=str)
163
    parser.add_argument('-okd', '--origin-keep-dumps',
164
                        help='Keep dump file count for origin system',
165
                        required=False,
166
                        type=int)
167
    parser.add_argument('-odn', '--origin-db-name',
168
                        help='Database name for origin system',
169
                        required=False,
170
                        type=str)
171
    parser.add_argument('-odh', '--origin-db-host',
172
                        help='Database host for origin system',
173
                        required=False,
174
                        type=str)
175
    parser.add_argument('-odu', '--origin-db-user',
176
                        help='Database user for origin system',
177
                        required=False,
178
                        type=str)
179
    parser.add_argument('-odpw', '--origin-db-password',
180
                        help='Database password for origin system',
181
                        required=False,
182
                        type=str)
183
    parser.add_argument('-odpo', '--origin-db-port',
184
                        help='Database port for origin system',
185
                        required=False,
186
                        type=int)
187
188
    return parser.parse_args(helper.dict_to_args(args))
189
190
191
def build_config(args):
192
    """
193
    Building an optional config
194
    :param args:
195
    :return:
196
    """
197
    config = defaultdict(dict)
198
199
    if not args.type is None:
200
        config['type'] = args.type
201
202
    if not args.target_path is None:
203
        config['target']['path'] = args.target_path
204
205
    if not args.target_name is None:
206
        config['target']['name'] = args.target_name
207
208
    if not args.target_host is None:
209
        config['target']['host'] = args.target_host
210
211
    if not args.target_user is None:
212
        config['target']['user'] = args.target_user
213
214
    if not args.target_password is None:
215
        config['target']['password'] = args.target_password
216
217
    if not args.target_key is None:
218
        config['target']['key'] = args.target_key
219
220
    if not args.target_port is None:
221
        config['target']['port'] = args.target_port
222
223
    if not args.target_dump_dir is None:
224
        config['target']['dump_dir'] = args.target_dump_dir
225
226
    if not args.target_db_name is None:
227
        config['target']['db']['name'] = args.target_db_name
228
229
    if not args.target_db_host is None:
230
        config['target']['db']['host'] = args.target_db_host
231
232
    if not args.target_db_user is None:
233
        config['target']['db']['user'] = args.target_db_user
234
235
    if not args.target_db_password is None:
236
        config['target']['db']['password'] = args.target_db_password
237
238
    if not args.target_db_port is None:
239
        config['target']['db']['port'] = args.target_db_port
240
241
    if not args.origin_path is None:
242
        config['origin']['path'] = args.origin_path
243
244
    if not args.origin_name is None:
245
        config['origin']['name'] = args.origin_name
246
247
    if not args.origin_host is None:
248
        config['origin']['host'] = args.origin_host
249
250
    if not args.origin_user is None:
251
        config['origin']['user'] = args.origin_user
252
253
    if not args.origin_password is None:
254
        config['origin']['password'] = args.origin_password
255
256
    if not args.origin_key is None:
257
        config['origin']['key'] = args.origin_key
258
259
    if not args.origin_port is None:
260
        config['origin']['port'] = args.origin_port
261
262
    if not args.origin_dump_dir is None:
263
        config['origin']['dump_dir'] = args.origin_dump_dir
264
265
    if not args.origin_db_name is None:
266
        config['origin']['db']['name'] = args.origin_db_name
267
268
    if not args.origin_db_host is None:
269
        config['origin']['db']['host'] = args.origin_db_host
270
271
    if not args.origin_db_user is None:
272
        config['origin']['db']['user'] = args.origin_db_user
273
274
    if not args.origin_db_password is None:
275
        config['origin']['db']['password'] = args.origin_db_password
276
277
    if not args.origin_db_port is None:
278
        config['origin']['db']['port'] = args.origin_db_port
279
280
    return config
281
282
283
if __name__ == "__main__":
284
    main()
285