aboutsummaryrefslogtreecommitdiffstats
path: root/python/fatcat_tools/transforms/entities.py
blob: e5da633fde02e5cbbc2c22b1631a280c28901b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import collections
import json
from typing import Any, Dict, List, Mapping, Optional

import toml
from fatcat_openapi_client import ApiClient


def entity_to_dict(entity: Any, api_client: Optional[ApiClient] = None) -> Dict[str, Any]:
    """
    Hack to take advantage of the code-generated serialization code.

    Initializing/destroying ApiClient objects is surprisingly expensive
    (because it involves a threadpool), so we allow passing an existing
    instance. If you already have a full-on API connection `api`, you can
    access the ApiClient object as `api.api_client`. This is such a speed-up
    that this argument may become mandatory.
    """
    if not api_client:
        api_client = ApiClient()
    return api_client.sanitize_for_serialization(entity)


def entity_from_json(
    json_str: str, entity_type: Any, api_client: Optional[ApiClient] = None
) -> Any:
    """
    Hack to take advantage of the code-generated deserialization code

    See note on `entity_to_dict()` about api_client argument.
    """
    if not api_client:
        api_client = ApiClient()
    thing = collections.namedtuple("thing", ["data"])
    thing.data = json_str
    return api_client.deserialize(thing, entity_type)


def entity_from_dict(
    obj: Mapping[str, Any], entity_type: Any, api_client: Optional[ApiClient] = None
) -> Any:
    json_str = json.dumps(obj)
    return entity_from_json(json_str, entity_type, api_client=api_client)


def entity_to_toml(
    entity: Any, api_client: Optional[ApiClient] = None, pop_fields: Optional[List[str]] = None
) -> str:
    """
    pop_fields parameter can be used to strip out some fields from the resulting
    TOML. Eg, for fields which should not be edited, like the ident.
    """
    obj = entity_to_dict(entity, api_client=api_client)
    pop_fields = pop_fields or []
    for k in pop_fields:
        obj.pop(k, None)
    return toml.dumps(obj)


def entity_from_toml(
    toml_str: str, entity_type: Any, api_client: Optional[List[str]] = None
) -> Any:
    obj = toml.loads(toml_str)
    return entity_from_dict(obj, entity_type, api_client=api_client)