Skip to content

Commit

Permalink
Merge pull request #13 from a-luna:fix-data-bootstrapping-issues_2024…
Browse files Browse the repository at this point in the history
…0105

Refactor UnicodeDataCache class to use methods from UnicodeApiSettings class
a-luna authored Jan 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents bb7440d + eabeb0b commit a50d1ea
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 10 additions & 0 deletions app/config/api_settings.py
Original file line number Diff line number Diff line change
@@ -143,6 +143,16 @@ def get_unicode_planes_data(self) -> list[db.UnicodePlane]:
planes_dict = json.loads(self.PLANES_JSON.read_text()) if self.PLANES_JSON.exists() else UNICODE_PLANES_DEFAULT
return [db.UnicodePlane(**plane) for plane in planes_dict]

def get_unicode_blocks_data(self) -> list[db.UnicodeBlock]:
blocks_dict = json.loads(self.BLOCKS_JSON.read_text()) if self.BLOCKS_JSON.exists() else []
return [db.UnicodeBlock(**block) for block in blocks_dict]

def get_non_unihan_character_name_map(self) -> dict[int, str]:
if not self.CHAR_NAME_MAP.exists():
return {}
json_map = json.loads(self.CHAR_NAME_MAP.read_text())
return {int(codepoint): name for (codepoint, name) in json_map.items()}

def init_data_folders(self) -> None: # pragma: no cover
self.DB_FOLDER.mkdir(parents=True, exist_ok=True)
if self.DB_FILE.exists():
8 changes: 2 additions & 6 deletions app/data/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import itertools
import json
from functools import cache, cached_property

from rapidfuzz import process
@@ -31,18 +30,15 @@ def __init__(self):

@cached_property
def non_unihan_character_name_map(self) -> dict[int, str]:
json_map = json.loads(self.settings.CHAR_NAME_MAP.read_text())
return {int(codepoint): name for (codepoint, name) in json_map.items()}
return self.settings.get_non_unihan_character_name_map()

@property
def non_unihan_character_name_choices(self) -> dict[int, str]:
return {codepoint: name.lower() for (codepoint, name) in self.non_unihan_character_name_map.items()}

@cached_property
def blocks(self) -> list[db.UnicodeBlock]:
if not self.settings.BLOCKS_JSON.exists(): # pragma: no cover
return []
blocks = [db.UnicodeBlock(**block) for block in json.loads(self.settings.BLOCKS_JSON.read_text())]
blocks = self.settings.get_unicode_blocks_data()
for block in blocks:
block.plane = self.get_unicode_plane_containing_block_id(block.id if block.id else 0)
return blocks

0 comments on commit a50d1ea

Please sign in to comment.