Skip to content

codingame_tools.language.registry

registry

Discovery of every codingame_tools.language.languages module, and the lookup functions built on top of the resulting index--get_language/get_language_by_extension/ list_language_cg_ids. See codingame_tools.language.languages's package docstring for the discovery contract each language module must follow.

Discovery runs once, eagerly, the first time this module is imported (i.e. at codingame_tools.language package load time)--not lazily on first lookup. There are only 27 language modules today, so there's no meaningful cost concern either way; eager discovery is simply easier to reason about (the index is fully built before any caller could possibly observe it partially populated).

Importing CgLanguage/CgDefaultLanguage here is from .base/.default specifically, never from codingame_tools.language itself--the parent package's __init__.py is still mid-execution while this module runs during discovery, so importing from it here would hit a partially-initialized module. Every language module under .languages must follow the same rule (import CgLanguage from ..base, never from codingame_tools.language).

get_language

get_language(cg_id)

Look up the CgLanguage for a CodinGame protocol language ID.

Always succeeds: returns the real implementation if cg_id matches a discovered language plugin, or--for a cg_id this client has no record of at all--a CgDefaultLanguage bound to that ID anyway (memoized, so repeated lookups of the same unrecognized ID return the same object).

Source code in codingame_tools/language/registry.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_language(cg_id: str) -> CgLanguage:
    """Look up the `CgLanguage` for a CodinGame protocol language ID.

       Always succeeds: returns the real implementation if `cg_id` matches a discovered language
       plugin, or--for a `cg_id` this client has no record of at all--a `CgDefaultLanguage` bound
       to that ID anyway (memoized, so repeated lookups of the same unrecognized ID return the
       same object).
    """
    language = _by_cg_id.get(cg_id)
    if language is not None:
        return language
    unknown = _unknown_by_cg_id.get(cg_id)
    if unknown is None:
        unknown = CgDefaultLanguage(cg_id)
        _unknown_by_cg_id[cg_id] = unknown
    return unknown

get_language_by_extension

get_language_by_extension(filename_or_extension)

Look up the CgLanguage whose extension matches filename_or_extension (a bare extension, with or without a leading '.', or a full filename--only the suffix after the last '.' is considered; case-insensitive).

Returns:

  • CgLanguage | None

    The matching CgLanguage, or None if no known language claims this extension--there's

  • CgLanguage | None

    no reasonable default to fall back to, unlike get_language.

Source code in codingame_tools/language/registry.py
81
82
83
84
85
86
87
88
89
90
91
92
93
def get_language_by_extension(filename_or_extension: str) -> CgLanguage | None:
    """Look up the `CgLanguage` whose `extension` matches `filename_or_extension` (a bare
       extension, with or without a leading '.', or a full filename--only the suffix after the
       last '.' is considered; case-insensitive).

    Returns:
        The matching `CgLanguage`, or `None` if no known language claims this extension--there's
        no reasonable default to fall back to, unlike `get_language`.
    """
    ext = filename_or_extension.lower()
    if "." in ext:
        ext = ext.rsplit(".", 1)[1]
    return _by_extension.get(ext)

list_language_cg_ids

list_language_cg_ids()

The cg_ids of every discovered language plugin (sorted)--every language CodinGame is confirmed to support has one, whether or not it implements local execution.

Source code in codingame_tools/language/registry.py
96
97
98
99
def list_language_cg_ids() -> tuple[str, ...]:
    """The `cg_id`s of every discovered language plugin (sorted)--every language CodinGame is
       confirmed to support has one, whether or not it implements local execution."""
    return tuple(sorted(_by_cg_id))