Completed
Push — master ( d5c3aa...9d3bc0 )
by Oleksandr
01:08
created

_get_getter()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.3145

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 1
cts 6
cp 0.1666
rs 9.4285
cc 2
crap 4.3145
1
# coding: utf-8
2
3 1
from schematics.types import StringType, IntType, FloatType, BooleanType
4
5 1
from il2fb.config.ds.compat import configparser
6
7
8 1
_NO_VALUE = object()
9 1
_TYPE_TO_GETTER_MAP = {
10
    StringType: 'get',
11
    IntType: 'getint',
12
    FloatType: 'getfloat',
13
    BooleanType: 'getboolean',
14
}
15
16
17 1
def _get_getter(class_type, ini):
18
    try:
19
        method_name = _TYPE_TO_GETTER_MAP.get(class_type)
20
        return getattr(ini, method_name)
21
    except:
22
        return ini.get
23
24
25 1
def field_from_ini(field, ini, section, option, default=_NO_VALUE):
26
    getter = _get_getter(field.typeclass, ini)
27
    try:
28
        return getter(section, option)
29
    except (configparser.NoSectionError, configparser.NoOptionError):
30
        return (
31
            default
32
            if default is not _NO_VALUE else
33
            field.default
34
        )
35