Skip to content

Commit

Permalink
Black and isort
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroallenrevez committed Oct 4, 2021
1 parent 7217199 commit 9626b2d
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 161 deletions.
2 changes: 1 addition & 1 deletion jisho_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from rich.console import Console

console = Console()
console = Console()
63 changes: 36 additions & 27 deletions jisho_api/cli.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,75 @@
import json
import click
import pprint
from jisho_api.word.request import Word
from jisho_api.kanji.request import Kanji
from jisho_api.sentence.request import Sentence
from jisho_api import console

from pathlib import Path


import click
from rich.progress import Progress, track
from rich.prompt import Prompt
from rich.progress import track, Progress

from jisho_api import console
from jisho_api.kanji.request import Kanji
from jisho_api.sentence.request import Sentence
from jisho_api.word.request import Word


@click.group()
def main():
"""A jisho.org API. Test the API, or search the Japanese dictionary."""
pass


@click.group()
def search():
"""Search jisho.org for words, kanjis, or sentences."""
pass


@click.group()
def scrape():
"""Scrape requests, given a list of search terms."""
pass


@click.command(name="config")
def config():
"""Set ~/.jisho/config.json with cache settings."""
val = click.confirm("Cache enabled?")
p = Path.home() / '.jisho'
p = Path.home() / ".jisho"
p.mkdir(exist_ok=True)
with open(p / 'config.json', 'w') as fp:
with open(p / "config.json", "w") as fp:
json.dump({"cache": val}, fp, indent=4)
console.print("Config written to '.jisho/config.json'")


def _get_home_config():
p = Path.home() / '.jisho/config.json'
p = Path.home() / ".jisho/config.json"
if p.exists():
with open(p, 'r') as fp:
with open(p, "r") as fp:
return json.load(fp)
else:
return None


def _cache_enabled():
cfg = _get_home_config()
if cfg:
return cfg['cache']
return cfg["cache"]
return False



def scraper(cls, words, root_dump, cache=True):
words = {}
with Progress(console=console, transient=True) as progress:
task1 = progress.add_task("[green]Scraping...", total=len(words))
for i, w in enumerate(words):
# 0 - name should be between quotes to search specifically for it
# with a * it is a wildcard, to see applications of this word at the end
strict = '*' not in w
strict = "*" not in w
if strict:
w = f'"{w}"'

# 1 - if file exists do not request
word_path =root_dump / f'{w}.json'
word_path = root_dump / f"{w}.json"
if word_path.exists():
progress.advance(task1)
continue
Expand All @@ -76,16 +78,17 @@ def scraper(cls, words, root_dump, cache=True):
wr = cls.request(w, cache=cache)
if wr is None:
progress.advance(task1)
continue
continue
words[w] = wr

progress.advance(task1)
return words


def _load_words(file_path):
with open(file_path, 'r') as fp:
with open(file_path, "r") as fp:
txt = fp.read()
words = txt.split('\n')
words = txt.split("\n")
return words


Expand All @@ -97,6 +100,7 @@ def scrape_words(file_path: str):

scraper(Word, _load_words(file_path), root_dump)


@click.command(name="kanji")
@click.argument("file_path")
def scrape_kanji(file_path: str):
Expand All @@ -105,6 +109,7 @@ def scrape_kanji(file_path: str):

scraper(Kanji, _load_words(file_path), root_dump)


@click.command(name="sentence")
@click.argument("file_path")
def scrape_sentence(file_path: str):
Expand All @@ -113,36 +118,40 @@ def scrape_sentence(file_path: str):

scraper(Sentence, _load_words(file_path), root_dump)


@click.command(name="word")
@click.argument("word")
@click.option('--cache', type=bool, is_flag=True)
@click.option('--no-cache', type=bool, is_flag=True)
@click.option("--cache", type=bool, is_flag=True)
@click.option("--no-cache", type=bool, is_flag=True)
def request_word(word: str, cache: bool, no_cache: bool):
flag = (cache or _cache_enabled()) and not no_cache
w = Word.request(word, cache=flag)
if w:
w.rich_print()


@click.command(name="kanji")
@click.argument("kanji")
@click.option('--cache', type=bool, is_flag=True)
@click.option('--no-cache', type=bool, is_flag=True)
@click.option("--cache", type=bool, is_flag=True)
@click.option("--no-cache", type=bool, is_flag=True)
def request_kanji(kanji: str, cache: bool, no_cache: bool):
flag = (cache or _cache_enabled()) and not no_cache
k = Kanji.request(kanji, cache=flag)
if k:
k.rich_print()


@click.command(name="sentence")
@click.argument("sentence")
@click.option('--cache', type=bool, is_flag=True)
@click.option('--no-cache', type=bool, is_flag=True)
@click.option("--cache", type=bool, is_flag=True)
@click.option("--no-cache", type=bool, is_flag=True)
def request_sentence(sentence: str, cache: bool, no_cache: bool):
flag = (cache or _cache_enabled()) and not no_cache
k = Sentence.request(sentence, cache=flag)
if k:
k.rich_print()


# =============
# ==== CLI ====
# =============
Expand All @@ -162,4 +171,4 @@ def make_cli():


if __name__ == "__main__":
make_cli()
make_cli()
23 changes: 14 additions & 9 deletions jisho_api/kanji/cfg.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from pydantic import BaseModel, HttpUrl
from typing import List, Optional, Dict
from enum import Enum
from typing import Dict, List, Optional

from pydantic import BaseModel, HttpUrl
from rich.markdown import Markdown

from jisho_api import console
from jisho_api.util import CLITagger
from rich.markdown import Markdown


class JLPT(str, Enum):
N5 = 'N5'
N4 = 'N4'
N3 = 'N3'
N2 = 'N2'
N1 = 'N1'
N5 = "N5"
N4 = "N4"
N3 = "N3"
N2 = "N2"
N1 = "N1"


class MainReadings(BaseModel):
kun: Optional[List[str]]
on: Optional[List[str]]


class KanjiRadical(BaseModel):
alt_forms: Optional[List[str]]
meaning: str
Expand All @@ -36,7 +40,7 @@ class KanjiMetaReadings(BaseModel):
japanese: Optional[List[str]]
chinese: Optional[List[str]]
korean: Optional[List[str]]

education: Optional[KanjiMetaEducation]
dictionary_idxs: Dict[str, str]
classifications: Dict[str, str]
Expand All @@ -49,6 +53,7 @@ class Example(BaseModel):
kanji: str
reading: str
meanings: List[str]

kun: Optional[List[Example]]
on: Optional[List[Example]]

Expand Down
Loading

0 comments on commit 9626b2d

Please sign in to comment.