summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorColin Wilk <colin.wilk@tum.de>2023-10-09 10:42:30 +0200
committerColin Wilk <colin.wilk@tum.de>2023-10-09 10:42:30 +0200
commit1da7f1638babdfe4173d0e87ff3b45b32e0c9123 (patch)
treec3e12222f6372b64651a6dee9b2da124840e2fd7 /src
parent0afdff3363897c55ccafa8692e15a066c1ba6dff (diff)
downloadszuruboorupy-1da7f1638babdfe4173d0e87ff3b45b32e0c9123.tar.gz
szuruboorupy-1da7f1638babdfe4173d0e87ff3b45b32e0c9123.zip
Add pydocstyle linter and apply suggestions
Signed-off-by: Colin Wilk <colin.wilk@tum.de>
Diffstat (limited to 'src')
-rw-r--r--src/lib/szurubooru.py121
1 files changed, 54 insertions, 67 deletions
diff --git a/src/lib/szurubooru.py b/src/lib/szurubooru.py
index 38c6265..2484691 100644
--- a/src/lib/szurubooru.py
+++ b/src/lib/szurubooru.py
@@ -55,7 +55,7 @@ class SzurubooruException(Exception):
@staticmethod
def response_is_exception(res: r.Response) -> bool:
- """Checks if the Szurubooru REST API response is an error or not.
+ """Check if the Szurubooru REST API response is an error or not.
Args:
res (r.Response): the Response of the request from the requests module.
@@ -63,12 +63,11 @@ class SzurubooruException(Exception):
Returns:
bool: True if response is an error and False if it is not.
"""
-
return res.status_code != 200
@staticmethod
def map_exception(res: r.Response) -> Exception:
- """Generates an exception for an error response from the Szurubooru REST API.
+ """Generate an exception for an error response from the Szurubooru REST API.
Args:
res (r.Response): the Response from the Szurubooru REST API that should be
@@ -83,7 +82,6 @@ class SzurubooruException(Exception):
Returns:
Exception: Returns a more specific exception.
"""
-
content: dict[str, str] = json.loads(res.content)
name: str = content["name"]
title: str = content["title"]
@@ -180,175 +178,175 @@ class SzurubooruException(Exception):
raise LookupError(f'Unknown SzurubooruException: "{name}"')
-class MissingRequiredFileError(SzurubooruException):
+class MissingRequiredFileError(SzurubooruException): # noqa: D101
pass
-class MissingRequiredParameterError(SzurubooruException):
+class MissingRequiredParameterError(SzurubooruException): # noqa: D101
pass
-class InvalidParameterError(SzurubooruException):
+class InvalidParameterError(SzurubooruException): # noqa: D101
pass
-class IntegrityError(SzurubooruException):
+class IntegrityError(SzurubooruException): # noqa: D101
pass
-class SearchError(SzurubooruException):
+class SearchError(SzurubooruException): # noqa: D101
pass
-class AuthError(SzurubooruException):
+class AuthError(SzurubooruException): # noqa: D101
pass
-class PostNotFoundError(SzurubooruException):
+class PostNotFoundError(SzurubooruException): # noqa: D101
pass
-class PostAlreadyFeaturedError(SzurubooruException):
+class PostAlreadyFeaturedError(SzurubooruException): # noqa: D101
pass
-class PostAlreadyUploadedError(SzurubooruException):
+class PostAlreadyUploadedError(SzurubooruException): # noqa: D101
pass
-class InvalidPostIdError(SzurubooruException):
+class InvalidPostIdError(SzurubooruException): # noqa: D101
pass
-class InvalidPostSafetyError(SzurubooruException):
+class InvalidPostSafetyError(SzurubooruException): # noqa: D101
pass
-class InvalidPostSourceError(SzurubooruException):
+class InvalidPostSourceError(SzurubooruException): # noqa: D101
pass
-class InvalidPostContentError(SzurubooruException):
+class InvalidPostContentError(SzurubooruException): # noqa: D101
pass
-class InvalidPostRelationError(SzurubooruException):
+class InvalidPostRelationError(SzurubooruException): # noqa: D101
pass
-class InvalidPostNoteError(SzurubooruException):
+class InvalidPostNoteError(SzurubooruException): # noqa: D101
pass
-class InvalidPostFlagError(SzurubooruException):
+class InvalidPostFlagError(SzurubooruException): # noqa: D101
pass
-class InvalidFavoriteTargetError(SzurubooruException):
+class InvalidFavoriteTargetError(SzurubooruException): # noqa: D101
pass
-class InvalidCommentIdError(SzurubooruException):
+class InvalidCommentIdError(SzurubooruException): # noqa: D101
pass
-class CommentNotFoundError(SzurubooruException):
+class CommentNotFoundError(SzurubooruException): # noqa: D101
pass
-class EmptyCommentTextError(SzurubooruException):
+class EmptyCommentTextError(SzurubooruException): # noqa: D101
pass
-class InvalidScoreTargetError(SzurubooruException):
+class InvalidScoreTargetError(SzurubooruException): # noqa: D101
pass
-class InvalidScoreValueError(SzurubooruException):
+class InvalidScoreValueError(SzurubooruException): # noqa: D101
pass
-class TagCategoryNotFoundError(SzurubooruException):
+class TagCategoryNotFoundError(SzurubooruException): # noqa: D101
pass
-class TagCategoryAlreadyExistsError(SzurubooruException):
+class TagCategoryAlreadyExistsError(SzurubooruException): # noqa: D101
pass
-class TagCategoryIsInUseError(SzurubooruException):
+class TagCategoryIsInUseError(SzurubooruException): # noqa: D101
pass
-class InvalidTagCategoryNameError(SzurubooruException):
+class InvalidTagCategoryNameError(SzurubooruException): # noqa: D101
pass
-class InvalidTagCategoryColorError(SzurubooruException):
+class InvalidTagCategoryColorError(SzurubooruException): # noqa: D101
pass
-class TagNotFoundError(SzurubooruException):
+class TagNotFoundError(SzurubooruException): # noqa: D101
pass
-class TagAlreadyExistsError(SzurubooruException):
+class TagAlreadyExistsError(SzurubooruException): # noqa: D101
pass
-class TagIsInUseError(SzurubooruException):
+class TagIsInUseError(SzurubooruException): # noqa: D101
pass
-class InvalidTagNameError(SzurubooruException):
+class InvalidTagNameError(SzurubooruException): # noqa: D101
pass
-class InvalidTagRelationError(SzurubooruException):
+class InvalidTagRelationError(SzurubooruException): # noqa: D101
pass
-class InvalidTagCategoryError(SzurubooruException):
+class InvalidTagCategoryError(SzurubooruException): # noqa: D101
pass
-class InvalidTagDescriptionError(SzurubooruException):
+class InvalidTagDescriptionError(SzurubooruException): # noqa: D101
pass
-class UserNotFoundError(SzurubooruException):
+class UserNotFoundError(SzurubooruException): # noqa: D101
pass
-class UserAlreadyExistsError(SzurubooruException):
+class UserAlreadyExistsError(SzurubooruException): # noqa: D101
pass
-class InvalidUserNameError(SzurubooruException):
+class InvalidUserNameError(SzurubooruException): # noqa: D101
pass
-class InvalidEmailError(SzurubooruException):
+class InvalidEmailError(SzurubooruException): # noqa: D101
pass
-class InvalidPasswordError(SzurubooruException):
+class InvalidPasswordError(SzurubooruException): # noqa: D101
pass
-class InvalidRankError(SzurubooruException):
+class InvalidRankError(SzurubooruException): # noqa: D101
pass
-class InvalidAvatarError(SzurubooruException):
+class InvalidAvatarError(SzurubooruException): # noqa: D101
pass
-class ProcessingError(SzurubooruException):
+class ProcessingError(SzurubooruException): # noqa: D101
pass
-class ValidationError(SzurubooruException):
+class ValidationError(SzurubooruException): # noqa: D101
pass
@@ -447,7 +445,7 @@ class Szurubooru:
)
def __map_tag_resource_response(self, res: r.Response, name: str = "") -> Tag:
- """Maps the requests response from the Szurubooru REST API to the Tag dataclass.
+ """Map the requests response from the Szurubooru REST API to the Tag dataclass.
This function is similar to __map_tag_resource and calls it internally.
Instead of receiving a dictionary if the Szurubooru Tag it operates on the
@@ -486,11 +484,10 @@ class Szurubooru:
-1. The name will be the first name returned by the Szurubooru REST API
or the name that was passed to the function as an optional parameter.
"""
-
return self.__map_tag_resource(json.loads(res.content), name)
def __map_tag_resource(self, c: dict, name: str = "") -> Tag:
- """Maps the dict from the Szurubooru REST API to the Tag dataclass.
+ """Map the dict from the Szurubooru REST API to the Tag dataclass.
This function is similar to __map_tag_resource_response and is called by it.
Instead of receiving a requests Response it operates on the dictionary of the
@@ -526,7 +523,6 @@ class Szurubooru:
-1. The name will be the first name returned by the Szurubooru REST API
or the name that was passed to the function as an optional parameter.
"""
-
return Tag(
name=name if name != "" else c["names"][0],
version=c.get("version", -1),
@@ -537,7 +533,7 @@ class Szurubooru:
)
def tag_exists(self, name: str) -> bool:
- """Checks if the tag exists on Szurubooru.
+ """Check if the tag exists on Szurubooru.
Args:
name (str): Name of the tag.
@@ -546,7 +542,6 @@ class Szurubooru:
bool: True if tag already exists. False if there was no matching tag on
Szurubooru.
"""
-
try:
res = self.__get(path=f"/api/tag/{quote(name)}")
except TagNotFoundError:
@@ -556,16 +551,15 @@ class Szurubooru:
return res.status_code == 200
def tag_len(self) -> int:
- """Checks how many tags there are on the Szurubooru instance.
+ """Check how many tags there are on the Szurubooru instance.
Returns:
int: The number of tags that exist on the Szurubooru instance
"""
-
return json.loads(self.__get(path="/api/tags").content)["total"]
def tag_list(self, limit: int = 100, offset: int = 0, query: str = "") -> List[Tag]:
- """Lists tags on the Szurubooru instance.
+ """List tags on the Szurubooru instance.
Args:
limit (int, optional): Limits the amount of tags displayed on one page.
@@ -587,13 +581,12 @@ class Szurubooru:
Returns:
List[Tag]: List of Tag objects in the current page.
"""
-
path = f"/api/tags?offset={offset}&limit={limit}&query={quote(query)}"
results = json.loads(self.__get(path=path).content)["results"]
return list(map(self.__map_tag_resource, results))
def tag_list_all(self, query: str = "") -> List[Tag]:
- """Lists every available Tag (matching the query) on the Szurubooru instance.
+ """List every available Tag (matching the query) on the Szurubooru instance.
WARNING: This method calls the Szurubooru REST API multiple times depending on
how many tags there are. It can take a while and can put significant load on the
@@ -613,7 +606,6 @@ class Szurubooru:
List[Tag]: List of all Tag objects matching the query on the Szurubooru
instance.
"""
-
tags = []
for i in range(0, math.ceil(self.tag_len() / 100)):
tags.extend(self.tag_list(offset=100 * i, limit=100, query=query))
@@ -631,7 +623,6 @@ class Szurubooru:
Returns:
Tag: Tag object of the requested tag.
"""
-
res = self.__get(path=f"/api/tag/{quote(name)}")
return self.__map_tag_resource_response(res, name=name)
@@ -646,7 +637,6 @@ class Szurubooru:
representing the created tag as it was created on the Szurubooru
instance.
"""
-
res = self.__post(
path="/api/tags",
params={
@@ -671,7 +661,6 @@ class Szurubooru:
representing the updated version of the tag as it is now on the
Szurubooru instance.
"""
-
res = self.__put(
path=f"/api/tag/{quote(tag.name)}",
params={
@@ -685,7 +674,7 @@ class Szurubooru:
return self.__map_tag_resource_response(res, name=tag.name)
def tag_delete(self, tag: Tag) -> None:
- """Deletes the given tag on the Szurubooru instance.
+ """Delete the given tag on the Szurubooru instance.
Args:
tag (Tag): Tag dataclass of the tag that should be reledte. Needs to contain
@@ -697,7 +686,7 @@ class Szurubooru:
return
def tag_merge(self, tag_to_merge: Tag, tag_to_merge_into: Tag) -> Tag:
- """Removes tag_to_merge and merges all uses into tag_to_merge_into.
+ """Remove tag_to_merge and merges all uses into tag_to_merge_into.
Removes source tag and merges all of its usages, suggestions and implications to
the target tag. Other tag properties such as category and aliases do not get
@@ -715,7 +704,6 @@ class Szurubooru:
Tag: Resulting Tag dataclass that was returned by the Szurubooru REST API
representing the new (merged) tag on the Szurubooru instance.
"""
-
res = self.__post(
path="/api/tag-merge",
params={
@@ -728,7 +716,7 @@ class Szurubooru:
return self.__map_tag_resource_response(res, name=tag_to_merge_into.name)
def tag_list_siblings(self, tag: Tag) -> List[dict[str, int]]:
- """Lists siblings of the given Tag
+ """List siblings of the given Tag
Lists siblings of given tag, e.g. tags that were used in the same posts as the
given tag. `occurrences` field signifies how many times a given sibling appears
@@ -743,7 +731,6 @@ class Szurubooru:
name as the key and the number of occurrences as the
value.
"""
-
res = self.__get(path=f"/api/tag-siblings/{tag.name}")
results = json.loads(res.content)["results"]
return list(map(lambda a: {a["tag"]["names"][0]: a["occurrences"]}, results))