Passed
Push — master ( de8926...d30041 )
by Oleksandr
11:49
created

tabpy.tabpy_server.management.util   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 51.52%

Importance

Changes 0
Metric Value
wmc 6
eloc 34
dl 0
loc 46
ccs 17
cts 33
cp 0.5152
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_state_from_file() 0 19 3
A write_state_config() 0 14 3
1 1
import logging
2 1
import os
3
4 1
try:
5 1
    from ConfigParser import ConfigParser as _ConfigParser
6 1
except ImportError:
7 1
    from configparser import ConfigParser as _ConfigParser
8 1
from tabpy.tabpy_server.app.app_parameters import ConfigParameters, SettingsParameters
9
10
11 1
def write_state_config(state, settings, logger=logging.getLogger(__name__)):
12
    if SettingsParameters.StateFilePath in settings:
13
        state_path = settings[SettingsParameters.StateFilePath]
14
    else:
15
        msg = f"{ConfigParameters.TABPY_STATE_PATH} is not set"
16
        logger.log(logging.CRITICAL, msg)
17
        raise ValueError(msg)
18
19
    logger.log(logging.DEBUG, f"State path is {state_path}")
20
    state_key = os.path.join(state_path, "state.ini")
21
    tmp_state_file = state_key
22
23
    with open(tmp_state_file, "w") as f:
24
        state.write(f)
25
26
27 1
def _get_state_from_file(state_path, logger=logging.getLogger(__name__)):
28 1
    state_key = os.path.join(state_path, "state.ini")
29 1
    tmp_state_file = state_key
30
31 1
    if not os.path.exists(tmp_state_file):
32
        msg = f"Missing config file at {tmp_state_file}"
33
        logger.log(logging.CRITICAL, msg)
34
        raise ValueError(msg)
35
36 1
    config = _ConfigParser(allow_no_value=True)
37 1
    config.optionxform = str
38 1
    config.read(tmp_state_file)
39
40 1
    if not config.has_section("Service Info"):
41
        msg = "Config error: Expected [Service Info] section in " f"{tmp_state_file}"
42
        logger.log(logging.CRITICAL, msg)
43
        raise ValueError(msg)
44
45
    return config
46