Virtual ``conspectus.py`` file for brython. This file contains following
    variables:

    Attributes:
        consp_dict (dict): Dictionary with conspects.
        cosp_id_pairs (list): List of tuples ``(name, id)`` for conspectus.
        subs_by_mdt (dict): Dictionary containing ``mdt: sub_dict``.
        mdt_by_name (dict): ``name: mdt`` mapping.

    Note:
        Example of the `cons_dict` format::

            {
                "1": {
                    "id": "1",
                    "name": "Antropologie, etnografie",
                    "subconspects": {
                        "304": {
                            "conspect_id": "1",
                            "name": "Kulturn\u00ed politika"
                            "en_name": "Culture and institutions",
                            "mdt": "304",
                            "ddc": "306.2",
                        },
                        ...
                    }
                },
                ...
            }

    Values are stored in json and upacked after load. This is used because of
    performance issues with parsing large brython files.

def conspectus_api():
    """
    Virtual ``conspectus.py`` file for brython. This file contains following
    variables:

    Attributes:
        consp_dict (dict): Dictionary with conspects.
        cosp_id_pairs (list): List of tuples ``(name, id)`` for conspectus.
        subs_by_mdt (dict): Dictionary containing ``mdt: sub_dict``.
        mdt_by_name (dict): ``name: mdt`` mapping.

    Note:
        Example of the `cons_dict` format::

            {
                "1": {
                    "id": "1",
                    "name": "Antropologie, etnografie",
                    "subconspects": {
                        "304": {
                            "conspect_id": "1",
                            "name": "Kulturn\u00ed politika"
                            "en_name": "Culture and institutions",
                            "mdt": "304",
                            "ddc": "306.2",
                        },
                        ...
                    }
                },
                ...
            }

    Values are stored in json and upacked after load. This is used because of
    performance issues with parsing large brython files.
    """
    def to_json(data):
        """
        JSON conversion is used, because brython has BIG performance issues
        when parsing large python sources. It is actually cheaper to just load
        it as JSON objects.

        Load times:
            Brython: 24s firefox, 54s chromium
            JSON: 14s firefox, 9s chromium
        """
        return repr(json.dumps(data))

    conspectus_dict = json.loads(read_template("full_conspectus.json"))

    # raw conspectus dictionary
    out = PY_HEADER
    out += "import json\n"

    out += "consp_dict = json.loads(%s)\n\n" % to_json(conspectus_dict)

    # (consp, id) pairs
    cosp_id_pairs = sorted(
        (x["name"], x["id"])
        for x in conspectus_dict.values()
    )
    out += "cosp_id_pairs = json.loads(%s)\n\n" % to_json(cosp_id_pairs)

    # mdt -> subconspect mapping
    subs_by_mdt = [
        x["subconspects"].values()
        for x in conspectus_dict.values()
    ]
    subs_by_mdt = {d["mdt"]: d for d in sum(subs_by_mdt, [])}
    out += "subs_by_mdt = json.loads(%s)\n\n" % to_json(subs_by_mdt)

    # subconspect_name -> subconspect mapping
    out += "mdt_by_name = json.loads(%s)\n\n" % to_json({
        d["name"]: d["mdt"]
        for d in subs_by_mdt.values()
    })

    return out