Passed
Push — master ( 514a56...449f87 )
by Andrey
01:53 queued 29s
created

cookiecutter.replay   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 28
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 4
A get_file_name() 0 5 2
A dump() 0 17 5
1
"""
2
cookiecutter.replay.
3
4
-------------------
5
"""
6
import json
7
import os
8
9
from cookiecutter.utils import make_sure_path_exists
10
11
12
def get_file_name(replay_dir, template_name):
13
    """Get the name of file."""
14
    suffix = '.json' if not template_name.endswith('.json') else ''
15
    file_name = f'{template_name}{suffix}'
16
    return os.path.join(replay_dir, file_name)
17
18
19
def dump(replay_dir: "os.PathLike[str]", template_name: str, context: dict):
20
    """Write json data to file."""
21
    make_sure_path_exists(replay_dir)
22
23
    if not isinstance(template_name, str):
24
        raise TypeError('Template name is required to be of type str')
25
26
    if not isinstance(context, dict):
27
        raise TypeError('Context is required to be of type dict')
28
29
    if 'cookiecutter' not in context:
30
        raise ValueError('Context is required to contain a cookiecutter key')
31
32
    replay_file = get_file_name(replay_dir, template_name)
33
34
    with open(replay_file, 'w') as outfile:
35
        json.dump(context, outfile, indent=2)
36
37
38
def load(replay_dir, template_name):
39
    """Read json data from file."""
40
    if not isinstance(template_name, str):
41
        raise TypeError('Template name is required to be of type str')
42
43
    replay_file = get_file_name(replay_dir, template_name)
44
45
    with open(replay_file) as infile:
46
        context = json.load(infile)
47
48
    if 'cookiecutter' not in context:
49
        raise ValueError('Context is required to contain a cookiecutter key')
50
51
    return context
52