Completed
Push — master ( a8a578...6c485c )
by Ramon
19s queued 10s
created

jsons._fork_impl.fork()   A

Complexity

Conditions 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 9.6
c 0
b 0
f 0
cc 2
nop 2
1
"""
2
PRIVATE MODULE: do not import (from) it directly.
3
4
This module contains the implementation of ``fork()``.
5
"""
6
from typing import Type, Optional
7
from jsons._common_impl import StateHolder, get_class_name, T
8
9
10
def fork(
11
        fork_inst: Type[T] = StateHolder,
12
        name: Optional[str] = None) -> Type[T]:
13
    """
14
    Fork from the given ``StateHolder`` to create a separate "branch" of
15
    serializers and deserializers.
16
    :param fork_inst: The ``StateHolder`` on which the new fork is based.
17
    :param name: The ``__name__`` of the new ``type``.
18
    :return: A "fork inst" that can be used to separately store
19
    (de)serializers from the regular ``StateHolder``.
20
    """
21
    fork_inst._fork_counter += 1
22
    if name:
23
        class_name = name
24
    else:
25
        class_name = '{}_fork{}'.format(
26
            get_class_name(fork_inst, fork_inst=fork_inst),
27
            fork_inst._fork_counter
28
        )
29
    result = type(class_name, (fork_inst,), {})
30
    result._classes_serializers = fork_inst._classes_serializers.copy()
31
    result._classes_deserializers = fork_inst._classes_deserializers.copy()
32
    result._serializers = fork_inst._serializers.copy()
33
    result._deserializers = fork_inst._deserializers.copy()
34
    result._fork_counter = 0
35
    return result
36