parse_iso_datetime()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
c 1
b 0
f 1
dl 0
loc 10
rs 9.2
1
import collections
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
from functools import partial
3
from itertools import chain
4
from types import MappingProxyType
5
from typing import Callable
0 ignored issues
show
Configuration introduced by
The import typing could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6
from uuid import UUID
7
8
import iso8601
0 ignored issues
show
Configuration introduced by
The import iso8601 could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9
from foil.parsers import parse_iso_date as _parse_iso_date
10
11
12
def parse_uuid(value):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
13
    try:
14
        value = UUID(value, version=4)
15
    except ValueError:
16
        pass
17
18
    return value
19
20
21
def parse_iso_date(value):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
22
    try:
23
        value = _parse_iso_date(value)
24
    except AttributeError:
25
        pass
26
27
    return value
28
29
30
def parse_iso_datetime(value):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
31
32
    # Prevent iso8601 over-zealous datetime parsing
33
    if '-' in value and ':' in value:
34
        try:
35
            value = iso8601.parse_date(value)
36
        except iso8601.ParseError:
37
            pass
38
39
    return value
40
41
42
STRING_DECODERS = (parse_uuid, parse_iso_date, parse_iso_datetime)
43
44
45
def json_decoder_hook(dct, str_decoders=STRING_DECODERS,
46
                      converters=MappingProxyType(dict())) -> dict:
47
    """Decoder for parsing typical objects like uuid's and dates."""
48
49
    for k, v in dct.items():
50
        if k in converters:
51
            parse_func = converters[k]
52
            dct[k] = parse_func(v)
53
54
        elif isinstance(v, str):
55
            for decode_func in str_decoders:
56
                v = decode_func(v)
57
58
                if not isinstance(v, str):
59
                    break
60
61
            dct[k] = v
62
        elif isinstance(v, collections.Mapping):
63
            dct[k] = json_decoder_hook(v, str_decoders, converters)
64
65
    return dct
66
67
68
def make_json_decoder_hook(str_decoders=STRING_DECODERS,
69
                           extra_str_decoders=tuple(),
70
                           converters=MappingProxyType(dict())) -> Callable:
71
    """Customize JSON string decoder hooks.
72
73
    Object hook for typical deserialization scenarios.
74
75
    Notes
76
    -----
77
    Specifying a field in converters will ensure custom decoding/passthrough.
78
79
    Parameters
80
    ----------
81
    str_decoders: functions for decoding strings to objects.
82
    extra_str_decoders: appends additional string decoders to str_decoders.
83
    converters: field / parser function mapping.
84
    """
85
86
    str_decoders = tuple(chain(str_decoders, extra_str_decoders))
87
    object_hook = partial(json_decoder_hook, str_decoders=str_decoders,
88
                          converters=converters)
89
90
    return object_hook
91