File: debug.py

package info (click to toggle)
python-xsdata 24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,936 kB
  • sloc: python: 29,257; xml: 404; makefile: 27; sh: 6
file content (30 lines) | stat: -rw-r--r-- 741 bytes parent folder | download
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
import json
from pathlib import Path
from typing import Any


def dump(obj: Any):
    """
    Write any object into a dump json file.

    For internal troubleshooting purposes only!!!
    """
    with Path.cwd().joinpath("xsdata_dump.json").open("w+") as f:
        json.dump(convert(obj), f, indent=4)


def convert(obj: Any) -> Any:
    """Dump any obj into a readable dictionary."""
    if not obj:
        return obj

    if isinstance(obj, list):
        return list(map(convert, obj))

    if isinstance(obj, dict):
        return {key: convert(value) for key, value in obj.items()}

    if hasattr(obj, "__slots__") and obj.__slots__:
        return {name: convert(getattr(obj, name)) for name in obj.__slots__}

    return str(obj)