Skip to content

Load implicit and preview clients only when needed. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/judge0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@
"wait",
]

JUDGE0_IMPLICIT_CE_CLIENT = None
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = None


def _get_implicit_client(flavor: Flavor) -> Optional[Client]:
global JUDGE0_IMPLICIT_CE_CLIENT, JUDGE0_IMPLICIT_EXTRA_CE_CLIENT

# Implicit clients are already set.
if flavor == Flavor.CE and JUDGE0_IMPLICIT_CE_CLIENT is not None:
return JUDGE0_IMPLICIT_CE_CLIENT
if flavor == Flavor.EXTRA_CE and JUDGE0_IMPLICIT_EXTRA_CE_CLIENT is not None:
return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT

def _create_default_client(
flavor: Flavor,
preview_client_class: Union[SuluJudge0CE, SuluJudge0ExtraCE],
) -> Optional[Client]:
from .clients import CE, EXTRA_CE

try:
Expand All @@ -62,31 +70,31 @@ def _create_default_client(
else:
client_classes = EXTRA_CE

# Try to find one of the predefined keys JUDGE0_{SULU,RAPID,ATD}_API_KEY
# in environment variables.
client = None
for client_class in client_classes:
api_key = os.getenv(client_class.API_KEY_ENV)
if api_key is not None:
# It is possible for a client to be subscribed to one flavor with
# API key.
try:
client = client_class(api_key)
break
except Exception as e:
warnings.warn(f"Failed to initialize client: {e}")
client = client_class(api_key)
break

# If we didn't find any of the possible predefined keys, initialize
# the preview Sulu client based on the flavor.
if client is None:
if flavor == Flavor.CE:
client = SuluJudge0CE()
else:
client = SuluJudge0ExtraCE()

if flavor == Flavor.CE:
JUDGE0_IMPLICIT_CE_CLIENT = client
else:
try:
client = preview_client_class("")
except Exception as e:
warnings.warn(f"Failed to initialize preview client: {e}")
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = client

return client


JUDGE0_IMPLICIT_CE_CLIENT = _create_default_client(Flavor.CE, SuluJudge0CE)
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = _create_default_client(
Flavor.EXTRA_CE, SuluJudge0ExtraCE
)

CE = Flavor.CE
EXTRA_CE = Flavor.EXTRA_CE

Expand Down
42 changes: 14 additions & 28 deletions src/judge0/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,34 @@ def resolve_client(
client: Optional[Union[Client, Flavor]] = None,
submissions: Optional[Union[Submission, list[Submission]]] = None,
) -> Union[Client, None]:
from . import JUDGE0_IMPLICIT_CE_CLIENT, JUDGE0_IMPLICIT_EXTRA_CE_CLIENT

# User explicitly passed a client.
if isinstance(client, Client):
return client

from . import _get_implicit_client

# User explicitly choose the flavor of the client.
if isinstance(client, Flavor):
if client == Flavor.CE:
return JUDGE0_IMPLICIT_CE_CLIENT
else:
return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
return _get_implicit_client(flavor=client)

# client is None and we have to determine a flavor of the client from the
# submissions and the languages.
if isinstance(submissions, Submission):
submissions = [submissions]

# Check which client supports all languages from the provided submissions.
languages = [submission.language_id for submission in submissions]

if JUDGE0_IMPLICIT_CE_CLIENT is not None:
if all(
(
JUDGE0_IMPLICIT_CE_CLIENT.is_language_supported(lang)
for lang in languages
)
):
return JUDGE0_IMPLICIT_CE_CLIENT
languages = (submission.language_id for submission in submissions)

if JUDGE0_IMPLICIT_EXTRA_CE_CLIENT is not None:
if all(
(
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT.is_language_supported(lang)
for lang in languages
)
for flavor in Flavor:
client = _get_implicit_client(flavor)
if client is not None and all(
(client.is_language_supported(lang) for lang in languages)
):
return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
return client

raise RuntimeError(
"Failed to resolve the client from submissions argument."
"None of the implicit clients supports all languages from the submissions."
"Failed to resolve the client from submissions argument. "
"None of the implicit clients supports all languages from the submissions. "
"Please explicitly provide the client argument."
)

Expand All @@ -65,13 +51,13 @@ def wait(
if retry_mechanism is None:
retry_mechanism = RegularPeriodRetry()

if not isinstance(submissions, (list, tuple)):
if isinstance(submissions, (list, tuple)):
submissions_to_check = {
submission.token: submission for submission in [submissions]
submission.token: submission for submission in submissions
}
else:
submissions_to_check = {
submission.token: submission for submission in submissions
submission.token: submission for submission in [submissions]
}

while len(submissions_to_check) > 0 and not retry_mechanism.is_done():
Expand Down
15 changes: 9 additions & 6 deletions src/judge0/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,25 +402,28 @@ def __init__(self, api_key):
class Sulu(Client):
API_KEY_ENV = "JUDGE0_SULU_API_KEY"

def __init__(self, endpoint, api_key):
def __init__(self, endpoint, api_key=None):
self.api_key = api_key
super().__init__(endpoint, {"Authorization": f"Bearer {api_key}"})
super().__init__(
endpoint,
{"Authorization": f"Bearer {api_key}"} if api_key else None,
)


class SuluJudge0CE(Sulu):
DEFAULT_ENDPOINT: str = "https://judge0-ce.p.sulu.sh"
HOME_URL: str = "https://sparkhub.sulu.sh/apis/judge0/judge0-ce/readme"

def __init__(self, api_key):
super().__init__(self.DEFAULT_ENDPOINT, api_key=api_key)
def __init__(self, api_key=None):
super().__init__(self.DEFAULT_ENDPOINT, api_key)


class SuluJudge0ExtraCE(Sulu):
DEFAULT_ENDPOINT: str = "https://judge0-extra-ce.p.sulu.sh"
HOME_URL: str = "https://sparkhub.sulu.sh/apis/judge0/judge0-extra-ce/readme"

def __init__(self, api_key):
super().__init__(self.DEFAULT_ENDPOINT, api_key=api_key)
def __init__(self, api_key=None):
super().__init__(self.DEFAULT_ENDPOINT, api_key)


CE = [RapidJudge0CE, SuluJudge0CE, ATDJudge0CE]
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy