Skip to content

Commit

Permalink
Fix all typing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhijeetKrishnan committed Feb 16, 2024
1 parent 315ea77 commit d5ae240
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 78 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dev = [
"pre-commit",
"mypy",
"pytest",
"types-requests"
]

[tool.ruff]
Expand Down
4 changes: 2 additions & 2 deletions src/frame_service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .json_directory import JsonDirectory
from .wavu import Wavu
from .json_directory import JsonDirectory as JsonDirectory
from .wavu import Wavu as Wavu
1 change: 1 addition & 0 deletions src/frame_service/json_directory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .json_directory import JsonDirectory as JsonDirectory
43 changes: 43 additions & 0 deletions src/frame_service/json_directory/json_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
import logging
import os

import requests

from framedb import Character, CharacterName, FrameService, Move

logger = logging.getLogger("main")


class JsonDirectory(FrameService):
def __init__(self, char_meta_dir: str, movelist_dir: str) -> None:
self.name = f"JSON Directory ({movelist_dir})"
self.icon = None
self.movelist_dir = movelist_dir

try:
with open(char_meta_dir, "r") as f:
self.character_meta = json.load(f)
except Exception as e:
raise Exception(f"Could not load character meta data from {char_meta_dir}") from e

def get_frame_data(self, character: CharacterName, session: requests.Session | None = None) -> Character:
# TODO: duplicated across wavu and json_directory -> refactor
target_char_meta = None
for char_meta in self.character_meta:
if char_meta["name"] == character.value:
target_char_meta = char_meta
break
if target_char_meta is None:
raise Exception(f"Could not find character meta data for {character.value}")

name = CharacterName(target_char_meta["name"])
portrait = target_char_meta["portrait"]
page = target_char_meta["page"]

filepath = os.path.abspath(os.path.join(self.movelist_dir, f"{character.value}.json"))
with open(filepath, encoding="utf-8") as move_file:
move_file_contents = json.load(move_file)
movelist = {move["id"]: Move(**move) for move in move_file_contents}
char = Character(name, portrait, movelist, page)
return char
162 changes: 162 additions & 0 deletions src/frame_service/json_directory/tests/static/character_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
[
{
"name": "alisa",
"portrait": "https://i.imgur.com/5uhzI8W.png",
"page": "https://wavu.wiki/t/Alisa"
},
{
"name": "asuka",
"portrait": "https://i.imgur.com/yl6wb3s.png",
"page": "https://wavu.wiki/t/Asuka"
},
{
"name": "azucena",
"portrait": "https://i.imgur.com/fjMRO7I.png",
"page": "https://wavu.wiki/t/Azucena"
},
{
"name": "bryan",
"portrait": "https://i.imgur.com/2pZOovW.png",
"page": "https://wavu.wiki/t/Bryan"
},
{
"name": "claudio",
"portrait": "https://i.imgur.com/kBtrwM6.png",
"page": "https://wavu.wiki/t/Claudio"
},
{
"name": "devil_jin",
"portrait": "https://i.imgur.com/Y0pez4V.png",
"page": "https://wavu.wiki/t/Devil_Jin"
},
{
"name": "dragunov",
"portrait": "https://i.imgur.com/MZClYKp.png",
"page": "https://wavu.wiki/t/Dragunov"
},
{
"name": "feng",
"portrait": "https://i.imgur.com/kuBCcMo.png",
"page": "https://wavu.wiki/t/Feng"
},
{
"name": "hwoarang",
"portrait": "https://i.imgur.com/21UCjZ0.png",
"page": "https://wavu.wiki/t/Hwoarang"
},
{
"name": "jack-8",
"portrait": "https://i.imgur.com/YZgpYIf.png",
"page": "https://wavu.wiki/t/Jack-8"
},
{
"name": "jin",
"portrait": "https://i.imgur.com/ucx6sUa.png",
"page": "https://wavu.wiki/t/Jin"
},
{
"name": "jun",
"portrait": "https://i.imgur.com/J1nIsFj.png",
"page": "https://wavu.wiki/t/Jun"
},
{
"name": "kazuya",
"portrait": "https://i.imgur.com/HhPyKVn.png",
"page": "https://wavu.wiki/t/Kazuya"
},
{
"name": "king",
"portrait": "https://i.imgur.com/rJfQwwz.png",
"page": "https://wavu.wiki/t/King"
},
{
"name": "kuma",
"portrait": "https://i.imgur.com/HhPyKVn.png",
"page": "https://wavu.wiki/t/Kuma"
},
{
"name": "lars",
"portrait": "https://i.imgur.com/gt5tF85.png",
"page": "https://wavu.wiki/t/Lars"
},
{
"name": "law",
"portrait": "https://i.imgur.com/GmE9yOG.png",
"page": "https://wavu.wiki/t/Law"
},
{
"name": "lee",
"portrait": "https://i.imgur.com/ARAtm1t.png",
"page": "https://wavu.wiki/t/Lee"
},
{
"name": "leo",
"portrait": "https://i.imgur.com/fAIdWfw.png",
"page": "https://wavu.wiki/t/Leo"
},
{
"name": "leroy",
"portrait": "https://i.imgur.com/wUttHKT.png",
"page": "https://wavu.wiki/t/Leroy"
},
{
"name": "lili",
"portrait": "https://i.imgur.com/UWKqF0v.png",
"page": "https://wavu.wiki/t/Lili"
},
{
"name": "nina",
"portrait": "https://i.imgur.com/PAUToyh.png",
"page": "https://wavu.wiki/t/Nina"
},
{
"name": "panda",
"portrait": "https://i.imgur.com/IgAKZwR.png",
"page": "https://wavu.wiki/t/Panda"
},
{
"name": "paul",
"portrait": "https://i.imgur.com/44jv2IR.png",
"page": "https://wavu.wiki/t/Paul"
},
{
"name": "raven",
"portrait": "https://i.imgur.com/r1CYJOr.png",
"page": "https://wavu.wiki/t/Raven"
},
{
"name": "reina",
"portrait": "https://i.imgur.com/WaiMf45.png",
"page": "https://wavu.wiki/t/Reina"
},
{
"name": "shaheen",
"portrait": "https://i.imgur.com/Z0uYuBp.png",
"page": "https://wavu.wiki/t/Shaheen"
},
{
"name": "steve",
"portrait": "https://i.imgur.com/97msjwy.png",
"page": "https://wavu.wiki/t/Steve"
},
{
"name": "victor",
"portrait": "https://i.imgur.com/3gXDki2.png",
"page": "https://wavu.wiki/t/Victor"
},
{
"name": "xiaoyu",
"portrait": "https://i.imgur.com/tnZ4qby.png",
"page": "https://wavu.wiki/t/Xiaoyu"
},
{
"name": "yoshimitsu",
"portrait": "https://i.imgur.com/414HXeL.png",
"page": "https://wavu.wiki/t/Yoshimitsu"
},
{
"name": "zafina",
"portrait": "https://i.imgur.com/36rN3ZO.png",
"page": "https://wavu.wiki/t/Zafina"
}
]
21 changes: 21 additions & 0 deletions src/frame_service/json_directory/tests/test_json_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os

