Skip to content

Commit

Permalink
conform to new ruff config
Browse files Browse the repository at this point in the history
  • Loading branch information
AbstractUmbra committed Nov 27, 2024
1 parent 64afbff commit 5696a56
Show file tree
Hide file tree
Showing 49 changed files with 1,179 additions and 693 deletions.
17 changes: 12 additions & 5 deletions examples/create_manga.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import pathlib
from typing import TYPE_CHECKING

import hondana
Expand All @@ -26,26 +27,32 @@ async def main() -> None:

# Create the manga with them:
draft_manga = await client.create_manga(
title=manga_title, original_language=original_language, status=status, content_rating=content_rating
title=manga_title,
original_language=original_language,
status=status,
content_rating=content_rating,
)

# This manga is now created in "draft" state. This is outlined more here:
# https://api.mangadex.org/docs.html#section/Manga-Creation
# tl;dr it's to remove the spam creations and to ensure there's a cover on the manga... so let's do that now.
with open("our_cover.png", "rb") as file: # noqa: PTH123
cover = file.read()
cover = pathlib.Path("our_cover.png").read_bytes()

# When we upload a cover, we need to attribute it to a manga, so lets use the draft one we created.
uploaded_cover = await draft_manga.upload_cover(
cover=cover, volume=None, description="My awesome cover", locale="en"
cover=cover,
volume=None,
description="My awesome cover",
locale="en",
)
print(uploaded_cover)

# Now that our manga is covered and uploaded, let's submit it for approval with a version of 1:
submitted_manga = await draft_manga.submit_draft(version=1)
print(submitted_manga)

# NOTE: Something to note is that the version of draft MUST match the version of submitted manga during the approval stage.
# NOTE: Something to note is that the version of draft MUST match the version of
# submitted manga during the approval stage.


# we don't log out as exiting the context manager provides a clean exit.
Expand Down
3 changes: 2 additions & 1 deletion examples/download_manga.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ async def main() -> None:
feed = await manga.feed(limit=500, offset=0, translated_language=["en"])

# This is how you recursively download the chapters.
# The string in the `.download()` call is the path to save all the chapters in. It will recursively create it, if needed.
# The string in the `.download()` call is the path to save all the chapters in.
# It will recursively create it, if needed.
for chapter in feed.chapters:
await chapter.download(f"{manga.title}/{chapter.chapter}")

Expand Down
9 changes: 7 additions & 2 deletions examples/my_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ async def main() -> None:
fifteen_minutes_ago = datetime.datetime.now(datetime.UTC) - datetime.timedelta(minutes=15)

# And let's order the responses by created at descending
# we also coerce the type here to prevent typechecker issues. This isn't needed but if you use a typechecker this is good to do.
# we also coerce the type here to prevent typechecker issues.
# This isn't needed but if you use a typechecker this is good to do.
order = FeedOrderQuery(created_at=Order.descending)

# `feed` will return a ChapterFeed instance. This just has the response info and list of chapters.
feed = await client.get_my_feed(
limit=20, offset=0, translated_language=["en"], created_at_since=fifteen_minutes_ago, order=order
limit=20,
offset=0,
translated_language=["en"],
created_at_since=fifteen_minutes_ago,
order=order,
)

# Let's view the responses.
Expand Down
22 changes: 14 additions & 8 deletions examples/upload_a_chapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,25 @@ async def main() -> None:
) as upload_session:
# let's open up some files and use their paths...
files = [*pathlib.Path("./to_upload").iterdir()]
# the above is a quick and easy method to create a list of pathlib.Path objects based on the `./to_upload` directory.
# the above is a quick and easy method to create a list of pathlib.Path objects
# based on the `./to_upload` directory.

# First we pass the list of paths, adhering to the earlier note.
# this method does sort them (alphabetically) by default, you can toggle this behaviour by passing `sort=False`
# I recommend naming your files `1.png`, `2.png`, `3.png`, etc.
data = await upload_session.upload_images(files)
if data.has_failures:
print(
data.errored_files
) # this means the upload request has one or more errors, you may wish to restart the session once fixing the error or other steps.
data.errored_files,
)
# this means the upload request has one or more errors,
# you may wish to restart the session once fixing the error or other steps.

# Then we choose to commit that data, which returns a valid ``hondana.Chapter`` instance.
chapter = await upload_session.commit()

## You can also choose not to commit manually, exiting this context manager will commit for you, and discard the returned chapter data.
# You can also choose not to commit manually, exiting this context manager will
# commit for you, and discard the returned chapter data.


async def alternative_main() -> None:
Expand All @@ -65,7 +69,7 @@ async def alternative_main() -> None:
scanlator_groups = ["..."]

# This will create and return an instance of ``hondana.ChapterUpload``
## You can also use a manga ID, or a ``hondana.Manga`` instance as the first parameter
# You can also use a manga ID, or a ``hondana.Manga`` instance as the first parameter
upload_session = client.upload_session(
"...",
volume=volume,
Expand All @@ -84,10 +88,12 @@ async def alternative_main() -> None:
data = await upload_session.upload_images(files)
if data.has_failures:
print(
data.errored_files
) # this means the upload request has one or more errors, you may wish to restart the session once fixing the error or other steps.
data.errored_files,
)
# this means the upload request has one or more errors
# you may wish to restart the session once fixing the error or other steps.

## NOTE: You **MUST** commit when not using the context manager.
# NOTE: You **MUST** commit when not using the context manager.
chapter = await upload_session.commit()


