1
|
|
|
""" |
2
|
|
|
PRIVATE MODULE: do not import (from) it directly. |
3
|
|
|
|
4
|
|
|
This module contains implementations that do not directly touch the core of |
5
|
|
|
jsons. |
6
|
|
|
""" |
7
|
|
|
from typing import Optional |
8
|
|
|
|
9
|
|
|
from jsons._cache import cached |
10
|
|
|
from jsons._common_impl import StateHolder, get_class_name |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def suppress_warnings( |
14
|
|
|
do_suppress: Optional[bool] = True, |
15
|
|
|
fork_inst: Optional[type] = StateHolder): |
16
|
|
|
""" |
17
|
|
|
Suppress (or stop suppressing) warnings altogether. |
18
|
|
|
:param do_suppress: if ``True``, warnings will be suppressed from now on. |
19
|
|
|
:param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
20
|
|
|
:return: None. |
21
|
|
|
""" |
22
|
|
|
fork_inst._suppress_warnings = do_suppress |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def suppress_warning( |
26
|
|
|
code: str, |
27
|
|
|
fork_inst: Optional[type] = StateHolder): |
28
|
|
|
""" |
29
|
|
|
Suppress a specific warning that corresponds to the given code (see the |
30
|
|
|
warning). |
31
|
|
|
:param code: the code of the warning that is to be suppressed. |
32
|
|
|
:param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
33
|
|
|
:return: None. |
34
|
|
|
""" |
35
|
|
|
fork_inst._suppressed_warnings |= {code} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
@cached |
39
|
|
|
def announce_class( |
40
|
|
|
cls: type, |
41
|
|
|
cls_name: Optional[str] = None, |
42
|
|
|
fork_inst: type = StateHolder): |
43
|
|
|
""" |
44
|
|
|
Announce the given cls to jsons to allow jsons to deserialize a verbose |
45
|
|
|
dump into that class. |
46
|
|
|
:param cls: the class that is to be announced. |
47
|
|
|
:param cls_name: a custom name for that class. |
48
|
|
|
:param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
49
|
|
|
:return: None. |
50
|
|
|
""" |
51
|
|
|
cls_name = cls_name or get_class_name(cls, fully_qualified=True) |
52
|
|
|
fork_inst._announced_classes[cls] = cls_name |
53
|
|
|
fork_inst._announced_classes[cls_name] = cls |
54
|
|
|
|