diff --git a/scratchattach/__init__.py b/scratchattach/__init__.py index f10ae38b..63a9c59f 100644 --- a/scratchattach/__init__.py +++ b/scratchattach/__init__.py @@ -11,7 +11,13 @@ # from .other.project_json_capabilities import ProjectBody, get_empty_project_pb, get_pb_from_dict, read_sb3_file, download_asset from .utils.encoder import Encoding from .utils.enums import Languages, TTSVoices -from .utils.exceptions import LoginDataWarning +from .utils.exceptions import ( + LoginDataWarning, + GetAuthenticationWarning, + StudioAuthenticationWarning, + ClassroomAuthenticationWarning, + ProjectAuthenticationWarning, + UserAuthenticationWarning) from .site.activity import Activity, ActvityTypes from .site.backpack_asset import BackpackAsset @@ -19,7 +25,8 @@ from .site.cloud_activity import CloudActivity from .site.forum import ForumPost, ForumTopic, get_topic, get_topic_list, youtube_link_to_scratch from .site.project import Project, get_project, search_projects, explore_projects -from .site.session import Session, login, login_by_id, login_by_session_string, login_by_io, login_by_file, login_from_browser +from .site.session import Session, login, login_by_id, login_by_session_string, login_by_io, login_by_file, \ + login_from_browser from .site.studio import Studio, get_studio, search_studios, explore_studios from .site.classroom import Classroom, get_classroom from .site.user import User, get_user, Rank diff --git a/scratchattach/site/classroom.py b/scratchattach/site/classroom.py index 3262ee9c..4eacac89 100644 --- a/scratchattach/site/classroom.py +++ b/scratchattach/site/classroom.py @@ -396,7 +396,13 @@ def get_classroom(class_id: str) -> Classroom: If you want to use these, get the user with :meth:`scratchattach.session.Session.connect_classroom` instead. """ - warnings.warn("For methods that require authentication, use session.connect_classroom instead of get_classroom") + warnings.warn( + "For methods that require authentication, use session.connect_classroom instead of get_classroom\n" + "If you want to remove this warning, use warnings.filterwarnings('ignore', category=scratchattach.ClassroomAuthenticationWarning)\n" + "To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use " + "`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.", + exceptions.ClassroomAuthenticationWarning + ) return commons._get_object("id", class_id, Classroom, exceptions.ClassroomNotFound) @@ -415,7 +421,13 @@ def get_classroom_from_token(class_token) -> Classroom: If you want to use these, get the user with :meth:`scratchattach.session.Session.connect_classroom` instead. """ - warnings.warn("For methods that require authentication, use session.connect_classroom instead of get_classroom") + warnings.warn( + "For methods that require authentication, use session.connect_classroom instead of get_classroom. " + "If you want to remove this warning, use warnings.filterwarnings('ignore', category=ClassroomAuthenticationWarning). " + "To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use " + "warnings.filterwarnings('ignore', category=GetAuthenticationWarning).", + exceptions.ClassroomAuthenticationWarning + ) return commons._get_object("classtoken", class_token, Classroom, exceptions.ClassroomNotFound) diff --git a/scratchattach/site/project.py b/scratchattach/site/project.py index ae44c69b..891e1a12 100644 --- a/scratchattach/site/project.py +++ b/scratchattach/site/project.py @@ -6,6 +6,7 @@ import random import base64 import time +import warnings import zipfile from io import BytesIO from typing import Callable @@ -933,7 +934,14 @@ def get_project(project_id) -> Project: If you want to use these methods, get the project with :meth:`scratchattach.session.Session.connect_project` instead. """ - print("Warning: For methods that require authentication, use session.connect_project instead of get_project") + warnings.warn( + "Warning: For methods that require authentication, use session.connect_project instead of get_project.\n" + "If you want to remove this warning, " + "use `warnings.filterwarnings('ignore', category=scratchattach.ProjectAuthenticationWarning)`.\n" + "To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use " + "`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.", + exceptions.ProjectAuthenticationWarning + ) return commons._get_object("id", project_id, Project, exceptions.ProjectNotFound) diff --git a/scratchattach/site/studio.py b/scratchattach/site/studio.py index 4d254bc0..a9eaf93f 100644 --- a/scratchattach/site/studio.py +++ b/scratchattach/site/studio.py @@ -1,6 +1,7 @@ """Studio class""" from __future__ import annotations +import warnings import json import random from . import user, comment, project, activity @@ -592,7 +593,13 @@ def get_studio(studio_id) -> Studio: If you want to use these, get the studio with :meth:`scratchattach.session.Session.connect_studio` instead. """ - print("Warning: For methods that require authentication, use session.connect_studio instead of get_studio") + warnings.warn( + "Warning: For methods that require authentication, use session.connect_studio instead of get_studio.\n" + "If you want to remove this warning, use warnings.filterwarnings('ignore', category=scratchattach.StudioAuthenticationWarning).\n" + "To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use " + "`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.", + exceptions.StudioAuthenticationWarning + ) return commons._get_object("id", studio_id, Studio, exceptions.StudioNotFound) def search_studios(*, query="", mode="trending", language="en", limit=40, offset=0): diff --git a/scratchattach/site/user.py b/scratchattach/site/user.py index e7b9c81f..a9b35d1e 100644 --- a/scratchattach/site/user.py +++ b/scratchattach/site/user.py @@ -990,5 +990,11 @@ def get_user(username) -> User: If you want to use these, get the user with :meth:`scratchattach.session.Session.connect_user` instead. """ - print("Warning: For methods that require authentication, use session.connect_user instead of get_user") + warnings.warn( + "Warning: For methods that require authentication, use session.connect_user instead of get_user.\n" + "To ignore this warning, use warnings.filterwarnings('ignore', category=scratchattach.UserAuthenticationWarning).\n" + "To ignore all warnings of the type GetAuthenticationWarning, which includes this warning, use " + "`warnings.filterwarnings('ignore', category=scratchattach.GetAuthenticationWarning)`.", + exceptions.UserAuthenticationWarning + ) return commons._get_object("username", username, User, exceptions.UserNotFound) diff --git a/scratchattach/utils/exceptions.py b/scratchattach/utils/exceptions.py index e33d2a85..3ed22106 100644 --- a/scratchattach/utils/exceptions.py +++ b/scratchattach/utils/exceptions.py @@ -236,3 +236,32 @@ class InvalidUpdateWarning(UserWarning): """ Warns you that something cannot be updated. """ + +class GetAuthenticationWarning(UserWarning): + """ + All authentication warnings. + """ + +class UserAuthenticationWarning(GetAuthenticationWarning): + """ + Warns you to use session.connect_user instead of user.get_user + for actions that require authentication. + """ + +class ProjectAuthenticationWarning(GetAuthenticationWarning): + """ + Warns you to use session.connect_project instead of project.get_project + for actions that require authentication. + """ + +class StudioAuthenticationWarning(GetAuthenticationWarning): + """ + Warns you to use session.connect_studio instead of studio.get_studio + for actions that require authentication. + """ + +class ClassroomAuthenticationWarning(GetAuthenticationWarning): + """ + Warns you to use session.connect_classroom or session.connect_classroom_from_token instead of classroom.get_classroom + for actions that require authentication. + """