Skip to content

tux.wrappers.godbolt

Functions:

Name Description
checkresponse

Check the response from the Godbolt API.

sendresponse

Send the response from the Godbolt API.

getlanguages

Get the languages from the Godbolt API.

getcompilers

Get the compilers from the Godbolt API.

getspecificcompiler

Get the specific compiler from the Godbolt API.

getoutput

This function sends a POST request to the Godbolt API to get the output of the given code.

generateasm

Generate assembly code from the given code.

Functions

checkresponse(res: httpx.Response) -> str | None

Check the response from the Godbolt API.

Parameters:

Name Type Description Default
res Response

The response from the Godbolt API.

required

Returns:

Type Description
str | None

The response from the Godbolt API if successful, otherwise None.

Source code in tux/wrappers/godbolt.py
Python
def checkresponse(res: httpx.Response) -> str | None:
    """
    Check the response from the Godbolt API.

    Parameters
    ----------
    res : httpx.Response
        The response from the Godbolt API.

    Returns
    -------
    str | None
        The response from the Godbolt API if successful, otherwise None.
    """

    try:
        return res.text if res.status_code == httpx.codes.OK else None
    except httpx.ReadTimeout:
        return None

sendresponse(url: str) -> str | None

Send the response from the Godbolt API.

Parameters:

Name Type Description Default
url str

The URL to send the response from.

required

Returns:

Type Description
str | None

The response from the Godbolt API if successful, otherwise None.

Source code in tux/wrappers/godbolt.py
Python
def sendresponse(url: str) -> str | None:
    """
    Send the response from the Godbolt API.

    Parameters
    ----------
    url : str
        The URL to send the response from.

    Returns
    -------
    str | None
        The response from the Godbolt API if successful, otherwise None.
    """

    try:
        response = client.get(url)
        response.raise_for_status()
    except httpx.ReadTimeout:
        return None
    else:
        return response.text if response.status_code == httpx.codes.OK else None

getlanguages() -> str | None

Get the languages from the Godbolt API.

Returns:

Type Description
str | None

The languages from the Godbolt API if successful, otherwise None.

Source code in tux/wrappers/godbolt.py
Python
def getlanguages() -> str | None:
    """
    Get the languages from the Godbolt API.

    Returns
    -------
    str | None
        The languages from the Godbolt API if successful, otherwise None.
    """
    url_lang = f"{url}/api/languages"
    return sendresponse(url_lang)

getcompilers() -> str | None

Get the compilers from the Godbolt API.

Returns:

Type Description
str | None

The compilers from the Godbolt API if successful, otherwise None.

Source code in tux/wrappers/godbolt.py
Python
def getcompilers() -> str | None:
    """
    Get the compilers from the Godbolt API.

    Returns
    -------
    str | None
        The compilers from the Godbolt API if successful, otherwise None.
    """

    url_comp = f"{url}/api/compilers"
    return sendresponse(url_comp)

getspecificcompiler(lang: str) -> str | None

Get the specific compiler from the Godbolt API.

Parameters:

Name Type Description Default
lang str

The language to get the specific compiler for.

required

Returns:

Type Description
str | None

The specific compiler from the Godbolt API if successful, otherwise None.

Source code in tux/wrappers/godbolt.py
Python
def getspecificcompiler(lang: str) -> str | None:
    """
    Get the specific compiler from the Godbolt API.

    Parameters
    ----------
    lang : str
        The language to get the specific compiler for.

    Returns
    -------
    str | None
        The specific compiler from the Godbolt API if successful, otherwise None.
    """

    url_comp = f"{url}/api/compilers/{lang}"
    return sendresponse(url_comp)

getoutput(code: str, lang: str, compileroptions: str | None = None) -> str | None

This function sends a POST request to the Godbolt API to get the output of the given code.

Parameters:

Name Type Description Default
code str

The code to compile.

required
lang str

The language of the code.

required
compileroptions str | None

The compiler options, by default None

None

Returns:

Type Description
str | None

The output of the code if successful, otherwise None.

Raises:

Type Description
ReadTimeout

If the request times out.

Source code in tux/wrappers/godbolt.py
Python
def getoutput(code: str, lang: str, compileroptions: str | None = None) -> str | None:
    """
    This function sends a POST request to the Godbolt API to get the output of the given code.

    Parameters
    ----------
    code : str
        The code to compile.
    lang : str
        The language of the code.
    compileroptions : str | None, optional
        The compiler options, by default None

    Returns
    -------
    str | None
        The output of the code if successful, otherwise None.

    Raises
    ------
    httpx.ReadTimeout
        If the request times out.
    """

    url_comp = f"{url}/api/compiler/{lang}/compile"

    copt = compileroptions if compileroptions is not None else ""

    payload: Payload = {
        "source": code,
        "options": {
            "userArguments": copt,
            "compilerOptions": {"skipAsm": True, "executorRequest": False},
            "filters": {
                "binary": False,
                "binaryObject": False,
                "commentOnly": True,
                "demangle": True,
                "directives": True,
                "execute": True,
                "intel": True,
                "labels": True,
                "libraryCode": True,
                "trim": True,
                "debugCalls": True,
            },
            "tools": [],
            "libraries": [],
        },
        "lang": f"{lang}",
        "allowStoreCodeDebug": True,
    }
    uri = client.post(url_comp, json=payload)

    try:
        return uri.text if uri.status_code == httpx.codes.OK else None

    except httpx.ReadTimeout:
        return "Could not get data back from the host in time"

generateasm(code: str, lang: str, compileroptions: str | None = None) -> str | None

Generate assembly code from the given code.

Parameters:

Name Type Description Default
code str

The code to generate assembly from.

required
lang str

The language of the code.

required
compileroptions str | None

The compiler options, by default None

None

Returns:

Type Description
str | None

The assembly code if successful, otherwise None.

Raises:

Type Description
ReadTimeout

If the request times out.

Source code in tux/wrappers/godbolt.py
Python
def generateasm(code: str, lang: str, compileroptions: str | None = None) -> str | None:
    """
    Generate assembly code from the given code.

    Parameters
    ----------
    code : str
        The code to generate assembly from.
    lang : str
        The language of the code.
    compileroptions : str | None, optional
        The compiler options, by default None

    Returns
    -------
    str | None
        The assembly code if successful, otherwise None.

    Raises
    ------
    httpx.ReadTimeout
        If the request times out.
    """

    url_comp = f"{url}/api/compiler/{lang}/compile"

    copt = compileroptions if compileroptions is not None else ""

    payload: Payload = {
        "source": code,
        "options": {
            "userArguments": copt,
            "compilerOptions": {"skipAsm": False, "executorRequest": False},
            "filters": {
                "binary": False,
                "binaryObject": False,
                "commentOnly": True,
                "demangle": True,
                "directives": True,
                "execute": False,
                "intel": True,
                "labels": True,
                "libraryCode": True,
                "trim": True,
                "debugCalls": True,
            },
            "tools": [],
            "libraries": [],
        },
        "lang": f"{lang}",
        "allowStoreCodeDebug": True,
    }

    uri = client.post(url_comp, json=payload)

    try:
        return uri.text if uri.status_code == httpx.codes.OK else None

    except httpx.ReadTimeout:
        return "Could not get data back from the host in time"