Expand Down
11 changes: 8 additions & 3 deletions examples/use_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Preface note: DEBUG logging on `hondana` (specifically it's `http` module) will result in your token as well as other information that could be sensitive
## showing to the CLI. Be careful if sharing these logs.
# Preface note: DEBUG logging on `hondana` (specifically it's `http` module)
# will result in your token as well as other information that could be sensitive
# showing to the CLI. Be careful if sharing these logs.

from __future__ import annotations

Expand Down Expand Up @@ -32,7 +33,11 @@ async def main() -> None:

# `feed` will return a `hondana.ChapterFeed` instance.
feed = await client.get_my_feed(
limit=20, offset=0, translated_language=["en"], created_at_since=fifteen_minutes_ago, order=order
limit=20,
offset=0,
translated_language=["en"],
created_at_since=fifteen_minutes_ago,
order=order,
)

# Let's view the response repr.
Expand Down
3 changes: 2 additions & 1 deletion examples/using_query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ async def main() -> None:
collection = await client.manga_list(includes=manga_list_includes)
print(len(collection.manga))

# Since our default is all possible expansions, you can just call an empty constructor, and it will populate accordingly.
# Since our default is all possible expansions,
# you can just call an empty constructor, and it will populate accordingly.
chapter_list_includes = hondana.query.ChapterIncludes()
# We also have the `all()` classmethod should you wish to use that.

Expand Down
5 changes: 2 additions & 3 deletions hondana/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def show_version() -> None:

entries: list[str] = [
f"- Python v{version_info.major}.{version_info.minor}.{version_info.micro}-{version_info.releaselevel}",
f"- Hondana v{md_version_info.major}.{md_version_info.minor}."
f"{md_version_info.micro}-{md_version_info.releaselevel}",
f"- Hondana v{md_version_info.major}.{md_version_info.minor}.{md_version_info.micro}-{md_version_info.releaselevel}",
]

if md_version_info.releaselevel != "final":
Expand All @@ -54,7 +53,7 @@ def show_version() -> None:
uname = platform.uname()
entries.append(f"- System Info: {uname.system} {uname.release} {uname.version}")

print("\n".join(entries))
print("\n".join(entries)) # noqa: T201 # this is intended


def parse_args() -> tuple[argparse.ArgumentParser, argparse.Namespace]:
Expand Down
54 changes: 29 additions & 25 deletions hondana/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,33 @@ class Artist(AuthorArtistTag):
"""

__slots__ = (
"_http",
"_data",
"__manga",
"_attributes",
"_biography",
"_created_at",
"_data",
"_http",
"_manga_relationships",
"_relationships",
"_updated_at",
"booth",
"fan_box",
"fantia",
"id",
"name",
"image_url",
"twitter",
"pixiv",
"melon_book",
"fan_box",
"booth",
"name",
"namicomi",
"naver",
"nico_video",
"pixiv",
"skeb",
"fantia",
"tumblr",
"youtube",
"weibo",
"naver",
"namicomi",
"website",
"twitter",
"version",
"_biography",
"_created_at",
"_updated_at",
"_manga_relationships",
"__manga",
"website",
"weibo",
"youtube",
)

def __init__(self, http: HTTPClient, payload: ArtistResponse) -> None:
Expand Down Expand Up @@ -142,7 +142,8 @@ def __init__(self, http: HTTPClient, payload: ArtistResponse) -> None:
self._created_at: str = self._attributes["createdAt"]
self._updated_at: str = self._attributes["updatedAt"]
self._manga_relationships: list[MangaResponse] = RelationshipResolver["MangaResponse"](
relationships, "manga"
relationships,
"manga",
).resolve(with_fallback=False, remove_empty=True)
self.__manga: list[Manga] | None = None

Expand All @@ -152,6 +153,9 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return self.name

def __hash__(self) -> int:
return hash(self.id)

def __eq__(self, other: object) -> bool:
return isinstance(other, AuthorArtistTag) and self.id == other.id

Expand All @@ -166,7 +170,7 @@ def biography(self) -> str | None:
This property will attempt to get the ``"en"`` key first, and fallback to the first key in the object.
"""
if not self._biography:
return
return None

biography = self._biography.get("en")
if biography is None:
Expand All @@ -189,7 +193,7 @@ def localised_biography(self, language: LanguageCode) -> str | None:
The artist's biography in the specified language.
"""
if not self._biography:
return
return None

return self._biography.get(language)

Expand Down Expand Up @@ -244,7 +248,7 @@ def manga(self) -> list[Manga] | None:
if not self._manga_relationships:
return None

from .manga import Manga
from .manga import Manga # noqa: PLC0415 # cyclic import cheat

formatted = [Manga(self._http, item) for item in self._manga_relationships]

Expand Down Expand Up @@ -273,19 +277,19 @@ async def get_manga(self) -> list[Manga] | None:
return self.manga

if not self._manga_relationships:
return
return None

ids = [r["id"] for r in self._manga_relationships]

formatted: list[Manga] = []
from .manga import Manga
from .manga import Manga # noqa: PLC0415 # cyclic import cheat

for manga_id in ids:
data = await self._http.get_manga(manga_id, includes=MangaIncludes())
formatted.append(Manga(self._http, data["data"]))

if not formatted:
return
return None

self.__manga = formatted
return self.__manga
Expand Down
Loading

0 comments on commit 5696a56

Please sign in to comment.