Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Sep 20, 2024
1 parent f31420c commit 668009f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cropro/cropro.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def __init__(self, ankimw: AnkiQt) -> None:
super().__init__(ankimw=ankimw, window_title=ADDON_NAME)
self.window_state = WindowState(self)
self.other_col = CollectionManager()
self.web_search_client = CroProWebSearchClient()
self.web_search_client = CroProWebSearchClient(config)
self._add_window_mgr = AddDialogLauncher(self)
self._search_lock = SearchLock(self)
self._importer = NoteImporter(web_client=self.web_search_client)
Expand Down
49 changes: 22 additions & 27 deletions cropro/remote_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import anki.httpclient
import requests

from .config import config
from .config import CroProConfig

API_URL = "https://api.immersionkit.com/look_up_dictionary?"

Expand Down Expand Up @@ -78,36 +78,37 @@ class RemoteNote:
sound_url: str
notes: str
tags: list[str]
_config: CroProConfig

def __post_init__(self):
self._media = {
config.remote_fields.image: RemoteMediaInfo(
config.remote_fields.image,
self._config.remote_fields.image: RemoteMediaInfo(
self._config.remote_fields.image,
self.image_url,
MediaType.image,
),
config.remote_fields.sentence_audio: RemoteMediaInfo(
config.remote_fields.sentence_audio,
self._config.remote_fields.sentence_audio: RemoteMediaInfo(
self._config.remote_fields.sentence_audio,
self.sound_url,
MediaType.sound,
),
}
self._mapping = {
config.remote_fields.sentence_kanji: self.sentence_kanji,
config.remote_fields.sentence_furigana: self.sentence_furigana,
config.remote_fields.sentence_eng: self.sentence_eng,
config.remote_fields.sentence_audio: self.audio.as_anki_ref(),
config.remote_fields.image: self.image.as_anki_ref(),
config.remote_fields.notes: self.notes,
self._config.remote_fields.sentence_kanji: self.sentence_kanji,
self._config.remote_fields.sentence_furigana: self.sentence_furigana,
self._config.remote_fields.sentence_eng: self.sentence_eng,
self._config.remote_fields.sentence_audio: self.audio.as_anki_ref(),
self._config.remote_fields.image: self.image.as_anki_ref(),
self._config.remote_fields.notes: self.notes,
}

@property
def image(self) -> RemoteMediaInfo:
return self._media[config.remote_fields.image]
return self._media[self._config.remote_fields.image]

@property
def audio(self) -> RemoteMediaInfo:
return self._media[config.remote_fields.sentence_audio]
return self._media[self._config.remote_fields.sentence_audio]

def __contains__(self, item) -> bool:
return item in self._mapping
Expand Down Expand Up @@ -135,7 +136,7 @@ def items(self):
return self._mapping.items()

@classmethod
def from_json(cls, json_dict: ApiReturnExampleDict):
def from_json(cls, json_dict: ApiReturnExampleDict, config: CroProConfig):
return RemoteNote(
tags=remote_tags_as_list(json_dict),
image_url=json_dict["image_url"],
Expand All @@ -144,6 +145,7 @@ def from_json(cls, json_dict: ApiReturnExampleDict):
sentence_furigana=json_dict["sentence_with_furigana"],
sentence_eng=json_dict["translation"],
notes=json_dict["sentence_id"],
_config=config,
)


Expand All @@ -162,8 +164,12 @@ def what(self) -> str:


class CroProWebSearchClient:
def __init__(self) -> None:
_client: anki.httpclient.HttpClient
_config: CroProConfig

def __init__(self, config: CroProConfig) -> None:
self._client = anki.httpclient.HttpClient()
self._config = config

def _get(self, url: str) -> requests.Response:
try:
Expand All @@ -187,15 +193,4 @@ def search_notes(self, search_args: dict[str, str]) -> Sequence[RemoteNote]:
return []
resp = self._get(get_request_url(search_args))
examples = list(itertools.chain(*(item["examples"] for item in resp.json()["data"])))
return [RemoteNote.from_json(example) for example in examples]


def main():
client = CroProWebSearchClient()
result = client.search_notes({"keyword": "人事"})
for note in result:
print(note)


if __name__ == "__main__":
main()
return [RemoteNote.from_json(example, self._config) for example in examples]
36 changes: 0 additions & 36 deletions cropro/widgets/remote_search_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,3 @@ def jlpt_level_combo(self) -> QComboBox:
@property
def wanikani_level_combo(self) -> QComboBox:
return self._wanikani_level_combo


# Debug
##########################################################################


class App(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Test")
self.search_opts = RemoteSearchOptions()
self.initUI()

def initUI(self):
self.setMinimumSize(640, 480)
self.setLayout(layout := QVBoxLayout())
layout.addWidget(self.search_opts)
layout.addStretch(1)

def hideEvent(self, event: QHideEvent):
print(self.search_opts.category_combo.currentText())
print(self.search_opts.sort_combo.currentText())
print(self.search_opts.jlpt_level_combo.currentText())
print(self.search_opts.wanikani_level_combo.currentText())


def main():
app = QApplication(sys.argv)
ex = App()
ex.show()
app.exec()
sys.exit()


if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions run/mock_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import json
import pathlib

from cropro.config import CroProConfig


class NoAnkiConfig(CroProConfig):
"""
Loads the default config without starting Anki.
"""

def _set_underlying_dicts(self) -> None:
path = pathlib.Path(__file__).parent.parent.joinpath("cropro", "config.json")
with open(path) as f:
self._default_config = self._config = json.load(f)
16 changes: 16 additions & 0 deletions run/run_remote_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

from cropro.remote_search import CroProWebSearchClient
from run.mock_config import NoAnkiConfig


def main():
client = CroProWebSearchClient(NoAnkiConfig())
result = client.search_notes({"keyword": "人事"})
for note in result:
print(note)


if __name__ == "__main__":
main()
38 changes: 38 additions & 0 deletions run/run_remote_search_opts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

from aqt.qt import *

from cropro.widgets.remote_search_opts import RemoteSearchOptions


class App(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Test")
self.search_opts = RemoteSearchOptions()
self.initUI()

def initUI(self):
self.setMinimumSize(640, 480)
self.setLayout(layout := QVBoxLayout())
layout.addWidget(self.search_opts)
layout.addStretch(1)

def hideEvent(self, event: QHideEvent):
print(self.search_opts.category_combo.currentText())
print(self.search_opts.sort_combo.currentText())
print(self.search_opts.jlpt_level_combo.currentText())
print(self.search_opts.wanikani_level_combo.currentText())


def main():
app = QApplication(sys.argv)
ex = App()
ex.show()
app.exec()
sys.exit()


if __name__ == "__main__":
main()

0 comments on commit 668009f

Please sign in to comment.