import pytest

from frame_service import JsonDirectory
from framedb import CharacterName

STATIC_BASE = os.path.join(os.path.dirname(__file__), "static")


@pytest.fixture
def json_directory() -> JsonDirectory:
return JsonDirectory(
char_meta_dir=os.path.join(STATIC_BASE, "character_list.json"),
movelist_dir=os.path.join(STATIC_BASE, "json_movelist"), # TODO: add static movelists to test dir
)


def test_get_movelist_from_json(json_directory: JsonDirectory) -> None:
char = json_directory.get_frame_data(CharacterName.AZUCENA, None)
assert char is not None
2 changes: 1 addition & 1 deletion src/frame_service/wavu/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .wavu import Wavu
from .wavu import Wavu as Wavu
22 changes: 11 additions & 11 deletions src/frame_service/wavu/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pytest.fixture
def wavu_response(request) -> Any:
def wavu_response(request: Any) -> Any:
with open(os.path.join(STATIC_BASE, f"{request.param.value}.json"), "r") as f:
return request.param, json.load(f)

Expand All @@ -22,9 +22,9 @@ def test_get_wavu_response() -> None:

class TestGetWavuCharacterMovelist:
@pytest.mark.parametrize("wavu_response", [CharacterName.AZUCENA], indirect=True)
def test_get_wavu_character_movelist(self, wavu_response) -> None:
def test_get_wavu_character_movelist(self, wavu_response: Any) -> None:
char_name, response = wavu_response
movelist = utils._get_wavu_character_movelist(char_name, content=response)
movelist = utils._get_wavu_character_movelist(response)
assert movelist["Azucena-1"].input == "1"
assert movelist["Azucena-df+1,4"].id == "Azucena-df+1,4"
assert movelist["Azucena-df+1,4,1"].input == "df+1,4,1"
Expand All @@ -38,30 +38,30 @@ def test_get_wavu_character_movelist(self, wavu_response) -> None:
assert movelist["Azucena-BT.3"].on_hit == "+4~+5"

@pytest.mark.parametrize("wavu_response", [CharacterName.ASUKA], indirect=True)
def test_get_wavu_character_movelist_aliases(self, wavu_response) -> None:
def test_get_wavu_character_movelist_aliases(self, wavu_response: Any) -> None:
char_name, response = wavu_response
movelist = utils._get_wavu_character_movelist(char_name, content=response)
movelist = utils._get_wavu_character_movelist(response)
assert movelist["Asuka-Destabilizer.1"].alias == () # TODO: what should this be?
assert movelist["Asuka-f+1+3"].alias == ("f+2+4",)

@pytest.mark.parametrize("wavu_response", [CharacterName.BRYAN], indirect=True)
def test_get_wavu_character_movelist_links(self, wavu_response) -> None:
def test_get_wavu_character_movelist_links(self, wavu_response: Any) -> None:
char_name, response = wavu_response
movelist = utils._get_wavu_character_movelist(char_name, content=response)
movelist = utils._get_wavu_character_movelist(response)
assert movelist["Bryan-4,3,f+4"].on_ch == "[+31a (+21)](https://wavu.wiki/t/Bryan_combos#Staples 'Combo')"

