Skip to content

Commit

Permalink
Format with black.
Browse files Browse the repository at this point in the history
  • Loading branch information
johndoknjas committed Oct 21, 2024
1 parent 5a242e1 commit b62f817
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions berserk/clients/studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,99 @@
from ..types import ChapterIdName
from .base import BaseClient

def append_query_params(url: str, clocks: bool, comments: bool, variations: bool,
source: bool, orientation: bool) -> str:
pairs = {'clocks': clocks, 'comments': comments, 'variations': variations,
'source': source, 'orientation': orientation}
return f'{url}?' + '&'.join(f'{k}={str(pairs[k]).lower()}' for k in pairs)

def append_query_params(
url: str,
clocks: bool,
comments: bool,
variations: bool,
source: bool,
orientation: bool,
) -> str:
pairs = {
"clocks": clocks,
"comments": comments,
"variations": variations,
"source": source,
"orientation": orientation,
}
return f"{url}?" + "&".join(f"{k}={str(pairs[k]).lower()}" for k in pairs)


class Studies(BaseClient):
"""Study chess the Lichess way."""

def export_chapter(self, study_id: str, chapter_id: str, clocks: bool = True,
comments: bool = True, variations: bool = True, source: bool = False,
orientation: bool = False) -> str:
def export_chapter(
self,
study_id: str,
chapter_id: str,
clocks: bool = True,
comments: bool = True,
variations: bool = True,
source: bool = False,
orientation: bool = False,
) -> str:
"""Export one chapter of a study.
:return: chapter PGN
"""
path = append_query_params(f"/api/study/{study_id}/{chapter_id}.pgn",
clocks, comments, variations, source, orientation)
path = append_query_params(
f"/api/study/{study_id}/{chapter_id}.pgn",
clocks,
comments,
variations,
source,
orientation,
)
return self._r.get(path, fmt=PGN)

def export(self, study_id: str, clocks: bool = True, comments: bool = True,
variations: bool = True, source: bool = False,
orientation: bool = False) -> Iterator[str]:
def export(
self,
study_id: str,
clocks: bool = True,
comments: bool = True,
variations: bool = True,
source: bool = False,
orientation: bool = False,
) -> Iterator[str]:
"""Export all chapters of a study.
:return: iterator over all chapters as PGN
"""
path = append_query_params(f"/api/study/{study_id}.pgn",
clocks, comments, variations, source, orientation)
path = append_query_params(
f"/api/study/{study_id}.pgn",
clocks,
comments,
variations,
source,
orientation,
)
yield from self._r.get(path, fmt=PGN, stream=True)

def export_by_username(self, username: str, clocks: bool = True, comments: bool = True,
variations: bool = True, source: bool = False,
orientation: bool = False) -> Iterator[str]:
def export_by_username(
self,
username: str,
clocks: bool = True,
comments: bool = True,
variations: bool = True,
source: bool = False,
orientation: bool = False,
) -> Iterator[str]:
"""Export all chapters of all studies of a user in PGN format.
If authenticated, then all public, unlisted, and private studies are included.
If not, only public (non-unlisted) studies are included.
return:iterator over all chapters as PGN"""
path = append_query_params(f"/study/by/{username}/export.pgn",
clocks, comments, variations, source, orientation)
path = append_query_params(
f"/study/by/{username}/export.pgn",
clocks,
comments,
variations,
source,
orientation,
)
yield from self._r.get(path, fmt=PGN, stream=True)

def import_pgn(
Expand Down

0 comments on commit b62f817

Please sign in to comment.