Skip to content

Commit 7a1e1e6

Browse files
Boss-1sfaretek1TheCommCraft
authored
feat: Option to ignore warnings for methods that don't require authentication (#494)
Co-authored-by: retek <107722825+faretek1@users.noreply.github.com> Co-authored-by: TheCommCraft <79996518+TheCommCraft@users.noreply.github.com> Closes #486
1 parent e83c63e commit 7a1e1e6

File tree

6 files changed

+76
-7
lines changed

6 files changed

+76
-7
lines changed

scratchattach/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@
1111
# from .other.project_json_capabilities import ProjectBody, get_empty_project_pb, get_pb_from_dict, read_sb3_file, download_asset
1212
from .utils.encoder import Encoding
1313
from .utils.enums import Languages, TTSVoices
14-
from .utils.exceptions import LoginDataWarning
14+
from .utils.exceptions import (
15+
LoginDataWarning,
16+
GetAuthenticationWarning,
17+
StudioAuthenticationWarning,
18+
ClassroomAuthenticationWarning,
19+
ProjectAuthenticationWarning,
20+
UserAuthenticationWarning)
1521

1622
from .site.activity import Activity, ActvityTypes
1723
from .site.backpack_asset import BackpackAsset
1824
from .site.comment import Comment, CommentSource
1925
from .site.cloud_activity import CloudActivity
2026
from .site.forum import ForumPost, ForumTopic, get_topic, get_topic_list, youtube_link_to_scratch
2127
from .site.project import Project, get_project, search_projects, explore_projects
22-
from .site.session import Session, login, login_by_id, login_by_session_string, login_by_io, login_by_file, login_from_browser
28+
from .site.session import Session, login, login_by_id, login_by_session_string, login_by_io, login_by_file, \
29+
login_from_browser
2330
from .site.studio import Studio, get_studio, search_studios, explore_studios
2431
from .site.classroom import Classroom, get_classroom
2532
from .site.user import User, get_user, Rank

scratchattach/site/classroom.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,13 @@ def get_classroom(class_id: str) -> Classroom:
396396
397397
If you want to use these, get the user with :meth:`scratchattach.session.Session.connect_classroom` instead.
398398
"""
399-
warnings.warn("For methods that require authentication, use session.connect_classroom instead of get_classroom")
399+
warnings.warn(
400+
"For methods that require authentication, use session.connect_classroom instead of get_classroom\n"
401+
"If you want to remove this warning, use warnings.filterwarnings('ignore', category=scratchattach.ClassroomAuthenticationWarning)\n"
402+
"To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use "
403+
"`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.",
404+
exceptions.ClassroomAuthenticationWarning
405+
)
400406
return commons._get_object("id", class_id, Classroom, exceptions.ClassroomNotFound)
401407

402408

@@ -415,7 +421,13 @@ def get_classroom_from_token(class_token) -> Classroom:
415421
416422
If you want to use these, get the user with :meth:`scratchattach.session.Session.connect_classroom` instead.
417423
"""
418-
warnings.warn("For methods that require authentication, use session.connect_classroom instead of get_classroom")
424+
warnings.warn(
425+
"For methods that require authentication, use session.connect_classroom instead of get_classroom. "
426+
"If you want to remove this warning, use warnings.filterwarnings('ignore', category=ClassroomAuthenticationWarning). "
427+
"To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use "
428+
"warnings.filterwarnings('ignore', category=GetAuthenticationWarning).",
429+
exceptions.ClassroomAuthenticationWarning
430+
)
419431
return commons._get_object("classtoken", class_token, Classroom, exceptions.ClassroomNotFound)
420432

421433

scratchattach/site/project.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import random
77
import base64
88
import time
9+
import warnings
910
import zipfile
1011
from io import BytesIO
1112
from typing import Callable
@@ -933,7 +934,14 @@ def get_project(project_id) -> Project:
933934
934935
If you want to use these methods, get the project with :meth:`scratchattach.session.Session.connect_project` instead.
935936
"""
936-
print("Warning: For methods that require authentication, use session.connect_project instead of get_project")
937+
warnings.warn(
938+
"Warning: For methods that require authentication, use session.connect_project instead of get_project.\n"
939+
"If you want to remove this warning, "
940+
"use `warnings.filterwarnings('ignore', category=scratchattach.ProjectAuthenticationWarning)`.\n"
941+
"To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use "
942+
"`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.",
943+
exceptions.ProjectAuthenticationWarning
944+
)
937945
return commons._get_object("id", project_id, Project, exceptions.ProjectNotFound)
938946

939947

scratchattach/site/studio.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Studio class"""
22
from __future__ import annotations
33

4+
import warnings
45
import json
56
import random
67
from . import user, comment, project, activity
@@ -592,7 +593,13 @@ def get_studio(studio_id) -> Studio:
592593
593594
If you want to use these, get the studio with :meth:`scratchattach.session.Session.connect_studio` instead.
594595
"""
595-
print("Warning: For methods that require authentication, use session.connect_studio instead of get_studio")
596+
warnings.warn(
597+
"Warning: For methods that require authentication, use session.connect_studio instead of get_studio.\n"
598+
"If you want to remove this warning, use warnings.filterwarnings('ignore', category=scratchattach.StudioAuthenticationWarning).\n"
599+
"To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use "
600+
"`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.",
601+
exceptions.StudioAuthenticationWarning
602+
)
596603
return commons._get_object("id", studio_id, Studio, exceptions.StudioNotFound)
597604

598605
def search_studios(*, query="", mode="trending", language="en", limit=40, offset=0):

scratchattach/site/user.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,5 +990,11 @@ def get_user(username) -> User:
990990
991991
If you want to use these, get the user with :meth:`scratchattach.session.Session.connect_user` instead.
992992
"""
993-
print("Warning: For methods that require authentication, use session.connect_user instead of get_user")
993+
warnings.warn(
994+
"Warning: For methods that require authentication, use session.connect_user instead of get_user.\n"
995+
"To ignore this warning, use warnings.filterwarnings('ignore', category=scratchattach.UserAuthenticationWarning).\n"
996+
"To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use "
997+
"`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.",
998+
exceptions.UserAuthenticationWarning
999+
)
9941000
return commons._get_object("username", username, User, exceptions.UserNotFound)

scratchattach/utils/exceptions.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,32 @@ class InvalidUpdateWarning(UserWarning):
236236
"""
237237
Warns you that something cannot be updated.
238238
"""
239+
240+
class GetAuthenticationWarning(UserWarning):
241+
"""
242+
All authentication warnings.
243+
"""
244+
245+
class UserAuthenticationWarning(GetAuthenticationWarning):
246+
"""
247+
Warns you to use session.connect_user instead of user.get_user
248+
for actions that require authentication.
249+
"""
250+
251+
class ProjectAuthenticationWarning(GetAuthenticationWarning):
252+
"""
253+
Warns you to use session.connect_project instead of project.get_project
254+
for actions that require authentication.
255+
"""
256+
257+
class StudioAuthenticationWarning(GetAuthenticationWarning):
258+
"""
259+
Warns you to use session.connect_studio instead of studio.get_studio
260+
for actions that require authentication.
261+
"""
262+
263+
class ClassroomAuthenticationWarning(GetAuthenticationWarning):
264+
"""
265+
Warns you to use session.connect_classroom or session.connect_classroom_from_token instead of classroom.get_classroom
266+
for actions that require authentication.
267+
"""

0 commit comments

Comments
 (0)