@pytest.mark.parametrize("wavu_response", [CharacterName.JUN], indirect=True)
def test_get_wavu_character_movelist_aliases_and_links(self, wavu_response) -> None:
def test_get_wavu_character_movelist_aliases_and_links(self, wavu_response: Any) -> None:
char_name, response = wavu_response
movelist = utils._get_wavu_character_movelist(char_name, content=response)
movelist = utils._get_wavu_character_movelist(response)
move = movelist["Jun-1,2,u_d"]
assert move.alias == ("1,2,d",)
assert move.input == "1,2,u"

@pytest.mark.parametrize("wavu_response", [CharacterName.JIN], indirect=True)
def test_get_wavu_character_movelist_html(self, wavu_response) -> None:
def test_get_wavu_character_movelist_html(self, wavu_response: Any) -> None:
char_name, response = wavu_response
movelist = utils._get_wavu_character_movelist(char_name, content=response)
movelist = utils._get_wavu_character_movelist(response)
assert movelist["Jin-1,2,3"].name == "Left Right > Axe Kick"


Expand Down
5 changes: 4 additions & 1 deletion src/frame_service/wavu/tests/test_wavu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

import requests

from frame_service import Wavu
from framedb import CharacterName
from framedb.const import NUM_CHARACTERS
Expand All @@ -15,7 +17,8 @@ def test_wavu_creation() -> None:

def test_get_frame_data() -> None:
wavu = Wavu()
char = wavu.get_frame_data(CharacterName.AZUCENA)
with requests.session() as session:
char = wavu.get_frame_data(CharacterName.AZUCENA, session)
assert char.name.value.title() == "Azucena"
assert char.portrait == "https://i.imgur.com/fjMRO7I.png"

Expand Down
21 changes: 11 additions & 10 deletions src/frame_service/wavu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ def _get_wavu_response(


def _get_wavu_character_movelist(
session: requests.Session, character_name: CharacterName, format: str = "json", content: Any = None
content: Any,
format: str = "json",
) -> Dict[str, Move]:
"""
Get the movelist for a character from a Wavu API response
"""

if content is None:
content = _get_wavu_response(session, character_name, format)
match format:
case "json":
movelist_raw = content["cargoquery"]
Expand Down Expand Up @@ -238,7 +237,7 @@ def _remove_html_tags(data: str) -> str:


def _process_links(data: str | None) -> str:
def _replace_link(matchobj):
def _replace_link(matchobj: re.Match[str]) -> str:
page, section, data = (
matchobj.group("page"),
matchobj.group("section"),
Expand All @@ -263,9 +262,11 @@ def _replace_link(matchobj):

if __name__ == "__main__":
# Fetch cargo movelists for all chars for testing purposes
for char in CharacterName:
print(f"Getting movelist for {char.value.title()}...")
movelist = _get_wavu_response(char)
print(f"Got {len(movelist)} moves for {char.value.title()}")
with open(os.path.join(os.path.dirname(__file__), "tests", "static", f"{char.value}.json"), "w") as f:
json.dump(movelist, f, indent=4)
with requests.session() as session:
for char in CharacterName:
print(f"Getting movelist for {char.value.title()}...")
response = _get_wavu_response(session, char)
movelist = _get_wavu_character_movelist(response)
print(f"Got {len(movelist)} moves for {char.value.title()}")
with open(os.path.join(os.path.dirname(__file__), "tests", "static", f"{char.value}.json"), "w") as f:
json.dump(movelist, f, indent=4)
9 changes: 6 additions & 3 deletions src/frame_service/wavu/wavu.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@


class Wavu(FrameService):
def __init__(self) -> None:
def __init__(self, _format: str = "json") -> None:
self.name = "Wavu Wiki"
self.icon = WAVU_LOGO
self._format = _format

try:
with open(WAVU_CHARACTER_META_PATH, "r") as f:
self.character_meta = json.load(f)
except Exception as e:
raise Exception(f"Could not load character meta data from {WAVU_CHARACTER_META_PATH}") from e

def get_frame_data(self, character: CharacterName, session: requests.Session) -> Character:
def get_frame_data(self, character: CharacterName, session: requests.Session | None = None) -> Character:
target_char_meta = None
for char_meta in self.character_meta:
if char_meta["name"] == character.value:
Expand All @@ -35,6 +36,8 @@ def get_frame_data(self, character: CharacterName, session: requests.Session) ->
portrait = target_char_meta["portrait"]
page = target_char_meta["page"]

movelist = utils._get_wavu_character_movelist(session, name)
assert session is not None
response = utils._get_wavu_response(session, name, self._format)
movelist = utils._get_wavu_character_movelist(response, self._format)
char = Character(name, portrait, movelist, page)
return char
Loading

0 comments on commit d5ae240

Please sign in to comment.